【问题标题】:findByAttributes ExamplefindByAttributes 示例
【发布时间】:2013-06-03 10:59:01
【问题描述】:

Yii中findByAttributes$condition$param的使用说明

在大多数情况下,这就是我使用findByAttributes的方式

Person::model()->findByAttributes(array('first_name'=>$firstName,'last_name'=>$lastName));

【问题讨论】:

  • 你先阅读this了吗?

标签: yii yii-cactiverecord


【解决方案1】:

【讨论】:

  • 如果我想解释 findByAttributes() 的源代码,我会仔细阅读文档
  • 很抱歉发布了类似的帖子,这是错误的
【解决方案2】:

当您使用模型的属性在 Yii 的 CActiveRecord 中进行查找时,您很可能会通过某种相等来查找。

Person::model()->findByAttributes(array(
  'first_name' => $firstName,
  'last_name' => $lastName,
));

在英语中,这翻译为“给我找一个姓氏为 $lastname 且名字为 $firstname 的人”。但这是一个不好的例子。请考虑以下内容。

Person::model()->findByAttributes(array(
  'age' => 18,
));

这是一个更好的例子,用英语翻译成“把所有 18 岁的孩子都给我找来”(即“age = 18 的位置”)。可以用这样的条件重写它

Person::model()->findByAttributes(array(
  'condition' => 'age = :age',
  'params' => array(
    ':age' => 18,
  ),
));

到目前为止,似乎没有什么好处。但是这样的表达式使我们能够使用更多的布尔运算符。比如……

Person::model()->findByAttributes(array(
  'condition' => 'age < :age',
  'params' => array(
    ':age' => 18,
  ),
));

...现在可以帮助我们找到所有低于特定年龄的人。

使用$conditions$params,查询可以随心所欲地增长,但使用常规方法,您只能使用赋值查询。

【讨论】:

  • 要让它与 yii 1.x 一起工作,你必须添加一个数组(): Person::model()->findByAttributes(array(), array( 'condition' => 'age 数组(':age' => 18, ), ));
猜你喜欢
  • 2011-11-10
  • 2013-05-01
  • 1970-01-01
  • 2011-11-21
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多