Thursday, June 27, 2013

Re: Model::AfterFind $results format, associations

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 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?

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?

This is so confusing.

--
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: