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
Use the below code. This should work.
$comments = EmailArchive::find()->where(['email_id' => $id])->all();
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'];
}
$id = 101;
$sql = 'SELECT * FROM ur_tbl t WHERE t.email_id = '. $id;
$email = Yii::app()->db->createCommand($sql)->queryAll();
var_dump($email);
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)
)
);
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
}
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);