yii中的findAll()

时间:2012-03-16 11:23:37

标签: php yii yii-cactiverecord

EmailArchive表:

id email_id to from
1  101      uk  msm
2  102      uu  avc
3  101      rk  uk
4  103      xyz abc
5  104      xyz poi
6  104      abc xyz
7  101      xyz abc

现在在Yii我想要记录,其中email_id = 101 我使用下面的代码,但它不起作用。

$id =101;
$criteria = new CDbCriteria();
$criteria->addCondition("email_id < :email_id");
$comments = EmailArchive::model()->findAll($criteria, array(':email_id' => $id,));

8 个答案:

答案 0 :(得分:53)

尝试:

$id =101;
$comments = EmailArchive::model()->findAll(
array("condition"=>"email_id =  $id","order"=>"id"));

OR

$id =101;
$criteria = new CDbCriteria();
$criteria->addCondition("email_id=:email_id");
$criteria->params = array(':email_id' => $id);
$comments = EmailArchive::model()->findAll($criteria);

OR

$Criteria = new CDbCriteria();
$Criteria->condition = "email_id = $id";
$Products = Product::model()->findAll($Criteria);

答案 1 :(得分:6)

只是添加一些备用,你也可以这样做:

$id =101;
$criteria = new CDbCriteria();
$criteria->condition = "email_id =:email_id";
$criteria->params = array(':email_id' => $id);
$comments = EmailArchive::model()->findAll($criteria);

答案 2 :(得分:6)

如果您使用findAll(),我建议您使用此项:

$data_email = EmailArchive::model()->findAll(
                  array(
                      'condition' => 'email_id = :email_id',
                      'params'    => array(':email_id' => $id)
                  )
              );

答案 3 :(得分:2)

在yii中使用findall的另一种简单方法

$id =101; 
$comments = EmailArchive::model()->findAll(array("condition"=>"':email_id'=$id"));
foreach($comments as $comments_1) 
{ 
echo  "email:".$comments_1['email_id'];
}

答案 4 :(得分:1)

这是您最安全的方法:

$id =101;
//$user_id=25;
$criteria=new CDbCriteria;
$criteria->condition="email_id < :email_id";
//$criteria->addCondition("user_id=:user_id");
$criteria->params=array(
  ':email_id' => $id,
  //':user_id' => $user_id,
);
$comments=EmailArchive::model()->findAll($criteria);

请注意,如果您注释掉注释行,则可以为搜索添加更多过滤功能。

在此之后,建议检查是否有任何数据返回如下:

if (isset($comments)) { // We found some comments, we can sleep well tonight
  // do comments process or whatever
}

答案 5 :(得分:0)

$id = 101;

$sql = 'SELECT * FROM ur_tbl t WHERE t.email_id = '. $id;
$email = Yii::app()->db->createCommand($sql)->queryAll();

var_dump($email);

答案 6 :(得分:0)

如果您使用$ criteria,我建议使用打击:

$criteria = new CDbCriteria();
$criteria->compare('email_id', 101);
$comments = EmailArchive::model()->findAll($criteria);

答案 7 :(得分:0)

使用以下代码。这应该有用。

$comments = EmailArchive::find()->where(['email_id' => $id])->all();