I'm having the same issue, and I have yet to find a solution. The Cake documentation is wrong and/or the implementation is incorrect.
On Oct 23, 1:58 am, "martin.westin...@gmail.com" <martin.westin...@gmail.com> wrote: > I compiled a list of the variations I have encountered in different > associations. I have not checked how behaviors are called. > > When primary is set this is the structure I get just as in the > Cookbook: > array( > '0' => array( > 'Model' => array( > 'id' => 1 > ) > ) > ) > > When primary is not set I get a subset of these for each association: > > hasOne > array( > 'id' => 1 > ) > > habtm > array( > '0' => array( > 'id' => 1 > ) > ) > > hasOne, hasMany, belongsTo > array( > '0' => array( > 'Model' => array( > 'id' => 1 > ) > ) > ) > > habtm, hasMany > array( > '0' => array( > 'Model' => array( > '0' => array( > 'id' => 1 > ) > ) > ) > ) > > This makes the number of ifs and fors quite many in order the catch > them all. And since more than one is sometimes called for the same > record in the same request, you also have to check is you have already > manipulated your data. At least if you do something "destructive" to > it like encryption/decryption or serialization. > > My orignal question still stands. What is the best way to write an > afterFind in order to: 1. not miss converting data in some queries 2. > not double-convert the data ? > > regards, > /Martin > > On Oct 22, 5:16 pm, "martin.westin...@gmail.com" > > <martin.westin...@gmail.com> wrote: > > Hi, > > I thought I'd ask this here. (see why below) > > How do I write afterFind() to modify a field. > > > For example just something simple like this (just an example): > > > function afterFind($data) { > > foreach ($data as $key => $val) { > > if ( isset($val[$this->alias]['name']) ) { > > $data[$key][$this->alias]['name2'] = $val[$this->alias] > > ['name']; > > } > > } > > debug($data); > > return $data; > > > } > > > What I want to know is how to pick out the field from the passed data > > array. There are so many different ways the data is formatted that I > > end up with a quite messy series of for's and if's and I still don't > > fell 100% sure I got them all. I feel there must be some sure-fire way > > to write these. > > > The Cookbook is not complete compared to what I > > get.http://book.cakephp.org/view/681/afterFind > > > The API does not mention much about this. > > > I did not find any test in the core that helped me. > > > I did not find anything on Google that dealt with anything but basic > > "primary" data. > > > I noticed that sometimes afterFind() is called more than once with > > different data-structure each time. I asked about that > > here:http://groups.google.com/group/cake-php/browse_thread/thread/c83e5f40... > > > I'd love some clarification of this callback. Thans in advance. > > /Martin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---