Cakephp: Contain Behavior, conditions on array of array - cakephp

I have a problem with contain behavior conditions and empty array.
There is part of code:
$menusTopUser = $this->Menu->find('all', array(
'contain' => array('TemplateRegion', 'Language', 'Item', 'MenusUserGroup' => array('conditions' => array('MenusUserGroup.user_group_id ' => $this->Auth->user('UserGroup.id')))
),
'conditions' => array(
'TemplateRegion.alias' => 'menu_top_user',
'Menu.active' => 1,
'Language.ISO-639-2' => $this->Session->read('Config.frontendLanguage'),
),
'fields' => array('Menu.title', 'Item.slug', 'Menu.url', 'Menu.target', 'Language.ISO-639-2'),
'order' => array('Menu.lft'),
'recursive' => 0));
$this->set(compact('menusTopUser'));
My associations look like:
Menu
hasMany: MenusUserGroup
MenusUserGroup
belongsTo: Menu, UserGroup
After executing the find, I have the next array where there are many MenusUserGroup empty. How make to filter only the record with 'MenusUserGroup.user_group_id ' => $this->Auth->user('UserGroup.id')?
Array(
[0] => Array
(
[Menu] => Array
(
[title] => Offerte e modulistica promoter
[url] =>
[target] => _self
[id] => 11
)
[Item] => Array
(
[slug] => offerte-e-modulistica-promoter
[id] => 36
)
[Language] => Array
(
[ISO-639-2] => ita
[id] => 1
)
[TemplateRegion] => Array
(
[id] => 10
)
[MenusUserGroup] => Array
(
)
)
[1] => Array
(
[Menu] => Array
(
[title] => Offerte e modulistica agenti
[url] =>
[target] => _self
[id] => 10
)
[Item] => Array
(
[slug] => offerte-modulistica
[id] => 29
)
[Language] => Array
(
[ISO-639-2] => ita
[id] => 1
)
[TemplateRegion] => Array
(
[id] => 10
)
[MenusUserGroup] => Array
(
[0] => Array
(
[id] => 7
[menu_id] => 10
[user_group_id] => 5
)
)
)
[2] => Array
(
[Menu] => Array
(
[title] => Gestione contratti
[url] =>
[target] => _self
[id] => 13
)
[Item] => Array
(
[slug] =>
[id] =>
)
[Language] => Array
(
[ISO-639-2] => ita
[id] => 1
)
[TemplateRegion] => Array
(
[id] => 10
)
[MenusUserGroup] => Array
(
[0] => Array
(
[id] => 10
[menu_id] => 13
[user_group_id] => 5
)
)
)

Use a join.
$menusTopUser = $this->Menu->find('all', array(
// join statement
'joins' => array(
array(
'table' => 'menus_user_groups',
'alias' => 'MenuUserGroupJoin',
'type' => 'INNER',
'conditions' => array(
'MenuUserGroupJoin.menu_id = Menu.id',
// condition
'MenuUserGroupJoin.user_group_id = ' . $this->Auth->user('UserGroup.id'),
),
),
),
'contain' => array('TemplateRegion', 'Language', 'Item', 'MenusUserGroup'),
'conditions' => array(
'TemplateRegion.alias' => 'menu_top_user',
'Menu.active' => 1,
'Language.ISO-639-2' => $this->Session->read('Config.frontendLanguage'),
),
'fields' => array('Menu.title', 'Item.slug', 'Menu.url', 'Menu.target', 'Language.ISO-639-2'),
'order' => array('Menu.lft'),
'recursive' => 0));

Related

hasMany condition show unwanted records cakephp 1.3

I have Questions table which have store so many question. Question related to question_topics so have create has many relationship with question. Now it's look like this:
$this->Question->bindModel(
array(
'hasMany' => array(
'QuestionTopic'=>array(
'className' => 'QuestionTopic',
'foreignKey' => 'question_id',
'dependent' => true,
'conditions' => array('QuestionTopic.areas_id' => 165),
'type' => 'left'
)
)
)
);
print_r($this->Question->find('all'));
die;
When i saw the result then it look like this
Array
(
[0] => Array
(
[Question] => Array
(
[id] => 89
[user_id] => 1
[question_group_id] => 0
[question] => jQuery function here
[target_id] => 1
[type] => 1
[order] => 1
[description] => additional info here
[privacy_friend_id] =>
[channel_id] => 1
[status] => 0
[location] => Chandigarh, India
[regions] => 307
[select_country] => 381
[select_states] => 515
[created] => 2014-04-15 06:59:44
[modified] => 2014-04-15 06:59:44
)
[QuestionTopic] => Array
(
[0] => Array
(
[id] => 167
[areas_id] => 165
[question_id] => 89
)
)
)
[1] => Array
(
[Question] => Array
(
[id] => 90
[user_id] => 1
[question_group_id] => 0
[question] => Art
[target_id] => 2
[type] => 1
[order] => 1
[description] => addional infomation here
[privacy_friend_id] =>
[channel_id] => 1
[status] => 0
[location] => Chandigarh, India
[regions] => 307
[select_country] => 381
[select_states] => 515
[created] => 2014-04-15 07:52:17
[modified] => 2014-04-15 07:52:17
)
[QuestionTopic] => Array
(
)
)
)
I want first record only which have Question topic id 167 not second one. How can i do this.
Don't use hasMany, Use has one like this
$this->Question->bindModel(
array(
'hasOne' => array(
'QuestionTopic'=>array(
'className' => 'QuestionTopic',
'foreignKey' => 'question_id',
'dependent' => true,
'type' => 'left'
)
)
)
);
print_r($this->Question->find('all',array('conditions'=>array('QuestionTopic.areas_id' => array('165')))));
Something like this?
$question = $this->Question->find('first', array('conditions' => array('QuestionTopic.id' => 167));

group is not working in CakePHP

I am running following query in CakePHP:
$options = array('conditions' => array('Patient.' . $this->Patient->primaryKey => $id),array('recursive'=>2,'group'=>array('group_id')));
$this->set('patient', $this->Patient->PatientTest->find('all', $options));
But my group is not working as needed. It gives me output as follows:
Array
(
[0] => Array
(
[PatientTest] => Array
(
[patient_id] => 2
[test_id] => 1
[date] => 2014-03-27 17:44:17
[result] => 55
[group_id] => 1
)
[Patient] => Array
(
[id] => 2
[name] => Test Patient
[age] => 44
[gender] => 0
[email] => emailid#gmail.com
[contact] => 789654123
[date] => 0000-00-00 00:00:00
)
[Test] => Array
(
[id] => 1
[name] => Hb
[group_id] => 1
[normal] => 12 - 16 gma%
)
[Group] => Array
(
[id] => 1
[name] => Haematology
)
)
[1] => Array
(
[PatientTest] => Array
(
[patient_id] => 2
[test_id] => 2
[date] => 2014-03-27 17:44:17
[result] => 55
[group_id] => 1
)
[Patient] => Array
(
[id] => 2
[name] => Test Patient
[age] => 44
[gender] => 0
[email] => emailid#gmail.com
[contact] => 789654123
[date] => 0000-00-00 00:00:00
)
[Test] => Array
(
[id] => 2
[name] => PCV
[group_id] => 1
[normal] => 35-50%
)
[Group] => Array
(
[id] => 1
[name] => Haematology
)
)
)
I need them group by either group_id or by Group.id.
I am unable to find why it is not grouping.
not
$options = array
(
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
array(
'recursive'=>2,
'group'=>array('group_id')
)
);
but
$options = array
(
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
'recursive'=>2,
'group'=>array('group_id')
);
recursive, conditions and group are at the same level
Simple and sweet code :
$groupBy = 'Patient.group_id';
$options = array (
'conditions' => array
(
'Patient.' . $this->Patient->primaryKey => $id
),
'group'=>array($groupBy)
);

Get data based on other model id Cake php

ServiceController.php
public function admin_index() {
$services=$this->Service->find('all', array('order'=>'Service.id desc'));
$this->set('services', $services);
}
admin_index.ctp(view file)
pr($services)
Array
(
[Service] => Array
(
[id] => 53
[user_id] => 65
[first_name] => client
)
[User] => Array
(
[id] => 65
[user_group_id] => 4
)
[SolutionType] => Array
(
[id] => 6
[solution_type] => face to face interaction
[status] => 0
)
[Service] => Array
(
[id] => 54
[user_id] => 66
[first_name] => client
)
[User] => Array
(
[id] => 66
[user_group_id] => 5
)
[SolutionType] => Array
(
[id] => 6
[solution_type] => face to face interaction
[status] => 0
)
)
Service.php(model file)
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'SolutionType' => array(
'className' => 'SolutionType',
'foreignKey' => 'solution_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
)
I want to make condition in ServicesController show all data where User user_group_id = 5.
Try this
$services=$this->Service->find('all', array('conditions'=>array('User.user_group_id'=>5),
'order'=> array('Service.id' => 'Desc')));

Cakephp 2.0 weird array_merge error when using contain

I have a weird error when using contain to limit my data to only what I need. Here is my contain
$this->Movie->contain(array(
'Review' => array(
'fields' => array('id', 'title', 'spoilers', 'body', 'created'),
'User' => array(
'fields' => array('id', 'username'),
'Profile' => array('fields' => array('id', 'image_thumb_small'))
),
'order' => 'Review.created DESC',
'limit' => 2,
),
'Movieupdates' => array(
'User' => array('Profile' => array('fields' => 'image_thumb_small')),
'order' => 'Movieupdates.created DESC',
'limit' => 2
),
'Actor' => array(
'fields' => array('name', 'slug', 'image_thumb'),
),
'Genre' => array(
'fields' => array('name', 'id'),
)
));
The problem is that Cakephp throws me two errors:
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 400]
Warning (2): array_unique() expects parameter 1 to be array, null given [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 400]
I think the problem has to do with the following line (in contain: Review -> User):
'fields' => array('id', 'username'),
If I remove that everything is fine, but then again I dont want all the data from User, especially not the passwords so I have to limit that.
What is wrong with my Contain? The error suggest that I should be using an array somewhere, but I have no idea where.
Edit added query outcome
Array
(
[Movie] => Array
(
[id] => 27
[user_id] =>
[title] => The Amazing Spider-Man
[slug] => The_Amazing_Spider_Man_2012
[year] => 2012
[summary] => Peter Parker finds a clue that might help him understand why his parents disappeared when he was young.
[poster] => /files/movies/53cb8be658a2e04a21909e288cdcb8.jpg
[poster_thumb] => /files/movies/53cb8be658a2e04a21909e288cdcb8_resized_101x150.jpg
[backdrop] => /files/movies/backdrop/amazing_spider_man.jpg
[rating] => 7.4285714285715
[like_count] => 5
[review_count] => 7
[see_count] => 7
[rating_count] => 14
)
[Movieupdates] => Array
(
[0] => Array
(
[id] => 20
[user_id] => 4
[movie_id] => 27
[type] => 4
[info] => 26
[created] => 2012-12-08 11:06:18
[User] => Array
(
[id] => 4
[username] => Ceriksen
[email] => skdji#gmail.com
[password] => ---
[validation_code] => 082b7735cfb3a3d5c74547d702bd97f9af517870
[group_id] => 3
[created] => 2012-11-27 11:23:57
[modified] => 2012-11-27 11:23:57
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
[1] => Array
(
[id] => 19
[user_id] => 4
[movie_id] => 27
[type] => 3
[info] => 10
[created] => 2012-12-08 11:06:08
[User] => Array
(
[id] => 4
[username] => Ceriksen
[email] => skdji#gmail.com
[password] => ---
[validation_code] => 082b7735cfb3a3d5c74547d702bd97f9af517870
[group_id] => 3
[created] => 2012-11-27 11:23:57
[modified] => 2012-11-27 11:23:57
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
)
[Genre] => Array
(
[0] => Array
(
[name] => Action
[id] => 11
[MoviesGenre] => Array
(
[id] => 56
[movie_id] => 27
[genre_id] => 11
)
)
[1] => Array
(
[name] => Adventure
[id] => 12
[MoviesGenre] => Array
(
[id] => 57
[movie_id] => 27
[genre_id] => 12
)
)
[2] => Array
(
[name] => Fantasy
[id] => 18
[MoviesGenre] => Array
(
[id] => 58
[movie_id] => 27
[genre_id] => 18
)
)
)
[Actor] => Array
(
[0] => Array
(
[name] => Emma
[slug] => Emma_Stone
[image_thumb] => /files/actors/d59efc7614151acb7ea5d08da47112_resized_30x30.jpg
[MoviesActor] => Array
(
[id] => 1
[movie_id] => 27
[actor_id] => 8
)
)
[1] => Array
(
[name] => Andrew
[slug] => Andrew_Garfield
[image_thumb] => /files/actors/0a9354d741328f4cf3e2954641e4eb_resized_25x30.jpg
[MoviesActor] => Array
(
[id] => 2
[movie_id] => 27
[actor_id] => 9
)
)
[2] => Array
(
[name] => Martin
[slug] => Martin_Sheen
[image_thumb] => /files/actors/2383fc87054b2e8b4249fc7e3ffba5_resized_30x27.jpg
[MoviesActor] => Array
(
[id] => 3
[movie_id] => 27
[actor_id] => 10
)
)
)
[Review] => Array
(
[0] => Array
(
[id] => 26
[title] => W00t
[spoilers] => 0
[created] => 2012-12-08 11:06:18
[user_id] => 4
[MoviesReview] => Array
(
[id] => 25
[movie_id] => 27
[review_id] => 26
)
[User] => Array
(
[id] => 4
[username] => Ceriksen
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
[1] => Array
(
[id] => 25
[title] => Testing the update
[spoilers] => 1
[created] => 2012-12-08 11:04:44
[user_id] => 4
[MoviesReview] => Array
(
[id] => 24
[movie_id] => 27
[review_id] => 25
)
[User] => Array
(
[id] => 4
[username] => Ceriksen
[Profile] => Array
(
[id] => 5
[user_id] => 4
[image] => /files/profiles/771a98602dc7849b2d004952f1f35f.jpg
[image_thumb] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_130x130-2.jpg
[image_thumb_medium] =>
[image_thumb_small] => /files/profiles/771a98602dc7849b2d004952f1f35f_resized_30x30.jpg
[name] =>
[last_name] =>
[bio] =>
[country] =>
[city] =>
)
)
)
)
)
Solved it, I had an error in my contain. Stupid one.
'User' => array('Profile' => array('fields' => 'image_thumb_small')),
Should have been:
'User' => array('Profile' => array('fields' => array('image_thumb_small'))),

Deep association relating to top-level model?

I have the following models:
Kin, which you can think of as an animal--a pet.
Species hasAndBelongsToMany Kin
GrowthStage hasAndBelongsToMany Species
Kin hasAndBelongsToMany GrowthStage
and KinGrowthStage, which defines the relationship between a kin and its growth stages (it contains its related images and such for the kin at that growth stage).
So in my controller I'm trying to get the following array to come out the other end. In this scenario the Species => GrowthStage => KinGrowthStages are related to the Kin id:
Array
(
[Kin] => Array
(
[id] => 4
[name] => Hobbes
[generation] => 1
[current_growth_stage_id] => 4
[species_id] => 1
)
[Species] => Array
(
[id] => 1
[name] => Tiger
[GrowthStage] => Array
(
[0] => Array
(
[id] => 5
[name] => cub
[order] => 1
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 4
[image] => /img/hobbes/cub.png
)
)
)
[1] => Array
(
[id] => 2
[name] => juvenile
[order] => 2
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 4
[image] => /img/hobbes/juvenile.png
)
)
)
[2] => Array
(
[id] => 9
[name] => adult
[order] => 3
[KinGrowthStage] => Array
(
// Nothing here because Hobbes hasn't reached this growth stage yet
)
)
)
)
)
What I'm getting is this. The Species => GrowthStage => KinGrowthStages is joining on the GrowthStage id rather than the Kin id:
Array
(
[Kin] => Array
(
[id] => 4
[name] => Hobbes
[generation] => 1
[current_growth_stage_id] => 4
[species_id] => 1
)
[Species] => Array
(
[id] => 1
[name] => Tiger
[GrowthStage] => Array
(
[0] => Array
(
[id] => 5
[name] => cub
[order] => 1
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 1
[kin_id] => 1
[image] => /img/tony/cub.png
)
)
)
[1] => Array
(
[id] => 2
[name] => juvenile
[order] => 2
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 2
[kin_id] => 1
[image] => /img/tony/juvenile.png
)
)
)
[2] => Array
(
[id] => 9
[name] => adult
[order] => 3
[KinGrowthStage] => Array
(
[0] => Array
(
[id] => 1
[growth_stage_id] => 3
[kin_id] => 1
[image] => /img/tony/adult.png
)
)
)
)
)
)
I understand why this happening, I just want to know if there's any way to modify the relationship 'on the fly' and join it on the kin id. I've tried adding the following conditions array to my contain array:
$this->Kin->contain(
array(
'Species' => array(
'GrowthStage' => array(
'KinGrowthStage' => array(
'conditions' => array('Kin.id = KinGrowthStage.kin_id'),
),
'order' => 'GrowthStage.order ASC'
)
)
)
);
But I get an error about 'unknown table Kin'.
Use the Linkable Behavior https://github.com/Terr/linkable
$this->Kin->find('all', array(
'link' => array(
'Species' => array(
'GrowthStage' => array(
'KinGrowthStage' => array(
'conditions' => 'Kin.id = KinGrowthStage.kin_id',
)
)
)
),
'order' => 'GrowthStage.order ASC'
));

Resources