I have the following tables: binders, docs, users, docs_users. Doc
belongsTo Binder, Doc hasAndBelongsToMany User.

I want to get binders and their associated docs for the user that is
currently logged in (the associated user_id in the docs_users table).

I have tried Containable and find('all') with joins, conditions, etc.
but I can't figure out how to remove the Docs that are from Users who
are not associated in the docs_users table.

This code does NOT work:

$binders = $this->Binder->find(
        'all',
        array(
            'joins' => array(
                array(
                    'table' => 'binders_users',
                    'alias' => 'BindersUser',
                    'type' => 'inner',
                    'foreignKey' => false,
                    'conditions'=> array(
                        'BindersUser.binder_id = Binder.id',
                        'BindersUser.user_id = ' . $this->Auth-
>user('id')
                    )
                ),
                array(
                    'table' => 'docs',
                    'alias' => 'Doc',
                    'type' => 'left',
                    'foreignKey' => false,
                    'conditions'=> array(
                        'Doc.binder_id = Binder.id',
                    )
                ),
                array(
                    'table' => 'docs_users',
                    'alias' => 'DocsUser',
                    'type' => 'left',
                    'foreignKey' => false,
                    'conditions'=> array(
                        'DocsUser.doc_id = Doc.id',
                        'DocsUser.user_id = ' . $this->Auth-
>user('id')
                    )
                )
            ),
            'recursive'=>0
        )
    );
$this->set('binders', $binders);

And neither does this:

$this->Binder->recursive = 2;
$this->Binder->Behaviors->attach('Containable');
$this->Binder->contain(array(
    'Branch',
    'Doc' => array(
        'User' => array(
            'DocsUser' => array(
                'conditions' => array('id = "17"')
            )
        )
    )
));
$binders = $this->Binder->find('all');

Any help from you seasoned pros would be great! Thanks! Also, that's
to those of you who have helped me on irc. I just couldn't get it
figured out and thought it justified a little more explanation.

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

Reply via email to