1

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->Book->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.

Any help is appreciated

Bizarro181
  • 142
  • 1
  • 12

1 Answers1

0
$this->Book->Behaviors->attach('Containable');

$this->Book->find('all', array(
                            'contain' => array(
                                           'Log.user_id' => 2
                                         )
                         )
                  );
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • I haven't tried this out yet, but that looks like an expanded format of what I have. What does this do that makes it act differently? – Bizarro181 May 03 '12 at 14:01
  • Thanks, but I just tried this now and it is giving the same results as my attempt in my question. It is returning only the books that are associated with a log created by user 2 and leaving out books with no logs, or that have no association with user 2 – Bizarro181 May 04 '12 at 02:57
  • Think you'll have to do it in 2 requests. One to retrieve all the books regardless of the user, and a second one to retrieve the logs associated to a certain book. – pbond May 06 '12 at 00:30
  • Hmm I'll have to give it a shot. I was hoping there was a way I could get everything in one quick array, but I may have to make do with that. Thanks – Bizarro181 May 06 '12 at 04:31