Re: afterFind() inconsistencies ?

2009-09-17 Thread iFeghali

I will totally ignore the value of primary and try the "guess" the
format of the results array by its contents.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: afterFind() inconsistencies ?

2009-09-17 Thread iFeghali

Ok more feedback on this in the same scenario described above, for
the same Machine->afterFind() defined as:



===
/services/view/ABC:
===
true
Array
(
[0] => Array
(
[id] =>
[category_id] =>
etc.
[MachinesService] => Array
(
)
[Type] => Array
(
)
[MachineContact] => Array
(
)
)

===
/machines/view/RST:
===
true
Array
(
[0] => Array
(
[Machine] => Array
(
[id] =>
[category_id] =>
etc.
)
[Type] => Array
(
)
[MachineContact] => Array
(
)
[Service] => Array
(
)
)
)

===
/types/view/XYZ:
===
false
Array
(
[0] => Array
(
[Machine] => Array
(
[id] =>
[category_id] =>
etc.
)
)
)

Note that primary is "true" for cases 1 and 2 and "false" for case 3.
Still, output for cases 2 and 3 are equivalent, and different from the
output of case 1. To make things even more complicated, according to
the manual [1] the value of $primary of cases 1,2,3 should be false,
true, false but they are in fact true, true, false and yet their
output are as they were false, true, true. confusing isn't it ? O.o

[1] http://book.cakephp.org/view/681/afterFind


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



Re: afterFind() inconsistencies ?

2009-09-17 Thread iFeghali

Hello vb13,

> Cake processes hasMany and hasOne, belongsTo differently. In the case
> of hasOne and belongsTo, when, say, 2 models are involved, they
> generate 1 query with a join, and then retrieve data in one shot.
>
> In the case  when model A hasMany Model B (for example Machine and
> MachineContact) it looks like cake will first retrieve a query for
> Machine data and then for each machine there will be one query to
> retrieve MachineContract.
>
> So there are 2 logical branches and each callsafterFindslightly
> different.

Thank you for sharing your research. In fact, I do understand your
point. What is not making sense to me is that afterFind() is being
called many times for a single read(). Anyway, even though cake
processes things differently (for different relationships) the
resulting array should be equal for all the cases, unless a flag is
given just like how it happens with $primary.

So we have here either a bug or a "work as expected" situation that is
not clear to me.

I am back to this question again, will keep in touch if i do any
progress.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: afterFind() inconsistencies ?

2009-09-17 Thread iFeghali

Hello vb13,

Thank you for the point but I am already aware of $primary. It does
not help me with this issue though, as for all the cases described
here $primary was set to the default value.

Best,
~IF.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: afterFind() inconsistencies ?

2009-09-16 Thread brian

On Wed, Sep 16, 2009 at 8:49 PM, Miles J  wrote:
>
> I believe $primary is true when an array is multidimensional (find
> with related tables), and false when it is not (simple find()).

I thought $primary was true for the main model's afterFind() and false
for that of any associated models.

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



Re: afterFind() inconsistencies ?

2009-09-16 Thread Miles J

I believe $primary is true when an array is multidimensional (find
with related tables), and false when it is not (simple find()).
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: afterFind() inconsistencies ?

2009-09-16 Thread vb13

My colleague just pointed out to me that afterFind() passes another
parameter $primary that is supposed to help (see 
http://book.cakephp.org/view/681/afterFind).
However I see it unitialized when it is supposed to be true.

On Sep 16, 5:23 pm, vb13  wrote:
>  iFeghali,
>
> I have a somewhat similar problem. I have spent today an hour or two
> looking into how Cake handles db queries for hasMany vs. hasOne or
> belongsTo relationships. Hopefully this will help you to understand
> why are you seeing what you are seeing.
>
> Cake processes hasMany and hasOne, belongsTo differently. In the case
> of hasOne and belongsTo, when, say, 2 models are involved, they
> generate 1 query with a join, and then retrieve data in one shot.
>
> In the case  when model A hasMany Model B (for example Machine and
> MachineContact) it looks like cake will first retrieve a query for
> Machine data and then for each machine there will be one query to
> retrieve MachineContract.
>
> So there are 2 logical branches and each callsafterFindslightly
> different.
>
> In the case of hasOne I see a call in the __filterResults() function
> around line 716 of db_source.php like such ( I am using Cake 1.2.5):
>
>                                                 if 
> (isset($model->{$className}) && is_object($model->
> {$className})) {
>                                                         $data = 
> $model->{$className}->afterFind(array(array($className
> => $results[$i][$className])), false);
>                                                 }
>
> This will pass yourafterFindmethod a structure $result[0]
> ['MachineContact'][..]
>
> In the case of hasManyafterFind() is called in the queryAssociation()
> function line 877 as follows:
>
>                                         if 
> (isset($resultSet[$i][$association])) {
>                                                 $resultSet[$i][$association] 
> = $linkModel->afterFind($resultSet
> [$i][$association]);
>                                         }
> and this passes $result[...] date toafterFind()
>
> Is this a defect? I can not tell without deeper understanding of why
> it was coded this way.
>
> On Sep 2, 3:56 pm, iFeghali  wrote:
>
> > anyone got a clue ?
>
> > I am still very confused on this matter. I have tried moving from
> > "recursive" to "containable" with no luck.
>
> > Thank you.
>
> > On Jul 28, 5:31 pm, iFeghali  wrote:
>
> > > Hello All,
>
> > > I found myself stuck for a week in an issue that I would really
> > > appreciate any kind of help. Thats my scenario:
>
> > > Machine HABTM Service
> > > Machine belongsTo Type
> > > Type hasOne Machine
> > > Machine hasMany MachineContact
> > > MachineContact belongsTo Machine
>
> > > Now I have Machine->afterFind() hypothetically defined as:
>
> > >  > > functionafterFind($results, $primary = false) {
> > > print_r($results);
> > > return $results;}
>
> > > ?>
>
> > > Recursive is set to 2 for everything. So far so good.
>
> > > If I go to /services/view/someId I get:
>
> > > Array
> > > (
> > >     [0] => Array
> > >         (
> > >             machine attribs ...
> > >             [MachinesService] => Array
> > >                 (
> > >                   ...
> > >                 )
> > >             [Type] => Array
> > >                 (
> > >                   ...
> > >                 )
> > >             [MachineContact] => Array
> > >                 (
> > >                     [0] => Array
> > >                         (
> > >                           ...
> > >                         )
>
> > >                     [1] => Array
> > >                         (
> > >                           ...
> > >                         )
> > >                 )
> > >         )
> > > )
> > > Array
> > > (
> > >     [0] => Array
> > >         (
> > >             [Machine] => the Array above
> > >        )
> > > )
>
> > > First question, I do not understand whyafterFind() was called twice.
> > > In the SQL log there is only one query searching the machines table.
> > > As I browsed cake's source I found thatafterFind() in called in the
> > > model layer and after that again at the DB layer. Did I get it right ?
> > > If so, why is the reason for that ?
>
> > > But the real issue comes when I go to /types/view/someId:
>
> > > Array
> > > (
> > >     [0] => Array
> > >         (
> > >             [Machine] => Array
> > >                 (
> > >                  ...
> > >                 )
> > >         )
> > > )
> > > Array
> > > (
> > >     [0] => Array
> > >         (
> > >             [Machine] => Array
> > >                 (
> > >                     [0] => Array
> > >                         (
> > >                            machine attribs...
> > >                             [Type] => Array
> > >                                 (
> > >                                  ...
> > >                                 )
> > >                             [MachineContact] => Array
> > >                                 (
> > >           

Re: afterFind() inconsistencies ?

2009-09-16 Thread vb13

 iFeghali,


I have a somewhat similar problem. I have spent today an hour or two
looking into how Cake handles db queries for hasMany vs. hasOne or
belongsTo relationships. Hopefully this will help you to understand
why are you seeing what you are seeing.

Cake processes hasMany and hasOne, belongsTo differently. In the case
of hasOne and belongsTo, when, say, 2 models are involved, they
generate 1 query with a join, and then retrieve data in one shot.

In the case  when model A hasMany Model B (for example Machine and
MachineContact) it looks like cake will first retrieve a query for
Machine data and then for each machine there will be one query to
retrieve MachineContract.

So there are 2 logical branches and each calls afterFind slightly
different.

In the case of hasOne I see a call in the __filterResults() function
around line 716 of db_source.php like such ( I am using Cake 1.2.5):

if (isset($model->{$className}) 
&& is_object($model->
{$className})) {
$data = 
$model->{$className}->afterFind(array(array($className
=> $results[$i][$className])), false);
}

This will pass your afterFind method a structure $result[0]
['MachineContact'][..]


In the case of hasMany afterFind() is called in the queryAssociation()
function line 877 as follows:

if 
(isset($resultSet[$i][$association])) {
$resultSet[$i][$association] = 
$linkModel->afterFind($resultSet
[$i][$association]);
}
and this passes $result[...] date to afterFind()


Is this a defect? I can not tell without deeper understanding of why
it was coded this way.




On Sep 2, 3:56 pm, iFeghali  wrote:
> anyone got a clue ?
>
> I am still very confused on this matter. I have tried moving from
> "recursive" to "containable" with no luck.
>
> Thank you.
>
> On Jul 28, 5:31 pm, iFeghali  wrote:
>
> > Hello All,
>
> > I found myself stuck for a week in an issue that I would really
> > appreciate any kind of help. Thats my scenario:
>
> > Machine HABTM Service
> > Machine belongsTo Type
> > Type hasOne Machine
> > Machine hasMany MachineContact
> > MachineContact belongsTo Machine
>
> > Now I have Machine->afterFind() hypothetically defined as:
>
> >  > functionafterFind($results, $primary = false) {
> > print_r($results);
> > return $results;}
>
> > ?>
>
> > Recursive is set to 2 for everything. So far so good.
>
> > If I go to /services/view/someId I get:
>
> > Array
> > (
> >     [0] => Array
> >         (
> >             machine attribs ...
> >             [MachinesService] => Array
> >                 (
> >                   ...
> >                 )
> >             [Type] => Array
> >                 (
> >                   ...
> >                 )
> >             [MachineContact] => Array
> >                 (
> >                     [0] => Array
> >                         (
> >                           ...
> >                         )
>
> >                     [1] => Array
> >                         (
> >                           ...
> >                         )
> >                 )
> >         )
> > )
> > Array
> > (
> >     [0] => Array
> >         (
> >             [Machine] => the Array above
> >        )
> > )
>
> > First question, I do not understand whyafterFind() was called twice.
> > In the SQL log there is only one query searching the machines table.
> > As I browsed cake's source I found thatafterFind() in called in the
> > model layer and after that again at the DB layer. Did I get it right ?
> > If so, why is the reason for that ?
>
> > But the real issue comes when I go to /types/view/someId:
>
> > Array
> > (
> >     [0] => Array
> >         (
> >             [Machine] => Array
> >                 (
> >                  ...
> >                 )
> >         )
> > )
> > Array
> > (
> >     [0] => Array
> >         (
> >             [Machine] => Array
> >                 (
> >                     [0] => Array
> >                         (
> >                            machine attribs...
> >                             [Type] => Array
> >                                 (
> >                                  ...
> >                                 )
> >                             [MachineContact] => Array
> >                                 (
> >                                     [0] => Array
> >                                         (
> >                                          ...
> >                                         )
> >                                 )
> >                             [Service] => Array
> >                                 (
> >                                     [0] => Array
> >                                         (
> >                                           ...
> >                                         )
> >

Re: afterFind() inconsistencies ?

2009-09-02 Thread iFeghali
anyone got a clue ?

I am still very confused on this matter. I have tried moving from
"recursive" to "containable" with no luck.

Thank you.

On Jul 28, 5:31 pm, iFeghali  wrote:
> Hello All,
>
> I found myself stuck for a week in an issue that I would really
> appreciate any kind of help. Thats my scenario:
>
> Machine HABTM Service
> Machine belongsTo Type
> Type hasOne Machine
> Machine hasMany MachineContact
> MachineContact belongsTo Machine
>
> Now I have Machine->afterFind() hypothetically defined as:
>
>  function afterFind($results, $primary = false) {
> print_r($results);
> return $results;}
>
> ?>
>
> Recursive is set to 2 for everything. So far so good.
>
> If I go to /services/view/someId I get:
>
> Array
> (
>     [0] => Array
>         (
>             machine attribs ...
>             [MachinesService] => Array
>                 (
>                   ...
>                 )
>             [Type] => Array
>                 (
>                   ...
>                 )
>             [MachineContact] => Array
>                 (
>                     [0] => Array
>                         (
>                           ...
>                         )
>
>                     [1] => Array
>                         (
>                           ...
>                         )
>                 )
>         )
> )
> Array
> (
>     [0] => Array
>         (
>             [Machine] => the Array above
>        )
> )
>
> First question, I do not understand why afterFind() was called twice.
> In the SQL log there is only one query searching the machines table.
> As I browsed cake's source I found that afterFind() in called in the
> model layer and after that again at the DB layer. Did I get it right ?
> If so, why is the reason for that ?
>
> But the real issue comes when I go to /types/view/someId:
>
> Array
> (
>     [0] => Array
>         (
>             [Machine] => Array
>                 (
>                  ...
>                 )
>         )
> )
> Array
> (
>     [0] => Array
>         (
>             [Machine] => Array
>                 (
>                     [0] => Array
>                         (
>                            machine attribs...
>                             [Type] => Array
>                                 (
>                                  ...
>                                 )
>                             [MachineContact] => Array
>                                 (
>                                     [0] => Array
>                                         (
>                                          ...
>                                         )
>                                 )
>                             [Service] => Array
>                                 (
>                                     [0] => Array
>                                         (
>                                           ...
>                                         )
>                                 )
>                         )
>                 )
>         )
> )
>
> afterFind() is called twice again as [un]expected. The problem is,
> this time, in the first run it didn't fetch any of the Machines
> associated models, even though recursive is set to 2. Also, the array
> is indexed by model name what doesn't happens in the first test.
> Secondly, in the second run the machine array comes complete with all
> the attributes plus the associated models, indexed by model name
> again.
>
> So, what did I missed here ?
>
> Thank you.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: afterFind() inconsistencies ?

2009-07-31 Thread iFeghali

On 30 jul, 13:07, "Dr. Loboto"  wrote:
> Post your controller code. afterFind will be called as much times as
> much you call find.

function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Type.', true));
$this->redirect(array('action'=>'index'));
}

$this->Type->recursive = 2;
$this->set('type', $this->Type->read(null, $id));
}
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: afterFind() inconsistencies ?

2009-07-30 Thread Dr. Loboto

Post your controller code. afterFind will be called as much times as
much you call find.

On Jul 29, 9:38 pm, iFeghali  wrote:
> anyone ?
>
> On 28 jul, 17:31, iFeghali  wrote:
>
>
>
> > Hello All,
>
> > I found myself stuck for a week in an issue that I would really
> > appreciate any kind of help. Thats my scenario:
>
> > Machine HABTM Service
> > Machine belongsTo Type
> > Type hasOne Machine
> > Machine hasMany MachineContact
> > MachineContact belongsTo Machine
>
> > Now I have Machine->afterFind() hypothetically defined as:
>
> >  > function afterFind($results, $primary = false) {
> > print_r($results);
> > return $results;}
>
> > ?>
>
> > Recursive is set to 2 for everything. So far so good.
>
> > If I go to /services/view/someId I get:
>
> > Array
> > (
> >     [0] => Array
> >         (
> >             machine attribs ...
> >             [MachinesService] => Array
> >                 (
> >                   ...
> >                 )
> >             [Type] => Array
> >                 (
> >                   ...
> >                 )
> >             [MachineContact] => Array
> >                 (
> >                     [0] => Array
> >                         (
> >                           ...
> >                         )
>
> >                     [1] => Array
> >                         (
> >                           ...
> >                         )
> >                 )
> >         )
> > )
> > Array
> > (
> >     [0] => Array
> >         (
> >             [Machine] => the Array above
> >        )
> > )
>
> > First question, I do not understand why afterFind() was called twice.
> > In the SQL log there is only one query searching the machines table.
> > As I browsed cake's source I found that afterFind() in called in the
> > model layer and after that again at the DB layer. Did I get it right ?
> > If so, why is the reason for that ?
>
> > But the real issue comes when I go to /types/view/someId:
>
> > Array
> > (
> >     [0] => Array
> >         (
> >             [Machine] => Array
> >                 (
> >                  ...
> >                 )
> >         )
> > )
> > Array
> > (
> >     [0] => Array
> >         (
> >             [Machine] => Array
> >                 (
> >                     [0] => Array
> >                         (
> >                            machine attribs...
> >                             [Type] => Array
> >                                 (
> >                                  ...
> >                                 )
> >                             [MachineContact] => Array
> >                                 (
> >                                     [0] => Array
> >                                         (
> >                                          ...
> >                                         )
> >                                 )
> >                             [Service] => Array
> >                                 (
> >                                     [0] => Array
> >                                         (
> >                                           ...
> >                                         )
> >                                 )
> >                         )
> >                 )
> >         )
> > )
>
> > afterFind() is called twice again as [un]expected. The problem is,
> > this time, in the first run it didn't fetch any of the Machines
> > associated models, even though recursive is set to 2. Also, the array
> > is indexed by model name what doesn't happens in the first test.
> > Secondly, in the second run the machine array comes complete with all
> > the attributes plus the associated models, indexed by model name
> > again.
>
> > So, what did I missed here ?
>
> > Thank you.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: afterFind() inconsistencies ?

2009-07-29 Thread iFeghali
anyone ?

On 28 jul, 17:31, iFeghali  wrote:
> Hello All,
>
> I found myself stuck for a week in an issue that I would really
> appreciate any kind of help. Thats my scenario:
>
> Machine HABTM Service
> Machine belongsTo Type
> Type hasOne Machine
> Machine hasMany MachineContact
> MachineContact belongsTo Machine
>
> Now I have Machine->afterFind() hypothetically defined as:
>
>  function afterFind($results, $primary = false) {
> print_r($results);
> return $results;}
>
> ?>
>
> Recursive is set to 2 for everything. So far so good.
>
> If I go to /services/view/someId I get:
>
> Array
> (
>     [0] => Array
>         (
>             machine attribs ...
>             [MachinesService] => Array
>                 (
>                   ...
>                 )
>             [Type] => Array
>                 (
>                   ...
>                 )
>             [MachineContact] => Array
>                 (
>                     [0] => Array
>                         (
>                           ...
>                         )
>
>                     [1] => Array
>                         (
>                           ...
>                         )
>                 )
>         )
> )
> Array
> (
>     [0] => Array
>         (
>             [Machine] => the Array above
>        )
> )
>
> First question, I do not understand why afterFind() was called twice.
> In the SQL log there is only one query searching the machines table.
> As I browsed cake's source I found that afterFind() in called in the
> model layer and after that again at the DB layer. Did I get it right ?
> If so, why is the reason for that ?
>
> But the real issue comes when I go to /types/view/someId:
>
> Array
> (
>     [0] => Array
>         (
>             [Machine] => Array
>                 (
>                  ...
>                 )
>         )
> )
> Array
> (
>     [0] => Array
>         (
>             [Machine] => Array
>                 (
>                     [0] => Array
>                         (
>                            machine attribs...
>                             [Type] => Array
>                                 (
>                                  ...
>                                 )
>                             [MachineContact] => Array
>                                 (
>                                     [0] => Array
>                                         (
>                                          ...
>                                         )
>                                 )
>                             [Service] => Array
>                                 (
>                                     [0] => Array
>                                         (
>                                           ...
>                                         )
>                                 )
>                         )
>                 )
>         )
> )
>
> afterFind() is called twice again as [un]expected. The problem is,
> this time, in the first run it didn't fetch any of the Machines
> associated models, even though recursive is set to 2. Also, the array
> is indexed by model name what doesn't happens in the first test.
> Secondly, in the second run the machine array comes complete with all
> the attributes plus the associated models, indexed by model name
> again.
>
> So, what did I missed here ?
>
> Thank you.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: afterFind() inconsistencies ?

2009-07-28 Thread iFeghali

sorry, Type *hasMany* Machine.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---