Yii Framework Forum: How to work with findAll(); - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to work with findAll(); Rate Topic: -----

#1 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 04 January 2012 - 12:48 PM

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

#2 User is offline   lhearin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 20-December 11

Posted 04 January 2012 - 02:58 PM

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 .= '*';
}

1

#3 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 04 January 2012 - 04:19 PM

That's what I ended up doing now :)

Thank you!!
0

#4 User is offline   jellysandwich 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 03-May 11

Posted 04 January 2012 - 05:57 PM

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;

0

#5 User is offline   Sampa 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 18-December 11
  • Location:Stockholm,Sweden

Posted 07 January 2012 - 06:23 PM

View Postjellysandwich, on 04 January 2012 - 05:57 PM, said:

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.
0

#6 User is offline   shafi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 03-August 12
  • Location:cochin keral india

Posted 10 August 2012 - 07:47 AM

i just used these keywords

foreach ($model as $record) {
echo "<pre>";print_r($record->getAttributes());echo"</pre>";
}
but gets something like this
Array
(
[id] => 1
[subject] => aa vathili
[content] => 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] => 2
[subject] => dfsdf
[content] => 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
0

#7 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 677
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 10 August 2012 - 08:57 AM

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
}

0

#8 User is offline   Vaibhav Shetti 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 28-January 13

  Posted 15 May 2013 - 12:38 AM

lhearin

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

thanks :)
0

#9 User is offline   sefburhan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 136
  • Joined: 08-February 12
  • Location:islamabad

Posted 17 May 2013 - 01:56 AM

View Postjrn, on 04 January 2012 - 12:48 PM, said:

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


try
foreach($testQuery as $test){
echo $test->myData1;

}
it will iterate through your array ! :D
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users