Wednesday, June 12, 2013

Re: Model data structure inconsistent?

I fully agree with Rolan here. I know this topic is a few years old, but I was curious to find out if a solution ever came to fruition. The fact that I can't just easily derive a standalone $parent, a $node, or a $child regardless of what Model I use to do the initial find on is causing me grief now in that I now have to code more to handle these different situations. Its a clear inconsistency and it would be nice to at least have an option to get it to output in this more consistent way.

As an example, take a simple Article -> Comment -> User relation.

An Article has many Comment, and a Comment belongs to a User,

If I do the find on the Article and have it also give me its Comment with its User I get:
$Article = Array
(
    [Article] => Array
    (
        [id] => 83
        ...
    )
    [Comment] => Array
    (
        [0] => Array
        (
            [id] => 2
            [content] => "Comment 1"
            [User] => Array
            (
                [id] => 3
                [name] => Bob
            )
        )
        [1] => Array
        (
            [id] = 3
            [content] = "Comment 2"
            [User] => Array
            (
                [id] => 3
                [name] => John
            )
        )
    )
)

I could then have a single view element that takes a $comment (lets assign $Article['Comment'][0] to it here) and renders out the content ($comment['content']) and the user who posted it ($comment['content']['name']).

If however, I want a lightweight way to get at the same comments using a find() on the Comment model (for implementing, for example, and AJAX comment provider), I get the following:
Array
(
    [0] => Array
    (
        [Comment] => Array
        (
            [id] => 2
            [content] => "Comment 1"
        )
        [User] => Array
        (
            [id] => 3
            [name] => Bob
        )
    )
    [1] => Array
    (
        [Comment] => Array
        (
            [id] = 3
            [content] = "Comment 2"
        )
        [User] => Array
        (
            [id] => 3
            [name] => John
        )
    )
)

I can't just grab a $comment now to feed to my view element. For each comment, I have to literally move the [User] element myself into the [Comment] element to get it in a format that the view element likes. And this costs processor power, and makes my code ugly.

Thanks.
Frank Weindel

On Friday, May 7, 2010 1:12:42 PM UTC-4, Roland Bock wrote:
Miles J wrote:
I don't see the benefit for this structure. Seems like twice the work  for no gain.    Cakes current implementation is best. The primary model and all  relations usually return within the same dimension, as it does not  need to be in hierarchical dimensions unless its a deep relationship.    
As of now, in a simple A -> B -> C relationship,

B found via Model A and
B found via Model B

require different code because they are structured differently.

Personally, I consider that inconsistent.

On May 7, 9:05 am, Roland Bock <feedback2...@eudoxos.de> wrote:    
Matthew Powell wrote:      
Jamie said it once, but I feel it's worth repeating.        
Modifying the code to output in a format YOU want for your new  application could pretty much screw up every cakephp application out  there.        
Matt        
Matt,    I am fully aware of that. And that is certainly not a decision to be  taken lightly. And if such a change should ever be introduced, some  migration strategy would be required.    BTW: I would have to change quite a few files myself.    On the other hand, the current data structure is inconsistent. In the  long run, it might be worth changing it.    Regards,    Roland                
On Fri, May 7, 2010 at 10:33, Roland Bock <feedback2...@eudoxos.de> wrote:        
Jamie,          
thanks for the reassurance :-)          
Submitting a ticket certainly is a good idea!          
Regards,          
Roland          
Jamie wrote:          
Hi Roland,          
What you're suggesting is a pretty major change to data returned by  Cake - major in that there would be a lot of user-end code that needs  changing if this were to happen to the core. Though I think it's  awesome that you'd be willing to write a patch and submit it, maybe  you should first submit a ticket to Lighthouse (  http://cakephp.lighthouseapp.com  ) for some feedback from the core development team. To be honest, I  found the structure of returned data a bit odd when I was starting out  with Cake as well, so you're not the only one.          
- Jamie          
On May 7, 5:46 am, Roland Bock <feedback2...@eudoxos.de> wrote:          
Yes, and validation, too, I guess...          
I hope to find the time :-)          
Thanks for your help!          
Regards,          
Roland          
You could probably write a Behaviour, but that will take additional  processing, which slows down the application.          
On May 7, 2:48 pm, Roland Bock <feedback2...@eudoxos.de> wrote:          
John Andersen wrote:          
I agree that the structure could be better, but as it is, is the way  we have to work with it, and I consider it sufficient for my usage.  Enjoy,     John          
OK, two questions:          
a) Do you think I could tweak current Models with a Behavior in order to  get the structure I prefer?          
b) Do you think it would make sense to send a patch that would change  the data structure (maybe for cake-1.4)?          
Thanks and regards,          
Roland          
On May 7, 12:30 pm, Roland Bock <feedback2...@eudoxos.de> wrote:          
Hi,  John Andersen wrote:          
I do think that if you don't like something in CakePHP, please write  your own solution to it, then offer it to the CakePHP developers.          
Sure. I'd like to understand first, though :-)  In CakePHP, Parent -> Node -> Child leads to different ways to handle a  single Node:  $node['id'];    // in case Node has been retrieved by Parent  $node[Child'];  but  $currentRecord['Node']['id']; // Node has been retrieved by Node  $currentRecord['Child'];  This seems to add complexity.  Now, here is what I would aim for (including the Ghost you added)  Array  (      [Parent] => Array          (              [0] => Array                  (                      [id] => 1                      [Node] => Array                          (                              [0] => Array                                  (                                      [id] => 17                                      [Child] => Array                                          (                                          )                                  )                              [1] => Array                                  (                                      [id] => 19                                      [Child] => Array                                          (                                          )                                  )                          )                      [Ghost] => Array                          (                              [0] => Array                                  (                                      [id] => 2                                  )                              [1] => Array                                  (                                      [id] => 3                                  )                          )                  )          )  )  This way I can say  $resultSet             <- This is an array of Parents  $currentRecord = $resultSet['Parent']['0']  $currentRecord         <- This is a Parent  $currentRecord['id']  $currentRecord['Node']  $currentRecord['Node'][0]['id']  $currentRecord['Node'][0]['Child']  $currentRecord['Ghost]  In the original CakePHP structure,  $resultSet  <- This is an array of arrays(Parent,Nodes, Ghosts)  $currentRecord = $resultSet['0']  $currentRecord        <- This is a Parent/Nodes/Ghosts array  $currentRecord['Parent']  $currentRecord['Parent']['id']  $currentRecord['Node']  $currentRecord['Node'][0]['Child]  $currentRecord['Ghost']  So basically, my way, you need to write *less* to access members of  'Parent'. The rest is pretty similar. But, (huge benefit, I think), you  can handle a Node always in the same way, regardless of whether you get  it included in a Parent or use the Node Model directly:  My way:  $node['id'];  // no matter what  $node['Child'];  Remeber the CakePHP way:  $node['id'];    // in case Node has been retrieved by Parent  $node[Child'];  but  $currentRecord['Node']['id']; // Node has been retrieved by Node  $currentRecord['Child'];          
Does the above helps you to clarify why the original structure is  good?          
Not yet, I am afraid. Maybe I was able to intrigue you with my proposal? :-)  Regards,  Roland          
[snip]  Check out the new CakePHP Questions sitehttp://cakeqs.organdhelpothers with  their CakePHP related questions.  You received this message because you are subscribed to the Google Groups  "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group  athttp://groups.google.com/group/cake-php?hl=en          
Check out the new CakePHP Questions sitehttp://cakeqs.organdhelpothers with  their CakePHP related questions.          
You received this message because you are subscribed to the Google Groups  "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group  athttp://groups.google.com/group/cake-php?hl=en          
Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others  with their CakePHP related questions.          
You received this message because you are subscribed to the Google Groups  "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group  athttp://groups.google.com/group/cake-php?hl=en          
Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others  with their CakePHP related questions.          
You received this message because you are subscribed to the Google Groups  "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group  athttp://groups.google.com/group/cake-php?hl=en          
Check out the new CakePHP Questions sitehttp://cakeqs.organd help others  with their CakePHP related questions.          
You received this message because you are subscribed to the Google Groups  "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group at  http://groups.google.com/group/cake-php?hl=en          
Check out the new CakePHP Questions sitehttp://cakeqs.organd help others  with their CakePHP related questions.          
You received this message because you are subscribed to the Google Groups  "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group at  http://groups.google.com/group/cake-php?hl=en          
Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with their CakePHP related questions.        
You received this message because you are subscribed to the Google Groups "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en        
Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with their CakePHP related questions.    You received this message because you are subscribed to the Google Groups "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en      
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.    You received this message because you are subscribed to the Google Groups "CakePHP" group.  To post to this group, send email to cake...@googlegroups.com  To unsubscribe from this group, send email to  cake-php+u...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en    

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
 
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake...@googlegroups.com
To unsubscribe from this group, send email to
cake-php+u...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: