I have code working search related models using the Search Plugin, but it forces me to break the structure of my model associations. As a result, my save and edit functions do not work properly.
Here is the structure of my data.
Model:
public $belongsTo = array(
'Movie' => array(
'className' => 'Movie',
'foreignKey' => 'movie_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MovieStarRecurrance' => array(
'className' => 'MovieStarRecurrance',
'foreignKey' => 'movie_star_recurrance_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MovieStarType' => array(
'className' => 'MovieStarType',
'foreignKey' => 'movie_star_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
public $hasMany = array(
'MovieStarLine' => array(
'className' => 'MovieStarLine',
'foreignKey' => 'movie_star_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => 'status_date',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
I am searching and filtering within the related model Movie and the User that Movie belongsTo.
public $filterArgs = array(
'parent_user_id' => array('type' => 'like', 'field' => 'User.parent_user_id'),
'initials' => array('type' => 'value', 'field' => 'User.id'),
'trace' => array('type' => 'like', 'field' => 'MovieStar.trace'),
'movie' => array('type' => 'query', 'method' => 'orConditionsMovie'),
'star_type' => array('type' => 'value', 'field' => 'MovieStar.movie_star_type_id'),
'code' => array('type' => 'value', 'field' => 'MovieStar.code'),
'status' => array('type' => 'value', 'field' => 'MovieStarLine.movie_star_status_id'),
'open' => array('type' => 'value', 'field' => 'MovieStar.open'),
'from_date' => array('type' => 'query', 'method' => 'orConditionsFromDate'),
'end_date' => array('type' => 'query', 'method' => 'orConditionsEndDate'),
);
In order to access these models at the top level of my search, I unbind the Movie model belongsTo association and rebind it as hasOne. I do the same for MovieStarLine and MovieStarStatus.
Controller:
$this->MovieStar->recursive = 0;
$this->MovieStar->unbindModel( array(
'belongsTo' => array('Movie')
)
);
// MovieStarStatus to be top level json element
$this->MovieStar->bindModel( array(
'hasOne' => array(
'Movie' => array(
'foreignKey' => false,
'conditions' => array('Movie.id = MovieStar.movie_id'),
'fields' => array('Movie.id', 'Movie.movie_id', 'Movie.user_id', 'Movie.movie_mid', 'Movie.movie_dba')
),
'User' => array(
'foreignKey' => false,
'conditions' => array('Movie.user_id = User.id'),
'fields' => array('User.id', 'User.initials', 'User.user_first_name', 'User.user_last_name', 'User.name', 'User.parent_user_id', 'User.secondary_parent_user_id')
),
'MovieStarLine' => array(
'foreignKey' => false,
'conditions' => array('MovieStarLine.movie_star_id = MovieStar.id',
'MovieStarLine.id = (SELECT id FROM "public"."movie_star_lines" AS "MovieStarLine" WHERE movie_star_id = MovieStar.id ORDER BY status_date DESC NULLS LAST, id DESC LIMIT 1)' // Get only most recent line
),
'order' => array( 'MovieStarLine.status_date' => 'DESC NULLS LAST')
),
'MovieStarStatus' => array(
'foreignKey' => false,
'conditions' => array('MovieStarLine.movie_star_status_id = MovieStarStatus.id'),
),
),
));
$this->MovieStar->contain(array(
'Movie',
'MovieStarLine' => array('MovieStarStatus'),
'MovieStarType',
'MovieStarRecurrance',
'User',
'MovieStarStatus'
));
// This is where the search filter occurs
$this->Prg->commonProcess();
$this->paginate = array( 'conditions' => $this->MovieStar->parseCriteria($this->Prg->parsedParams()), 'limit' => 100);
$movieStars = $this->paginate();
$this->set(compact('movieStars'));
}
The problem is when I try to search by parent_user_id or initials, I get an ERROR: missing FROM-clause entry for table "User".
It seems like the dynamic binding doesn't work. I have to remove Movie from belongsTo
Model:
public $belongsTo = array(
//'Movie' => array(
// 'className' => 'Movie',
// 'foreignKey' => 'movie_id',
// 'conditions' => '',
// 'fields' => '',
// 'order' => ''
//),
'MovieStarRecurrance' => array(
'className' => 'MovieStarRecurrance',
'foreignKey' => 'movie_star_recurrance_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MovieStarType' => array(
'className' => 'MovieStarType',
'foreignKey' => 'movie_star_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
and add hasOne for Movie and User for the search to work.
public $hasOne = array(
'Movie' => array(
'foreignKey' => false,
'conditions' => array('Movie.id = MovieStar.movie_id'),
'fields' => array('Movie.id', 'Movie.movie_id', 'Movie.user_id', 'Movie.movie_mid', 'Movie.movie_dba')
),
'User' => array(
'foreignKey' => false,
'conditions' => array('Movie.user_id = User.id'),
'fields' => array('User.id', 'User.initials', 'User.user_first_name', 'User.user_last_name', 'User.name', 'User.parent_user_id', 'User.secondary_parent_user_id')
),
'MovieStarLine' => array(
'foreignKey' => false,
'conditions' => array('MovieStarLine.movie_star_id = MovieStar.id',
'MovieStarLine.id = (SELECT id FROM "public"."movie_star_lines" AS "MovieStarLine" WHERE movie_star_id = MovieStar.id ORDER BY status_date DESC NULLS LAST, id DESC LIMIT 1)' // Get only most recent line
),
//'order' => array( 'MovieStarLine.status_date' => 'DESC NULLS LAST')
),
);
I can't do this though because it breaks all of my save and edit functionality. Does anyone know how to properly rebind the associations?
##################### UPDATE
I found a partial solution that seemingly gets me past the User model not showing up.
"Passing false as a second parameter of your Model::bindModel call. This will make your on-the-fly binding last the duration of the request." from this answer..
This might have fixed my bindings, still not sure because now I have an error with count and group by.
ERROR: column "MovieStarLine.status_date" must appear in the GROUP BY clause or be used in an aggregate
SQL Query: SELECT COUNT(*) AS "count"
$this->MovieStar->unbindModel( array(
'belongsTo' => array('Movie')
), false
);
// MovieStarStatus to be top level json element
$this->MovieStar->bindModel( array(
'hasOne' => array(
'Movie' => array(
'foreignKey' => false,
'conditions' => array('Movie.id = MovieStar.movie_id'),
'fields' => array('Movie.id', 'Movie.movie_id', 'Movie.user_id', 'Movie.movie_mid', 'Movie.movie_dba')
),
'User' => array(
'foreignKey' => false,
'conditions' => array('Movie.user_id = User.id'),
'fields' => array('User.id', 'User.initials', 'User.user_first_name', 'User.user_last_name', 'User.name', 'User.parent_user_id', 'User.secondary_parent_user_id')
),
'MovieStarLine' => array(
'foreignKey' => false,
'conditions' => array('MovieStarLine.movie_star_id = MovieStar.id',
'MovieStarLine.id = (SELECT id FROM "public"."movie_star_lines" AS "MovieStarLine" WHERE movie_star_id = MovieStar.id ORDER BY status_date DESC NULLS LAST, id DESC LIMIT 1)' // Get only most recent line
),
'order' => array( 'MovieStarLine.status_date' => 'DESC NULLS LAST')
),
'MovieStarStatus' => array(
'foreignKey' => false,
'conditions' => array('MovieStarLine.movie_star_status_id = MovieStarStatus.id'),
),
),
), false);
Related
I have 5 tables: tournaments, rounds, videos, rounds_tournaments, rounds_videos
Relations:
Tournament hasMany Video
Video belongsTo a Tournament
Tournament HABTM Round
Round HABTM Tournament
Video HABTM Round
Round HABTM Video
What I want to achieve:
Now I only want the videos that belong to a specific Tournament and Round.
So when clicking on a Tournament I only want the rounds associated with the clicked Tournament. And when clicking on a Round I only want the get the videos associated with the clicked Round and the Tournament.
I've tried everything but can't get it to work. I've played with several views as well (the index, view actions of Round/Tournament/Video). I'm not sure where to put the the JOIN statement, but I think it's inside the controller.
The Problem I've got atm is that when clicking on a Round it gives me the videos of that Round but for all the Tournaments and not for one Tournament.
Also looked here:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
CakePHP find HABTM
Round Model:
public $hasAndBelongsToMany = array(
'Tournament' => array(
'className' => 'Tournament',
'joinTable' => 'rounds_tournaments',
'foreignKey' => 'round_id',
'associationForeignKey' => 'tournament_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
'Video' => array(
'className' => 'Video',
'joinTable' => 'rounds_videos',
'foreignKey' => 'round_id',
'associationForeignKey' => 'video_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
Video Model:
public $belongsTo = array(
'Tournament' => array(
'className' => 'Tournament',
'foreignKey' => 'tournament_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
public $hasAndBelongsToMany = array(
'Round' => array(
'className' => 'Round',
'joinTable' => 'rounds_videos',
'foreignKey' => 'video_id',
'associationForeignKey' => 'round_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
Tournament Model:
public $hasMany = array(
'Video' => array(
'className' => 'Video',
'foreignKey' => 'tournament_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $hasAndBelongsToMany = array(
'Round' => array(
'className' => 'Round',
'joinTable' => 'rounds_tournaments',
'foreignKey' => 'tournament_id',
'associationForeignKey' => 'round_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
);
RoundsVideo model:
public $belongsTo = array(
'Round' => array(
'className' => 'Round',
'foreignKey' => 'round_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Video' => array(
'className' => 'Video',
'foreignKey' => 'video_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
RoundsTournament model:
public $belongsTo = array(
'Round' => array(
'className' => 'Round',
'foreignKey' => 'round_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Tournament' => array(
'className' => 'Tournament',
'foreignKey' => 'tournament_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
What I have tried:
index action of VideosController
$this->Video->recursive = -1;
$options['joins'] = array(
array('table' => 'rounds_videos',
'alias' => 'RoundsVideo',
'type' => 'inner',
'conditions' => array(
'Video.video_id = RoundsVideo.video_id'
)
),
array('table' => 'rounds',
'alias' => 'Round',
'type' => 'inner',
'conditions' => array(
'RoundsVideo.round_id = Round.round_id'
)
)
);
$this->set('videos', $this->Paginator->paginate());
index action of TournamentsController
$this->Tournament->recursive = -1;
$options['joins'] = array(
array('table' => 'rounds_tournaments',
'alias' => 'RoundsTournament',
'type' => 'inner',
'conditions' => array(
'Tournament.tournament_id = RoundsTournament.tournament_id'
)
),
array('table' => 'rounds',
'alias' => 'Round',
'type' => 'inner',
'conditions' => array(
'RoundsTournament.round_id = Round.round_id'
)
)
);
$this->set('tournaments', $this->Paginator->paginate());
a post has a user (belongsTo) and a user has a profile (hasOne). Normally everything works: I can access from Post to User and from User to Profile.
From Post, all works using a generic Find. The result is more or less this (I deleted some keys, just for example):
array(
'Post' => array(
'id' => '1',
'category_id' => '1',
'user_id' => '1',
'title' => 'Example',
'text' => '',
),
'User' => array(
'password' => '*****',
'id' => '1',
'group_id' => '1',
'username' => 'mirko',
'status' => 'active',
'Profile' => array(
'id' => '1',
'user_id' => '1',
'first_name' => 'Mirko',
'last_name' => 'Pagliai',
),
)
)
The problem comes when I use "fields", when I want to extract only a few fields.
For example, this works:
'fields' => array('id', 'title', 'User.id')
and the result is:
array(
'Post' => array(
'id' => '1',
'title' => 'Articolo di prova :-)'
),
'User' => array(
'id' => '1'
)
)
But I don't understand, always using "fields", how to access Profile.
These don'yt work:
'fields' => array('id', 'title', 'User.id', 'Profile.id')
'fields' => array('id', 'title', 'User.id', 'User.Profile.id')
I always get this error "Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'User.Profile.id' in 'field list'".
What's wrong? Thanks.
EDIT:
I'm not using the Containable behavior. Maybe is this the problem? Is it essential in this case?
An example, using paginate:
$this->paginate = array(
'conditions' => $conditions,
'fields' => array('id', 'title', 'User.id', 'Profile.id'),
'limit' => 10,
'recursive' => 2,
);
EDIT2:
thanks to #Hoff, I am close to the solution. But there is still something wrong (and Containable is very useful!).
In AppModel.php I added:
...
class AppModel extends Model {
public $actsAs = array('Containable');
public $recursive = -1;
...
But this doesn't work:
$this->paginate = array(
'conditions' => $conditions,
'contain' => array(
'User' => array(
'fields' => array('id'),
'Profile' => array(
'fields' => array('id', 'full_name'),
),
),
'Category' => array(
'fields' => 'title',
),
),
'fields' => array('id', 'user_id', 'category_id', 'title', 'created', 'modified', 'published'),
'limit' => 10,
);
Cause I get:
array(
(int) 0 => array(
'Post' => array(
'id' => '1',
'user_id' => '1',
'category_id' => '1',
'title' => 'Articolo di prova :-)',
'created' => '2012-06-21 18:46:00',
'modified' => '0000-00-00 00:00:00',
'published' => true
),
'Category' => array(
'title' => 'prova',
'id' => '1'
),
'User' => array(
'id' => '1'
)
)
)
but if I add any field to User (email, username, password, etc.), this works:
'User' => array(
'fields' => array('id', 'username'),
'Profile' => array(
'fields' => array('id', 'full_name'),
),
),
And I get:
array(
(int) 0 => array(
'Post' => array(
'id' => '1',
'user_id' => '1',
'category_id' => '1',
'title' => 'Articolo di prova :-)',
'created' => '2012-06-21 18:46:00',
'modified' => '0000-00-00 00:00:00',
'published' => true
),
'Category' => array(
'title' => 'prova',
'id' => '1'
),
'User' => array(
'id' => '1',
'username' => 'mirko',
'Profile' => array(
'id' => '1',
'full_name' => 'Mirko Pagliai'
)
)
)
)
Instead of using the 'recursive' key, I highly suggest using the containable behavior. The documentation on the behavior can be found here. I would suggest applying the behavior in your AppModel so you don't need to add it to all your models. In Model/AppModel.php:
App::uses('Model', 'Model');
class AppModel extends Model {
public $actsAs = array('Containable');
public $recursive = -1;
}
Then, for your pagination:
$this->paginate = array(
'conditions' => $conditions,
'fields' => array('id', 'title'),
'limit' => 10,
'contain' => array(
'User' => array(
'fields' => array('id'),
'Profile' => array(
'fields' => array('id')
)
)
)
);
I have 4 model:
Item------hasMany---->Detail
Item------hasMany---->Favourite
Item------hasMany---->Category
How can I find all Item that has Favourite.member_id = '8' and Category.item_category_id = '1'
Here is my Item model relation:
public $hasMany = array(
'Detail' => array(
'className' => 'Detail',
'foreignKey' => 'item_id',
'dependent' => true,
'conditions' => '',
'order' => 'created DESC',
),
'Category' => array(
'className' => 'Category',
'foreignKey' => 'item_id',
'dependent' => true,
'conditions' => '',
'order' => 'created DESC',
),
'Favorite' => array(
'className' => 'Favorite',
'foreignKey' => 'item_id',
'dependent' => true,
'conditions' => '',
'order' => 'created DESC',
)
);
I used this query() and it's ok:
$this->set('test',$this->Item->query("SELECT items.*
FROM items, categories, favourites
WHERE items.id = items_item_categories.item_id
AND items.id = favorites.item_id
AND categories.item_category_id = 1 AND favourites.member_id = 8"));
But I don't like using query(), and I change it into find(), and it seems not good:
$this->set('test',$this->Item->find('all', array(
'contain'=>array(
'ItemDetail',
'Category'=>array('conditions'=>array('Category.item_category_id'=>1)),
'Favorite'=>array('conditions'=>array('Favorite.member_id'=>8)));
You need call the containableBehaivor on your model
<?php
class Item extends AppModel {
public $actsAs = array('Containable');
}
in your controller you can make the query
$items = $this->Item->find('all', array(
'contain'=>array(
'ItemDetail',
'Category'=>array(
'conditions'=>array('Category.item_category_id'=>1)
),
'Favorite'=>array(
'conditions'=>array('Favorite.member_id'=>8)
)
)
);
$this->set('test', $items);
try using Joins
$items = $this->Item->find('all', array(
'joins' => array(
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'inner',
'conditions'=> array('Category.item_category_id' => 1)
),
array(
'table' => 'favorites',
'alias' => 'Favorite',
'type' => 'inner',
'conditions'=> array('Favorite.member_id' => 8, 'Item.id = Favorite.item_id')
)
),
'contain'=>array('ItemDetail','Category','Favorite')
);
$this->set('test', $items);
You could also try something like this:
$this->Item->hasMany['Favorite']['conditions']['member_id'] = 8;
Which has the same effect as rebinding the model with the condition.
Just a possible issue. The above will add the condition for the rest of the request, if you want to rollback to the previous behavior, you need to unset the condition:
unset($this->Item->hasMany['Favorite']['conditions']['member_id']);
Remember to set the $this->Item->recursive property to the desired value (recursive attribute). The default it's 1, which it's the minimum you need for this to work.
edit - the code that uses contain.
$this->paginate = array(
'limit' => 20,
'order' => array(
'Individual.last_name' => 'asc',
'Individual.first_name' => 'asc'
),
'contain' => array(
'AccountsTrailersIndividual' => array(
'fields' => array('id', 'accounts_trailer_id', 'individual_id', 'account_holder_id'),
'AccountsTrailer' => array(
'fields' => array('id', 'account_id', 'trailer_id', 'charge_off_date', 'closed_date'),
'Balance' => array(
'fields' => array('id', 'accounts_trailer_id', 'balance')
),
'Account' => array(
'fields' => array('id', 'account_number'),
'conditions' => array('account_number' => $search)
),
),
'AccountHolder' => array(
'fields' => array('id', 'name')
)
),
),
'fields' => array('id', 'first_name', 'middle_initial', 'last_name', 'suffix'),
);
I am trying to contain my data to only pull what I need and it is not working. I have everything set up correctly I believe but it is giving me errors. What I have is in my appModel.php file
public $actsAs = array('Containable');
the errors I am getting are as follows:
Model "AccountsTrailersIndividual" is not associated with model "AccountsTrailer" [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 339]
Model "AccountsTrailersIndividual" is not associated with model "AccountHolder" [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 339]
I have checked the associations in the stated models and the associations are there.
for AccountsTrailersIndividual it looks like this
public $belongsTo = array(
'Individual' => array(
'className' => 'Individual',
'foreignKey' => 'individual_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'AccountsTrailer' => array(
'className' => 'AccountsTrailer',
'foreignKey' => 'accounts_trailer_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'AccountHolder' => array(
'className' => 'AccountHolder',
'foreignKey' => 'account_holder_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
for AccountsTrailer
public $hasAndBelongsToMany = array(
'Individual' => array(
'className' => 'Individual',
'joinTable' => 'accounts_trailers_individuals',
'foreignKey' => 'accounts_trailer_id',
'associationForeignKey' => 'individual_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
and for the AccountHolder
public $hasMany = array(
'AccountsTrailersIndividual' => array(
'className' => 'AccountsTrailersIndividual',
'foreignKey' => 'account_holder_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
I know that I must be missing something but I can't find it.
Any help is greatly appreciated.
I know the title doesn't describe the best what I want to achieve, but I couldn't think of anything else.
Let's say I have the following array:
[Sender] => Array
(
[0] => Array
(
[id] => 1
[username] => admin
[PrivateMessagesUser] => Array
(
[id] => 1
[private_message_id] => 1
[sender_id] => 1
[recipient_id] => 3
[sender_status] => 1
[recipient_status] => 0
[random_key] =>
)
)
[1] => Array
(
[id] => 1
[username] => admin
[PrivateMessagesUser] => Array
(
[id] => 3
[private_message_id] => 1
[sender_id] => 1
[recipient_id] => 2
[sender_status] => 1
[recipient_status] => 0
[random_key] =>
)
)
)
I am using the ContainableBehaviour to cut off extra data.
My problem is: how do I filter out the Senders after a key which is present in PrivateMessagesUser? Say I need only the Sender where PrivateMessagesUser.recipient_id = 3.
I tried with
'Sender' => array(
'PrivateMessagesUser' => array(
)
),
but it didn't work. Looks like I have to get over the [0], [1]... keys somehow and I don't know how.
Any help would be appreciated!
EDIT
Here's how I obtain the array posted above:
$this->paginate = array(
'conditions' => array(
'PrivateMessage.id' => $this->__getUserPrivateMessagesInbox()
),
'contain' => array(
'Sender' => array(
'fields' => array('id', 'username'),
'PrivateMessagesUser' => array(
'fields' => array('PrivateMessagesUser.id')
//'conditions' => array('PrivateMessagesUser.recipient_id' => $this->Auth->user('id'))
)
),
'Recipient' => array(
'fields' => array('id', 'username'),
'conditions' => array('Recipient.id' => $this->Auth->user('id'))
)
)
);
How can I edit these lines in order to achieve the desired result?
EDIT 2
I am calling the paginate() from the PrivateMessagesController.
Associations:
User Model:
var $hasAndBelongsToMany = array(
'PrivateMessageSent' => array(
'className' => 'PrivateMessage',
'joinTable' => 'private_messages_users',
'foreignKey' => 'sender_id',
'associationForeignKey' => 'private_message_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'PrivateMessageReceived' => array(
'className' => 'PrivateMessage',
'joinTable' => 'private_messages_users',
'foreignKey' => 'recipient_id',
'associationForeignKey' => 'private_message_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
PrivateMessage Model:
var $hasAndBelongsToMany = array(
'Sender' => array(
'className' => 'User',
'joinTable' => 'private_messages_users',
'foreignKey' => 'private_message_id',
'associationForeignKey' => 'sender_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Recipient' => array(
'className' => 'User',
'joinTable' => 'private_messages_users',
'foreignKey' => 'private_message_id',
'associationForeignKey' => 'recipient_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
);
PrivateMessagesUser Model:
var $belongsTo = array(
'PrivateMessage' => array(
'className' => 'PrivateMessage',
'foreignKey' => 'private_message_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Sender' => array(
'className' => 'User',
'foreignKey' => 'sender_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Recipient' => array(
'className' => 'User',
'foreignKey' => 'recipient_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
And the database tables look like:
users: id, username, ...
private_messages: id, subject, ...
private_messages_users: id, private_message_id, sender_id, recipient_id, status, ...
There are three ways to achieve this.
Looking at your returned data array, I'm guessing PrivateMessagesUser belongsTo Sender.
1. Querying the PrivateMessagesUser model
Sometimes, all it takes is querying the right model. You can apply the conditions directly to the model you want to apply your restrictions on.
$this->Sender->PrivateMessagesUser->find('all', array(
'conditions' => array('PrivateMessagesUser.recipient_id' => 3),
'contain' => array('Sender')
));
2. Applying conditions to related models
This doesn't work all the time, because CakePHP's SQL generation doesn't always work the way you want it to. But in simple cases like a hasMany relationship, you can use this:
$this->Sender->find('all', array(
'conditions' => array('PrivateMessagesUser.recipient_id' => 3),
'contain' => array('PrivateMessagesUser')
));
Again, this will work only if Cake executes a JOIN for the Containable parameters AND uses the conditions right.
3. Using joins
This method is foolproof but a little messy since it's highly customizable.
$this->Sender->find('all', array(
'joins' => array(
array(
'table' => 'private_messages_users',
'alias' => 'PrivateMessagesUser',
'type' => 'inner',
'conditions' => array(
'PrivateMessagesUser.sender_id = Sender.id',
'PrivateMessagesUser.recipient_id' => 3
)
)
)
));
My last piece of advice for you is to place this code into the Model. Follow the principle of "fat models, skinny controllers" whenever possible.
In the Sender model:
function fetchByRecipientId($recipientId) {
...
}
Good luck!
EDIT: In order to make this work for pagination, follow the methods above but use the following format:
$this->paginate = array(
'PrivateMessagesUser' => array(
'conditions' => array('PrivateMessagesUser.recipient_id' => 3),
'contain' => array('Sender'),
// 'order' => ... other keys if you wish
)
);
$this->set('senders', $this->paginate());
You can put conditions inside Containable, like so:
$this->Sender->find('all', array(
'contain' => array(
'PrivateMessagesUser' => array('conditions' => array('recipient_id' => 3))
)
));
EDIT:
Try modifying your $this->paginate as follow:
$this->paginate = array(
'conditions' => array(
'PrivateMessage.id' => $this->__getUserPrivateMessagesInbox()//array of message id's
),
'fields' => array('PrivateMessagesUser.id'),
'contain' => array(
'Sender' => array(
'fields' => array('id', 'username'),
),
'Recipient' => array(
'fields' => array('id', 'username'),
'conditions' => array('Recipient.id' => $this->Auth->user('id'))
)
)
)
Then call your paginate $this->paginate($this->PrivateMessage)