How to work with findAll();

Hi,

I am trying to work with model()->findAll(); and running into some issues I don’t understand.


$testQuery = TestModel::model()->findAll();

This would retrieve all data and store it in the array called $testQuery, right?

I can see that there is A LOT in it if I dump it with:


echo "<pre>";

print_r ($testQuery);

echo "</pre>";

If I want to work with this array now, what would be the best practice to do so?

Let’s say I would like to access one field called TestField which ID would be 1, I could do the following:


echo $testQuery[0]->FieldText;

Is there an easier way to achieve this? Using a find method which retrieves only one row is not an option ;)

Not sure exactly what you are looking for here but $testQuery would be an array so you can do any type of array functions that you want. You might narrow down the number of records that the findAll returns by using CDbCriteria.

You can loop through the array and do whatever you want. For example, to add an asterisk ‘*’ at the end of each of the FieldText fields of the records:


foreach ($testQuery as $tq )

{

	$tq->FieldText .= '*';

}



That’s what I ended up doing now :)

Thank you!!

CActiveRecord.findAll() returns an array of active record models, and each active record model is rather large. If you want to see the database fields only, you can use CActiveRecord.getAttributes().


$testQuery = TestModel::model()->findAll();

foreach ($testQuery as $record) {

   echo "<pre>";print_r($record->getAttributes());echo"</pre>";

}

Also, you can index the records as needed. (the following code has not been tested)


$criteria = new CDbCriteria();

$criteria->index = "id";

$testQuery = TestModel::model()->findAll($criteria);

echo $testQuery[1]->FieldText;

I just want to say THANK YOUUUUUUU. after an entire day of hairpulling trying to understand the yii documentation on these things your example here made my pollet drop down. You have saved the day…(oh atleast the night;)). I mean it’s easy now when I understand it but creating it the first time was hard.

i just used these keywords

foreach ($model as $record) {

echo "<pre>";print_r($record->getAttributes());echo"</pre>";

}

but gets something like this

Array

(

[id] =&gt; 1


[subject] =&gt; aa vathili


[content] =&gt; Create BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate BloggerCreate Blogger

)

Array

(

[id] =&gt; 2


[subject] =&gt; dfsdf


[content] =&gt; sdfsfsf

)

actually i want to work with single rows like this row[‘id’] so what i want to do…i m first with these types of frameworks…thanku in advance

hey shafi

copy pasting does not work always you have understand what the code is for the code below is see you table schema in other words see what fields you have in that particular table.




foreach ($model as $record) {

  echo $recrod->fieldName; // change the fieldName to your field you wanna access

}

lhearin

thanks a lot…I was facing a similar kind of problem…your solution helped me out.

thanks :)

try

foreach($testQuery as $test){

echo $test->myData1;

}

it will iterate through your array ! :D