Monday, April 14, 2014

cake 3 - is it possible to use formatResults to stacking multiple operations like mapper/reducer?

Hi,

I'm trying to figure out if it's possible to use formatResults in custom finder methods to use ORM chain. I have seen in docs the example with mapper reducer.

In my table object i have

public findFlat($query, $options = []) {
       return $query->formatResults(function($results, $query) {
            return $results->map(function($row) {
                foreach ($row['content']->toArray() as $key => $value) {
                    $row[$key] = $value;
                }
                unset($row['content']);
                return $row;
            });
        });
}

then in my controller I have

$images = TableRegistry::get('ImageObjects');
$result = $images->find('flat');
$result->contain(['Contents', 'Streams']);
debug($result->toArray());

In this way it works but if I add first()

$result->first();

then I obtain a fatal error in findFlat() method

Error: Call to a member function toArray() on a non-object

Any help? 
Am I using wrong formatResults()? Is it possible to chain only mapper reducer?

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