cakephpassociationscontainable

cakephp contain twice model association


I have to fetch data from posts table

which has foreign keys (
category_id references to categories.id
created_by references to users.id, updated_by references to users.id )

I can fetch created_by username but not both

$this->Post->Behaviors->load('Containable');

        $this->paginate = array(
            'conditions' => array('Post.category_id' => $id),
            'order' => array('title'),
            'contain' => array(
                'User'=>array(
                        'fields'=>array('id','first_name','last_name','username'),
                        'conditions' => array('User.id = Post.created_by')
                    ),
                //posts table has 2 fields(created_by & updated_by) associated with users table
                //'User'=>array(
                //        'fields'=>array('id','first_name','last_name','username'),
                //        'conditions' => array('User.id = Post.updated_by')
                //    ),
                'Category'=>array(
                    'Type'=>array(
                        'fields'=>array('id','type_name')
                        )
                    ),
            )
        );

//post model

 public $belongsTo = array(
    'User'=> array(
        'className' => 'User',
        'foreignKey' => 'created_by',
        'foreignKey' => 'updated_by'
    ),

); 

//user model

public $hasMany = array(
    'Post' => array(
        'className' => 'Post',
        'foreignKey' => array('created_by','updated_by'),
    ),
);

how to show both and alias both Users as (created_by & updated_by)


Solution

  • First you need to define two relationships in the Post model

    public $belongsTo = array(
        'CreatedUser'=> array(
            'className' => 'User',
            'foreignKey' => 'created_by'
        ),
        'UpdatedUser'=> array(
            'className' => 'User',
            'foreignKey' => 'updated_by'
        )
    );
    

    Now create the converse relationships in the User model.

    public $hasMany = array(
        'CreatedPosts' => array(
            'className' => 'Post',
            'foreignKey' =>'created_by'
        ),
        'UpdatedPosts' => array(
            'className' => 'Post',
            'foreignKey' => 'updated_by'
        ),
    );
    

    Then the find()

    $this->Post->Behaviors->load('Containable');
    
    $this->paginate = array(
            'conditions' => array('Post.category_id' => $id),
            'order' => array('title'),
            'contain' => array(
                'CreatedUser'=>array(
                        'fields'=>array('id','first_name','last_name','username')
                    ),
                'UpdatedUser'=>array(
                        'fields'=>array('id','first_name','last_name','username')
                    ),
                'Category'=>array(
                    'Type'=>array(
                        'fields'=>array('id','type_name')
                        )
                    ),
            )
        );