Monday, May 7, 2012

Re: CakePHP Containable on Associated Model

How do you have your Models setup?
Book
    $hasMany = array('Log' => array('className' => 'Log', 'foreignKey' => 'book_id'));
   $belongsTo = array('User' => array('className' => 'User', 'foreignKey' => 'user_id'));
                                   
Log
    $belongsTo = array('Book' => array('className' => 'Book', 'foreignKey' => 'book_id'),
                                    'User' => array('className' => 'User', 'foreignKey' => 'user_id'));


If it is setup like this and you have user_id field in books table, then it should work.  If Book does not have user_id, then you may have to do something like a sub query of Log.user_id=2, and then join that with Book on book_id. or something like that.

Can you create a mySQL statement to pull the data out?  if so, then post the SQL and maybe someone can help explain how to do this.

HTH.
Bill

On 5/6/2012 6:33 PM, Rob-3 [via CakePHP] wrote:
Hi,

Let me try to clarify because I think I've confused even myself at
this point lol

I have 3 books that I am trying to retrieve from the database.  Users
have added these books to reading logs, but I want to return each
book, and then only the logs where the user id is equal to the current
user (lets say two)
right now using Log.user_id = 2 i get the following array

Book 1
     Empty log array

Book 2
     log array, userid = 2

Book 3
     log array, userid = 2
     log array, userid = 3

That third book, where the extra log is returned is what I don't want
happening.  Does this make more sense?

On May 6, 1:06 am, bs28723 <[hidden email]>
wrote:

>     Are you looking for a find where&nbsp; "Log.user_id=2 OR Book.log_id =
>     null"?
>
>     On 5/3/2012 11:00 PM, Rob-3 [via CakePHP] wrote:
>      I have a setup where there are Books Users and Logs.
>       Each Log has a
>
>       Book and a User and I am trying to retrieve a list of all the
>       books
>
>       regardless of the user, but then also retrieve the Logs associated
>
>       with that book (recursive is set to 1), but then only retrieving
>       the
>
>       logs of the current logged in user.
>
>       Hopefully that's clear. I tried using Containable and like so:
>
>       $this-&gt;Book-&gt;contain('Log.user_id = 2');
>
>       But unfortunately this leaves out Books for which User 2 has no
>       logs
>
>       for. Am I going about this correctly and I'm just not using
>
>       containable properly, or am I doing this all wrong.
>
>       Let me know if anything needs to be clarified or expanded...Any
>       help
>
>       is appreciated!
>
>       --
>       Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org&nbsp;
>
>       Check out the new CakePHP Questions sitehttp://ask.cakephp.org&nbsp;and
>       help others with their CakePHP related questions.
>
>       To unsubscribe from this group, send email to
>
>       [hidden email]
>       For more options, visit this group athttp://groups.google.com/group/cake-php
>
>         If you reply to this email, your
>           message will be added to the discussion below:
>        http://cakephp.1045679.n5.nabble.com/CakePHP-Containable-on-Associate...
>
>         To start a new topic under CakePHP, email
>         [hidden email]
>         To unsubscribe from CakePHP, click
>           here .
>         NAML
>
> --
> View this message in context:http://cakephp.1045679.n5.nabble.com/CakePHP-Containable-on-Associate...
> Sent from the CakePHP mailing list archive at Nabble.com.

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php



If you reply to this email, your message will be added to the discussion below:
http://cakephp.1045679.n5.nabble.com/CakePHP-Containable-on-Associated-Model-tp5684781p5689882.html
To start a new topic under CakePHP, email [hidden email]
To unsubscribe from CakePHP, click here.
NAML


View this message in context: Re: CakePHP Containable on Associated Model
Sent from the CakePHP mailing list archive at Nabble.com.

--
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: