On Mon, Oct 17, 2011 at 6:44 AM, WebbedIT <p...@webbedit.co.uk> wrote:
> You agree that the primary key of a table for which you are then
> fetching related hasMany records is required as part of Cakes
> automagic on hasMany associations.  It would then take more processing
> (albeit, a negligable amount) to then unset that primary key.

Well, I agree with you about what you said, but my question is about
not showing the foreign key of an associated Model
(Post->hasMany->Comment): such field is useful while _joining_ tables,
but once you have the data I don't get why it is still necessary.

>
> But the question I ask myself is, what problem does it cause you by
> existing in the results array?
>
> You are not forced to then use it in your view so just ignore it!

I know I can post-process returned data, but avoiding the db to return
useless data seems to me a more elegant and efficient solution;
moreover, given that the docs does not put any constraints on the
hasmany->fields variable, I'm curious to understand why that foreign
key is still necessary.

I don't know CakePHP internals but I imagine a query like this is used
to fetch associated data:

SELECT $fields specified in association fields
FROM comments
WHERE comments.post_id = $post_id

If this is true, imho there is no need to keep the foreign key there
(this is not true for the comment id (primary key), which must be
there not to break CakePHP Model engine.

Is there anything I'm not taking into account? If so, please correct me.


Cheers,
Matteo
>
> HTH, Paul
>
> On Oct 16, 11:42 am, Matteo Landi <landima...@gmail.com> wrote:
>> On Tue, Oct 11, 2011 at 6:23 PM, Matteo Landi <landima...@gmail.com> wrote:
>> > On Tue, Oct 11, 2011 at 6:15 AM, Angad Nadkarni
>> > <angad.nadka...@gmail.com> wrote:
>> >> Hi Matteo, this is not a bug. Cake requires the post_id field  to
>> >> fetch the associated Post associated array
>> >> as well, hence even when you don't mention it, the "PRIMARY" and
>> >> "FOREIGN" key fields are always mentioned.
>>
>> > I agree with you when you saying that to fetch all the comments
>> > associated to a given post the query engine need to work with
>> > ``post_id`` field, but what I quite don't understand is why such field
>> > is not removed after such comments are fethced; is the $field
>> > information used only before of the query? If so, are there any
>> > particular reasons behind that choice?
>>
>> The more I think of this, the less I understand why that field cannot
>> be removed by the engine itself.
>>
>> However, what is the more elegant way to remove those fields? Maybe
>> using ``finderQuery`` model field?
>>
>> Regards,
>> Matteo
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> > Regards,
>> > Matteo
>>
>> >> On Oct 10, 3:26 am, Matteo Landi <landima...@gmail.com> wrote:
>> >>> Hi everybody,
>> >>> I'm trying to create a simple cakephp application which handles posts
>> >>> and comments (like the one presented in the tutorial); I have a Post
>> >>> model which *hasMany* Comments, and I have a Comment which *belongsTo*
>> >>> to a Post.
>>
>> >>> I would like to use Post->hasMany['fields'] variable to reduce the set
>> >>> of fetched attributes, but for some strange reasons, I'm getting also
>> >>> attributes not present in the specified array; in particular, I get
>> >>> all the specified attributes plus the foreignKey (e.g. if my Comment
>> >>> is composed by an 'id', a 'body', a couple of date fields and
>> >>> 'post_id' (our foreignKey), and if I set *fields* to array('id'), I
>> >>> get back both 'id' and 'post_id' attributes).
>>
>> >>> However, while I fetch the Post associated to a Comment, the list of
>> >>> fetched attributes seems to match the one specified in the association
>> >>> definition.
>>
>> >>> Is this a bug, or is there anything I'm not taking into account?
>>
>> >>> Regards,
>> >>> Matteo
>>
>> >>> --http://www.matteolandi.net/
>>
>> >> --
>> >> Our newest site for the community: CakePHP Video 
>> >> Tutorialshttp://tv.cakephp.org
>> >> Check out the new CakePHP Questions sitehttp://ask.cakephp.organd help 
>> >> others with their CakePHP related questions.
>>
>> >> To unsubscribe from this group, send email to
>> >> cake-php+unsubscr...@googlegroups.com For more options, visit this group 
>> >> athttp://groups.google.com/group/cake-php
>>
>> > --
>> >http://www.matteolandi.net/
>>
>> --http://www.matteolandi.net/
>
> --
> Our newest site for the community: CakePHP Video Tutorials 
> http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help 
> others with their CakePHP related questions.
>
>
> 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
>



-- 
http://www.matteolandi.net/

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


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

Reply via email to