Question

Im new to yii. my problem is, I have an array $hometeamid which contains all the id that i need to display details. How can i get the data. plz somebody help me
I have tried
$mod = Yii::app()->db->createCommand('SELECT * FROM event WHERE id = 1432201 or id = 1432208')->queryAll();

 $no=1432201;
 $mod = Event::model()->findByAttributes(array('id' => $no));<br>

but does not working. Blank fields are showing.

Was it helpful?

Solution

Best to do addInCriteria wiith CDbCriteria or simple createCommand like, but before do that please check your query on phpmyadmin/navicate/mysql workbench etc. manually (is it returning result or not?)

$homeTeamIds=['1','2','3','4'];
$criteria = new CDbCriteria;
$criteria->addInCondition('id',$homeTeamIds,'OR'); //matching any of your homeTeadIds 
$result = Event::model()->findAll($criteria);

OR

$ids = implode(', ', $homeTeamIds);
$sql = "SELECT * FROM event WHERE id IN ($ids)"   // like where id IN(1,2,3,4)
$result = Yii::app()->db->createCommand($sql)->queryAll();

OTHER TIPS

dont use findByAttributes if you set the id as primary key in your db.

example:

$mod = Event::model()->findbyPk($no);

You can use CDbCriteria's addInCondition() method to find all rows with values matching items in an array, for example something like this should work (not tested):

$hometeamid = array(
    '1432201',
    '1432208',
    // ... etc... all the id's you want to show
);
$criteria = new CDbCriteria;
$criteria->addInCondition('id',$hometeamid,'OR');
$mod = Event::model()->findAll($criteria);

There's even an example using addInCondition at the top of the CDbCriteria reference page: http://www.yiiframework.com/doc/api/1.1/CDbCriteria

First process your array into a comma separated string which is used as conditional value in sql query

$idString = implode(', ', $hometeamid);

Then place that string inside IN clause in sql query.

$sql = "SELECT *
           FROM event
         WHERE id IN ($idString)"

$mod = Yii::app()->db->createCommand($sql)->queryAll();

after execution of queryAll method you will recieve the related rows as resultant array

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top