Friday, June 28, 2013

Re: Model::AfterFind $results format, associations

Damn it a typo:

The "$primary = false" query (an intermediary hasMany query result) looks like this:

array(  	(int) 0 => array(  		'HasManyAssoc' => array(  			'id' => '1'  		)  	),
        ...  )

(The same as all other query result formats).

Please just look if in doubt - It's so much faster/easier.

AD

On Friday, 28 June 2013 12:59:57 UTC+2, AD7six wrote:


On Thursday, 27 June 2013 22:25:02 UTC+2, Vanja Dizdarević wrote:
As "solved" in the thread I linked to in the original post: 

         public function afterFind($results, $primary = false) { 
                if (method_exists($this, 'doAfterFind')) { 
                          if ($primary) { 
                                   foreach ($results as $key => $val) { 
                                            if (isset($val[$this->alias])) { 
                                                     $results[$key][$this->alias] = $this->doAfterFind($results[$key][$this->alias]); 
                                                } 
                                      } 
                          } else { 
                                  if (isset($results[$this->primaryKey])) { 
                                           $results = $this->doAfterFind($results); 
                                   } else { 
                                            foreach ($results as $key => $val) { 
                                                     if (isset($val[$this->alias])) { 
                                                              if (isset($val[$this->alias][$this->primaryKey])) { 
                                                                       $results[$key][$this->alias] = $this->doAfterFind($results[$key][$this->alias]); 
                                                              } else { 
                                                                       foreach ($results[$key][$this->alias] as $key2=> $val2) { 
                                                                                $results[$key][$this->alias][$key2] = $this->doAfterFind($results[$key][$this->alias][$key2]); 
                                                                              } 
                                                                    } 
                                                          } 
                                                } 
                                      } 
                           } 
                } 
                 return $results; 
         } 
}

This code looks to be way over complex, possibly a testament to the none-value of reading 5 year old posts on a google group for a framework which is actively maintained and applying found advice blindly.


This supposedly applies the same "doAfterFind" method to all records, regardless of the passed $results array format, taking into account all possible associations of a primary model to this model.

The actual questions is: What are all array formats we can expect in the $results array?

Does that mean you still haven't looked =)?

a count looks like this:

array(  	(int) 0 => array(  		(int) 0 => array(  			'count' => '9'  		)  	)  )

a find first looks like this:

array(  	(int) 0 => array(  		'Alias' => array(  			'id' => '1'  		),  		'HasManyAssoc' => array(  			(int) 0 => array(  				'id' => '1'  			)  		)  	)  )  

a find all looks like this:

array(
	(int) 0 => array(  		'Alias' => array(  			'id' => '1'  		),  		'HasManyAssoc' => array(  			(int) 0 => array(  				'id' => '1'  			)  		)  	),
        ...  )

The "$primary = false" query (an intermediary hasMany query result) looks like this:

array(  	(int) 0 => array(  		'HasManyAssoc' => array(  			(int) 0 => array(  				'id' => '1'  			)  		)  	),
        ...  )

There's a very simple pattern there:

array($int => array('alias' => array('field' => $value)));

With a simple exception for hasMany associations, when included in the primary query's results, all primary finds always have the same results format.


To quote the docs:
 If a model is queried as an association the format of$results can differ; instead of the result you would normally get from a find operation, you may get this:
$results = array(      'field_1' => 'value1',      'field_2' => 'value2'  );
 
So, if I wish the afterFind to find the data in the results array, I have to test the format of the array or can I rely on $primary value?

I .. think you should stop wasting your time and ask how to do something specifically, from the look of your afterFind method, you're writing some sort of appmodel method that is converting/processing all data. Right now it's going to run on all queries, loop on all keys and therefore probably affect performance even if it's doing what you want it to do. You're probably better of writing either a recursive method to operate on the whole results format or a much narrower scope method that just does what you specifically want.

What is your afterFind method trying to do?

AD

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: