Yii Framework Forum: Difference For Find() And Findall() - Yii Framework Forum

Jump to content

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

Difference For Find() And Findall() Rate Topic: -----

#1 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 13 October 2012 - 09:06 AM

Is it true that if I have a table:

entry id | crop id | price
-------------------------------
1 | 3 | 90.00
2 | 3 |100.00

With this code: if $crop_id = 3
$price=Price::model()->find('crop_id = :cropId', array(
						':cropId' => $cropId
						) );

It only returns the field of the first row. 1 | 3 | 90.00

With this code:
$price=Price::model()->findAll('crop_id = :cropId', array(
						':cropId' => $cropId
						) );

It returns all the field of the crop_id = 3
thus returning 1 | 3 | 90.00 and 2 | 3 | 100.00

If it is true, how can I price[0] or price[1]?? Because it spouts an error.
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 13 October 2012 - 09:52 AM

Yes, it's true. Find returns one record, and findAll returns array of records.
So you can do $items = findAll(...) and then use $items[0] etc.

I have no idea why you're getting the error. Maybe the result was empty or null.
You can use print_r to see what has happened.
God is real unless declared as integer
0

#3 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 822
  • Joined: 02-July 10
  • Location:Central Poland

Posted 13 October 2012 - 12:20 PM

actually findAll can return associative array with key values equal to some field passed as 'index' in criteria object. If so - '0' and '1' can be invalid array keys. you should print_r($price) to check what exactly is returned or use safe iteration like:
foreach( $price as $p ) {}

red
0

#4 User is offline   Ujjwal Prajapati 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 47
  • Joined: 24-April 12

Posted 15 October 2012 - 12:51 AM

$price=Price::model()->findAll('crop_id = :cropId', array(
                                                ':cropId' => $cropId
                                                ) );


If you do this then, you can obtain result as such.

foreach($price as $object) {
echo $object -> entry_id;
}


if you need in an array with index 0, 1. you can push it in an array within foreach loop.

$entryID = array();
foreach($price as $object) {
array_push($entryID, $object->entry_id);
}


Now you have your entry id in an array with index 0, 1 ....

Try It!
0

#5 User is offline   AussieNeil 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 25-January 13

Posted 28 January 2013 - 04:17 AM

Hi,

Not that I have anything to add to this just would like to say thank you for giving this information as it helped me with a related problem.
0

#6 User is offline   developer! 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 28-November 12

Posted 28 January 2013 - 09:04 AM

try to use foreach to loop through..
foreach($product as $products)
        {
        }


//example would be
$product =Product::model()->findAll();
        $productsArray = array();
        foreach($product as $products)
        {
            $productsArray[] = array(
                'id' => $products->id,
                'name' => $products->name,

                'description' => $products->description,
             
            );
        }

you can loop through like this...

hope this helps...

Thanks,
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