I am calling find() on my Game model in order to get the reviews of that game for a particular platform. The problem is that the find() call doesn't seem to be paying attention to my conditions array. The call returns all the reviews for a game regardless of the platform_id.
I have these model associations:
User hasMany Review
Game hasMany Review
Review belongsTo Game
Review belongsTo User
Game HABTM Platform
This is the find call that I created:
$options = array(
'group' => array(
'Game.id'
),
'joins' => array(
array(
'table' => 'reviews',
'type' => 'LEFT',
'conditions' => array(
'Game.id=reviews.game_id'
)
),
array(
'table' => 'platforms',
'type' => 'LEFT',
'conditions' => array(
'reviews.platform_id=platforms.id'
)
),
array(
'table' => 'users',
'type' => 'LEFT',
'conditions' => array(
'reviews.user_id=users.id'
)
)
),
'fields' => array(
'Game.id', 'Game.name',
'platforms.id', 'platforms.name',
'users.id', 'users.username'
),
'conditions' => array(
'AND' => array(
'NOT' => array('reviews.review_text' => NULL),
'platforms.id' => $pid,
)
),
'limit' => 5
);
$this->Game->find('all', $options);
Here is a sample return for a find() call for platform_id=2:
array(
(int) 0 => array(
'Game' => array(
'id' => '58',
'name' => 'Bioshock 2'
),
'platforms' => array(
'id' => '2',
'name' => 'PlayStation 3'
),
'users' => array(
'id' => '20',
'username' => 'pspmaniac'
),
'Review' => array(
(int) 0 => array(
'id' => '32',
'review_text' => 'This is the PC review.',
'score' => '4',
'user_id' => '20',
'game_id' => '58',
'created' => '2013-04-30 19:59:40',
'platform_id' => '10'
),
(int) 1 => array(
'id' => '33',
'review_text' => 'This is the PS3 review.',
'score' => '7',
'user_id' => '20',
'game_id' => '58',
'created' => '2013-04-30 20:00:04',
'platform_id' => '2'
),
(int) 2 => array(
'id' => '34',
'review_text' => 'This is the XBOX 360 review.',
'score' => '6',
'user_id' => '20',
'game_id' => '58',
'created' => '2013-04-30 20:00:22',
'platform_id' => '1'
)
)
)
);
In the 'Review' index, it returns three reviews for the game (platform ids 10, 2, and 1) when it should only return the review with platform_id=2.
This is my reviews table:
CREATE TABLE reviews
(
id mediumint unsigned not null auto_increment,
review_text mediumint,
score int not null,
user_id mediumint unsigned not null,
game_id mediumint unsigned not null,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (game_id) REFERENCES games (id)
);
So, in the end, the only thing wrong with the returned array is that it contains reviews not only for the condition 'platforms.id=2' but for all platform ids.
Solution
Using Dave's comment, I came up with this solution to my problem using containable:
$this->loadModel('Review');
$this->Review->find('all', array( 'contain' => array('Platform'), 'conditions' => array( 'Review.platform_id' => 2 )
));
Related
In Cakephp I need to get the tutors who teach a subject 'primary English'. Instead I get all the tutors with any subject so the condition gets ignored with no error. There is a habtm relationship between tutors and subjects they teach.
$this->Tutor->Behaviors->load('Containable');
$tutors=$this->Tutor->find('all',array(
'contain' => array('Subject',array( 'conditions'=> array('Subject.name' => 'Primary English'))),
'contain' => array('Subject'),
'recursive' =>-1,
// 'order'=> $orderoptions,
'fields'=>array('Tutor.last_name', 'Tutor.first_name','Tutor.id' ),
));
debug( $tutors);
array(
(int) 0 => array(
'Tutor' => array(
'last_name' => 'Wyers',
'first_name' => 'Adele',
'id' => '13'
),
'Subject' => array()
),
(int) 1 => array(
'Tutor' => array(
'last_name' => 'Payet',
'first_name' => 'Allison',
'id' => '7'
),
'Subject' => array(
(int) 0 => array(
'id' => '4',
'name' => 'English - Year 11',
'TutorsSubject' => array(
'id' => '30',
'tutor_id' => '7',
'subject_id' => '4'
)
),
Remove 'recursive' => -1. This prevents it from selecting relationships recursively. If this still doesn't work then put 'recursive' => 2 in.
I have 3 tables: projects, project_reminder_users, project_types.
The relations is as follow:
Project => belong to => ProjectType
hasMany => ProjectReminderUser
ProjectReminderUser => belong to => Project
ProjectType => hasMany => Project
I get all data based on who assigned(ProjectReminderUser) by this
$this->Project->ProjectReminderUser->Behaviors->load('Containable');
$this->paginate = array(
'ProjectReminderUser' => array(
'limit' => $limit,
'contain' => array(
'Project' => array(
'ProjectComment',
'ProjectFile',
),
'User'
),
'conditions' => array(
'User.group_id' => $this->Session->read('Auth.User.group_id'),
'ProjectReminderUser.user_id' => $this->Session->read('Auth.User.id'),
'Project.project_status_id' => PROJECT_STATUS_OPEN,
),
'order' => 'Project.due_date',
)
);
$this->set('myTasks', $this->paginate('ProjectReminderUser'));
and the result look like this
array(
'ProjectReminderUser' => array(
'id' => '96',
'user_id' => '1',
'project_id' => '46'
),
'Project' => array(
'id' => '46',
'project_type_id' => '9',
'contact_id' => null,
'company_id' => null,
'subject' => 'Test Modified Field',
'description' => 'Test Modified Field',
'ProjectFile' => array(
(int) 0 => array(
'id' => '19',
'project_id' => '46',
'user_id' => '6',
'file_path' => '46_bhbinary_xmm_1728.jpg',
'notes' => null,
'created' => '2013-11-26 18:37:49'
),
),
'ProjectComment' => array(
)
),
'User' => array(
'password' => '*****',
'id' => '1',
'group_id' => '1',
'email' => 'xxx#xxxxx.com',
'first_name' => 'xxxx',
'deleted' => false,
'displayName' => 'xxxxx'
)
)
In the result There is data for Project.project_type_id, but I would like to have more detail of that. So I can show it as name instead of number. maybe like ProjectType.name instead.
How can I achieve this, so I can sort it in the view? something like this
$this->Paginator->sort('ProjectType.name', 'Type');
The problem is that Paginator doesn't play nice with deep model associations. I think though if rather than using Cake's methods for doing model associations, you instead do your joins manually, you may be able to work out the sorting as you want. See the below discussion.
http://sinkpoint.railsplayground.net/cakephp-pagination-deep-sort-and-habtm/
Worst comes to worse, you may have to also rewrite the model's paginate function to deal with sorting how you need it to.
How about
'contain' => array(
'Project' => array(
'ProjectComment',
'ProjectFile',
'ProjectType',
),
'User'
),
Looks like you did not contain "ProjectType"
I have 2 tables, Militia and Injunctions. Militia can have many injunctions and injunctions only belong to one militia. I set those up in the models but when i call a find all for militia the injunctions aren't pulled out.
Militia model
class Militia extends AppModel {
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
public $hasMany = array(
'Injunction' => array(
'className' => 'Injunction',
'foreignKey' => 'militia_id'
//'conditions' => array("not" => array('Injunction.removed' => null))
)
);
}
Injunctions model
class Injunction extends AppModel {
public $belongsTo = array(
'Militia' => array(
'className' => 'Militia',
'foreignKey' => 'militia_id'
)
);
}
and the query getting the militia members
$user = $this->Session->read("User");
$militias = $this->Militia->find('all',
array(
'conditions'=>array(
"Militia.user_id" => $user['User']['id'],
"Militia.deleted" => 0
)
)
);
output
/app/Controller/UsersController.php (line 41)
array(
(int) 0 => array(
'Militia' => array(
'id' => '26',
'first_name' => 'Chris',
'last_name' => 'Morris',
'created' => '2013-02-11 13:45:24',
'user_id' => '2',
'status' => '1',
'deleted' => '0'
)
),
(int) 1 => array(
'Militia' => array(
'id' => '31',
'first_name' => 'John',
'last_name' => 'Smith',
'created' => '2013-02-11 14:03:50',
'user_id' => '2',
'status' => '0',
'deleted' => '0'
)
),
(int) 2 => array(
'Militia' => array(
'id' => '32',
'first_name' => 'test',
'last_name' => 'user',
'created' => '2013-02-11 14:21:38',
'user_id' => '2',
'status' => '0',
'deleted' => '0'
)
),
(int) 3 => array(
'Militia' => array(
'id' => '33',
'first_name' => 'test',
'last_name' => 'user',
'created' => '2013-02-11 14:24:02',
'user_id' => '2',
'status' => '1',
'deleted' => '0'
)
)
)
I've done the same thing before on other projects but for some reason this one time it's not pulling out the associated data. It's probably something stupid like a typo but I've been looking and testing and can't find anything wrong.
Check ORM (hasOne, hasMany, belongsTo, hasAndBelongsToMany) definition,
Check if foreign key ids are set right
Check if there are data's in the table and its related table.
Check the results of find without condition
Check to see if recursive level is greater than 0
To rule out your old cached query issue, set debug level higher to 0
Look out in the bottom section of sql dump to see what query is being fired, if it feels ok try same query with SQL away from Cake/Php.
I am using cakephp 2.1 and developing an application, which contain the following table structures.
1. people(id, name)
2. movies(id, name)
and 'people' habtm movies by tables as follows.
1. castsmovie(id, person_id, movie_id)
2. directorsmovie(id, person_id, movie_id)
3. moviesmusicdirector(id, person_id, movie_id)
4. moviesproducers(id, person_id, movie_id)
5. movieswriter(id, person_id, movie_id)
6. cinematographersmovie(id, person_id, movie_id)
I wanna fetch the movies for perticular person and paginate the movies for him.
So the code I have written is in controller
$this->paginate = array('Person' => array(
'contain' => array(
'CastsMovie' => array('Movie'),
'DirectorsMovie' => array('Movie'),
'MoviesProducer' => array('Movie'),
'MoviesMusicDirector' => array('Movie')),
'MoviesWriter' => array('Movie'),
'CinematographersMovie' => array('Movie')
),
'conditions' => array('Person.id' => $id)));
and the result is as follows
array(
(int) 0 => array(
'Person' => array(
'id' => '101',
'name' => 'Darshan Thoogudeep',
),
'CastsMovie' => array(
(int) 0 => array(
'id' => '53',
'movie_id' => '14',
'person_id' => '101',
'order' => '1',
'Movie' => array(
'id' => '14',
'name' => 'Krantiveera Sangolli Rayanna',
)
),
(int) 1 => array(
'id' => '78',
'movie_id' => '19',
'person_id' => '101',
'order' => '0',
'Movie' => array(
'id' => '19',
'name' => 'Saarathi',
)
),
(int) 2 => array(
'id' => '137',
'movie_id' => '35',
'person_id' => '101',
'order' => '1',
'Movie' => array(
'id' => '35',
'name' => 'BulBul',
)
),
(int) 3 => array(
'id' => '153',
'movie_id' => '42',
'person_id' => '101',
'order' => '0',
'Movie' => array(
'id' => '42',
'name' => 'Viraat',
)
)
),
'CinematographersMovie' => array(),
'DirectorsMovie' => array(),
'MoviesMusicDirector' => array(),
'MoviesWriter' => array(),
'MoviesProducer' => array()
)
)
But I want to fetch all movies of perticular person with limit as 20.. So that I can easily paginate the limit of movies in each page.. Please help me to get the soluting.. The work is more appreciated.
I would suggest changing your data model so you have just one HABTM table:
people_movies(id, person_id, movie_id,role_id)
You would also need a role table:
role(id, role_name)
Then you can store them all in the same table and add additional roles in future if required.
Using this approach the query to get all movies for one person will be much simpler.
I have a few nested models which I'm trying to load using Containable behaviour in CakePHP.
Most of it works fine.
However, 1 model that has a hasMany relation only returns 1 object:
$article = $this->News->find('first',array(
'conditions'=>array('News.id'=>$id),
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'Product' => array(
'Brand',
'Category'
)
)))
));
The object only being loaded once is the relation Newspicture hasMany NewspicturesProduct
When I log the queries, I get the following:
SELECT `NewspicturesProduct`.`id`, `NewspicturesProduct`.`x`, `NewspicturesProduct`.`y`, `NewspicturesProduct`.`product_id`, `NewspicturesProduct`.`newspicture_id`, `NewspicturesProduct`.`w`, `NewspicturesProduct`.`h` FROM `edclondon`.`newspictures_products` AS `NewspicturesProduct` WHERE `NewspicturesProduct`.`newspicture_id` = 3
Which gives me 3 results in phpMyAdmin. But only 1 in CakePHP's debug:
'Newspicture' => array(
(int) 0 => array(
'id' => '3',
'news_id' => '2',
'newspicture_file_path' => '5022443f-ddf8-4115-ae57-618e9d60b047.jpg',
'newspicture_file_size' => '1232546',
'order' => null,
'NewspicturesProduct' => array(
'id' => '1',
'x' => '0.180664',
'y' => '0.295312',
'product_id' => '3',
'newspicture_id' => '3',
'w' => '0.286133',
'h' => '0.478125',
'Product' => array(
'id' => '3',
//....
'Brand' => array(
'id' => '6',
//...
),
'Category' => array(
'id' => '6',
//....
)
)
)
)
When retrieving the Newspictures object rather then retrieving the News object, I do get all 3 NewspicturesProduct objects.
It seems to me that the code corresponding the query you showed should be:
$article = $this->News->find('first',array(
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'conditions'=>array('NewspicturesProduct.newspicture_id'=>'3')
'Product' => array(
'Brand',
'Category'
)
)))
));
and not the one you gave...
It seems you need 3 records from NewspicturesProduct. If that then you can try:
$article = $this->News->find('first',array(
'contain' => array(
'Newslayout',
'Newspicture'=> array(
'NewspicturesProduct' => array(
'conditions'=>array(
'limit'=> 3
),
'Product' => array(
'Brand',
'Category'
)
)))
));