Monday, August 25, 2014

Re: cake3 - using formatResult() when entity was hydrated or not

Yeah! It works :)

Il giorno venerdì 22 agosto 2014 09:50:08 UTC+2, José Lorenzo ha scritto:
You can use $row['field'], entities implement array access

On Wednesday, August 20, 2014 1:56:56 PM UTC+2, bato wrote:
Hi all,

I have a behavior that define a Query::formatResults() method in beforeFind() event. In this way I simulate that I did in old cake afterFind. 

public function beforeFind(Event $event, Query $query, array $options, $primary) {
    
    $query->formatResults(function($results) {
        return $results->map(function($row) {
            if (is_array($row)) {
                $row['field_name'] = 'field_value';
                // do some other stuff on array $row
            } else {
                $row->set('field_name', 'field_value');
                // do some other stuff on Entity $row
            }
            return $row;
        });
    });
  
}

In map() method I have to distinguish if $row is array or object (Entity) because $row could be an Entity or an array if Entity was not hydrated.
Is there a better way to do it?

Alberto

--
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/d/optout.

No comments: