I just tested with this code and checked that the callback is executed only 
once:

$table = TableRegistry::get('Articles');
$table
->find()
->formatResults(function($r) {
return $r->map(function($r) {
debug('a');
return $r;
});
})
->first();

On Wednesday, April 16, 2014 2:33:35 PM UTC+2, José Lorenzo wrote:
>
> Paste the full code where you see it being called twice, please. I will 
> also check with my own code
>
> On Wednesday, April 16, 2014 2:23:54 PM UTC+2, bato wrote:
>>
>> ok it works!
>>
>> To me remain a mistery why using first() the callback in $results->map() 
>> is called twice while not using first is called once.
>>
>> Could it be a bug?
>>
>>
>> 2014-04-16 13:11 GMT+02:00 José Lorenzo <[email protected]>:
>>
>>> Try this
>>>
>>> public findFlat($query, $options = []) {
>>> return $query->formatResults(function($results, $query) {
>>>  return $results->map(function($row) {
>>> if ($row->content) {
>>> $row->set($row->content->toArray(), ['guard' => false]);
>>>  $row->unsetProperty('content');
>>> }
>>> return $row;
>>>  });
>>> });
>>> }
>>>
>>> $row = $table->find('flat')->first();
>>>
>>>
>>> On Tuesday, April 15, 2014 10:04:25 AM UTC+2, bato wrote:
>>>>
>>>> uhm...
>>>>
>>>> maybe I'm wrong to use map() function... 
>>>>
>>>> return $results->map(function($row) {
>>>>                 foreach ($row['content']->toArray() as $key => $value) {
>>>>                     $row[$key] = $value;
>>>>                 }
>>>>                 unset($row['content']);
>>>>                 return $row;
>>>>             });
>>>>
>>>>
>>>> In my db I have only one row. 
>>>> When I find all results the callback inside map() is called one time. 
>>>> If I add first() to chain then it's called two times and the second 
>>>> time $row['content'] is undefined because I unset it before so thrown 
>>>> fatal 
>>>> error. Why is it called two times?
>>>>
>>>> Suggestions?
>>>>
>>>>
>>>> 2014-04-15 9:22 GMT+02:00 bato <[email protected]>:
>>>>
>>>>> Sure,
>>>>>
>>>>> I'm trying to add 'content' properties to main Entity properties (it's 
>>>>> an exercise to collpase entities properties in an array)
>>>>>
>>>>> [
>>>>> (int) 0 => object(App\Model\Entity\Object) {
>>>>>  
>>>>> 'new' => false,
>>>>> 'accessible' => [
>>>>>  '*' => true
>>>>> ],
>>>>>  'properties' => [
>>>>> 'id' => (int) 5,
>>>>>  'object_type_id' => (int) 12,
>>>>> 'status' => 'on',
>>>>>  'created' => object(DateTime) {
>>>>> date => '2014-03-07 09:21:29'
>>>>>  timezone_type => (int) 3
>>>>> timezone => 'Europe/Rome'
>>>>>  },
>>>>> 'modified' => object(DateTime) {
>>>>>  date => '2014-04-07 23:32:48'
>>>>> timezone_type => (int) 3
>>>>>  timezone => 'Europe/Rome'
>>>>> },
>>>>>  'start_date' => null,
>>>>> 'end_date' => null,
>>>>>  'subject' => null,
>>>>> 'abstract' => null,
>>>>>  'body' => null,
>>>>> 'duration' => null
>>>>>  ],
>>>>> 'dirty' => [
>>>>>  'id' => true,
>>>>> 'start_date' => true,
>>>>>  'end_date' => true,
>>>>> 'subject' => true,
>>>>>  'abstract' => true,
>>>>> 'body' => true,
>>>>>  'duration' => true
>>>>> ],
>>>>>  'virtual' => [],
>>>>> 'errors' => [],
>>>>>  'repository' => 'ImageObjects'
>>>>>  }
>>>>> ]
>>>>>
>>>>> This is the results of find all. In 'dirty' key you can see the 
>>>>> 'content' properties that I add to App\Model\Entity\Object properties.
>>>>> I unset the 'content' key so it isn't show in the above array.
>>>>>
>>>>> I'm playing with ORM feature and I would want to know if formatResults 
>>>>> is usable in query chain.
>>>>>
>>>>>
>>>>> Il giorno lunedì 14 aprile 2014 11:40:53 UTC+2, José Lorenzo ha 
>>>>> scritto:
>>>>>>
>>>>>> Can you explain what you are trying to do? I'm not sure I get it from 
>>>>>> your code example.
>>>>>>
>>>>>> On Monday, April 14, 2014 11:31:42 AM UTC+2, bato wrote:
>>>>>>>
>>>>>>> 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 a topic in the 
>>>>> Google Groups "CakePHP" group.
>>>>> To unsubscribe from this topic, visit https://groups.google.com/d/
>>>>> topic/cake-php/djk1Ip6_Ca4/unsubscribe.
>>>>> To unsubscribe from this group and all its topics, send an email to 
>>>>> [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> Visit this group at http://groups.google.com/group/cake-php.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>
>>>>
>>>> -- 
>>>> ------ bato ------- 
>>>>
>>>  -- 
>>> 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 a topic in the 
>>> Google Groups "CakePHP" group.
>>> To unsubscribe from this topic, visit 
>>> https://groups.google.com/d/topic/cake-php/djk1Ip6_Ca4/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to 
>>> [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/cake-php.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> -- 
>> ------ bato ------- 
>>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Reply via email to