0

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.

Salini L
  • 829
  • 5
  • 15
  • 43
  • Well, your question is not clear. Could you please explain what exactly do you want to do? – Ali MasudianPour Jun 05 '14 at 12:42
  • SELECT * FROM event WHERE id = 1432201 or id = 1432208' I want to execute this select query its getting executed but the data is showing ad blank rows in grid. there are two data maching the above query. – Salini L Jun 05 '14 at 12:45
  • Well, are you sure that you have `1432201` or `1432208` id in your `event` table? You can check the result by getting a dump from it: `CVarDumper::dump($mod)` – Ali MasudianPour Jun 05 '14 at 12:47
  • @Salini will you post your table schema structure – gvgvgvijayan Jun 05 '14 at 12:49
  • have you passed the array to view using $this->render('viewname',array('mod'=>$mod)); in controller – gvgvgvijayan Jun 05 '14 at 12:52
  • @gvgvgvijayan yes passed. i have converted model to dataprovider by using the code $dataProvider=new CArrayDataProvider($mod); is any thng wrng with that? – Salini L Jun 05 '14 at 12:55
  • you just pass sql query without condition in order to check whether it is passed to view before that vardump the received data from `$mod` in controller itself – gvgvgvijayan Jun 05 '14 at 12:57
  • @AliMasudianPour yes its displaying data with CVarDumper::dump($mod) code but not displaying in grid and foreach($mod as ..) – Salini L Jun 05 '14 at 12:59
  • yes i have tried with out condition but its also blank – Salini L Jun 05 '14 at 13:00

4 Answers4

0

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

gvgvgvijayan
  • 1,851
  • 18
  • 34
  • its also displaying blank rows in CGridView – Salini L Jun 05 '14 at 12:50
  • 4
    REVIEW of low quality post: Please format your code better. Write some explanation of proposed solution as well. In the current form this answer is likely to be deleted for moderation reasons. I'm not voting for deletion only because it seems to be a good path to solve the problem. Someone else might not be so generous. – ElmoVanKielmo Jun 05 '14 at 13:12
0

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

example:

$mod = Event::model()->findbyPk($no);
Burhan Çetin
  • 676
  • 7
  • 16
0

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

Stu
  • 4,160
  • 24
  • 43
0

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();
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103