findAll() in yii

后端 未结 8 969
日久生厌
日久生厌 2020-12-25 09:19

EmailArchive Table:

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 xy         


        
相关标签:
8条回答
  • 2020-12-25 09:27

    Use the below code. This should work.

    $comments = EmailArchive::find()->where(['email_id' => $id])->all();
    
    0 讨论(0)
  • 2020-12-25 09:29

    Another simple way get by using findall in yii

    $id =101; 
    $comments = EmailArchive::model()->findAll(array("condition"=>"':email_id'=$id"));
    foreach($comments as $comments_1) 
    { 
    echo  "email:".$comments_1['email_id'];
    }
    
    0 讨论(0)
  • 2020-12-25 09:29
    $id = 101;
    
    $sql = 'SELECT * FROM ur_tbl t WHERE t.email_id = '. $id;
    $email = Yii::app()->db->createCommand($sql)->queryAll();
    
    var_dump($email);
    
    0 讨论(0)
  • 2020-12-25 09:38

    If you use findAll(), I recommend you to use this:

    $data_email = EmailArchive::model()->findAll(
                      array(
                          'condition' => 'email_id = :email_id',
                          'params'    => array(':email_id' => $id)
                      )
                  );
    
    0 讨论(0)
  • 2020-12-25 09:38

    This is your safest way to do it:

    $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);
    

    Note that if you comment out the commented lines you get a way to add more filtering to your search.

    After this it is recommend to check if there is any data returned like:

    if (isset($comments)) { // We found some comments, we can sleep well tonight
      // do comments process or whatever
    }
    
    0 讨论(0)
  • 2020-12-25 09:42

    Just to add some alternate, you could do like this also:

    $id =101;
    $criteria = new CDbCriteria();
    $criteria->condition = "email_id =:email_id";
    $criteria->params = array(':email_id' => $id);
    $comments = EmailArchive::model()->findAll($criteria);
    
    0 讨论(0)
提交回复
热议问题