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));
Related
When I fetch records using hasAndBelongsToMany it sends me the following output:
[Location] => Array
(
[id] => 1
[uuid] => 5a8e4d7f-e1d0-11e4-9567-c03fd5623744
[name] => chandigarh
[address_line1] => dummy
[address_line2] => dummy
[latitude] => 30.737780
[longitude] => 76.784439
[timezone] => UTC
[is_deleted] => 0
[created] => 2015-04-13 16:59:31
[modified] => 2015-04-13
[entry_ts] => 2015-04-13 16:59:31
)
[Department] => Array
(
[0] => Array
(
[dept_name] => Heater
[LocationDepartment] => Array
(
[id] => 1
[location_id] => 1
[department_id] => 1
[is_deleted] => 0
[created] => 2015-04-13 17:00:50
[modified] => 2015-04-13 17:00:50
[entry_ts] => 2015-04-13 17:00:50
)
)
)
But I don't need the LocationDepartment array. Can any one help me?
If your using a quick find you can do this before you do it
$this->Location->recursive = 1;
$this->Location->findById($id);
If your using a full find you can add in recursive as part of the passed parameters
$this->Location->find('first', array('conditions' => array('id' => $id), 'recursive' => 1));
Posting your sample code will help solve something this simple
You can also include the 'containable' behavior on the Model which will let you specify exactly what models/data to retrieve back.
class Location extends AppModel {
var $actsAs = array('Containable');
function GetRecord($id) {
return $this->find('first', array(
'conditions' => array(
'id' => $id,
),
'contain' => array('Department'),
);
}
}
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));
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')));
In cakephp I have codesets and codesetitems. codesetitems belong to codesets so in my codesetitems I have belongsTo = 'Codeset'. But in my view I don't seem to be able to call $codeset['Codesetitem']['id']. It says undefined index Codesetitem. I already checked out the cake documentation. One codeset can have many codesetitems.
Explanation about how cakephp handle and output array of result.
In order to get associated result you must define it in each model as below.
CodesetItem Model
<?php
class CodesetItem extends AppModel
{
var $name = 'CodesetItem';
var $belongsTo = array
(
'Codeset' => array
(
'className' => 'Codeset',
'foreignKey' => 'codeset_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
?>
Codeset Model
<?php
class Codeset extends AppModel
{
var $name = 'Codeset';
var $hasMany = array
(
'CodesetItem' => array
(
'className' => 'CodesetItem',
'foreignKey' => 'codeset_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
?>
Codeset Controller
<?php
class CodesetsController extends AppController
{
var $name = 'Codesets';
function beforeFilter()
{
parent::beforeFilter();
}
function index()
{
$codesets = $this->Codeset->find('first');
pr($codesets);
exit;
}
}
?>
Above will out put array of codeset with index of 0 means as below
Array
(
[Codeset] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[CodesetItem] => Array
(
[0] => Array
(
[id] => 123
[codeset_id] => 121
[title] => On Gwoo the Kungwoo
[body] => The Kungwooness is not so Gwooish
[created] => 2006-05-01 10:31:01
)
[1] => Array
(
[id] => 124
[codeset_id] => 123
[title] => More on Gwoo
[body] => But what of the ‘Nut?
[created] => 2006-05-01 10:41:01
)
)
)
but when you use find('all') in find method it will out as below.
Array
(
[0] => Array
(
[Codeset] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[CodesetItem] => Array
(
[0] => Array
(
[id] => 123
[codeset_id] => 121
[title] => On Gwoo the Kungwoo
[body] => The Kungwooness is not so Gwooish
[created] => 2006-05-01 10:31:01
)
[1] => Array
(
[id] => 124
[codeset_id] => 121
[title] => More on Gwoo
[body] => But what of the ‘Nut?
[created] => 2006-05-01 10:41:01
)
)
)
[1] => Array
(
[Codeset] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[CodesetItem] => Array
(
[0] => Array
(
[id] => 123
[codeset_id] => 121
[title] => On Gwoo the Kungwoo
[body] => The Kungwooness is not so Gwooish
[created] => 2006-05-01 10:31:01
)
[1] => Array
(
[id] => 124
[codeset_id] => 121
[title] => More on Gwoo
[body] => But what of the ‘Nut?
[created] => 2006-05-01 10:41:01
)
)
)
)
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'))),