Get data based on other model id Cake php - cakephp

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')));

Related

How to use saveAll with associated datas?

I have a attributes_products and a products table, i don't know why but my data are saved twice (for the attributes_products_table). so if i go to the product page i will insert one row, i will get one data but if there is one data for example, cake will save this data twice plus the new one. Thanks
if($this->request->is('post') || $this->request->is('put') ){
$d = $this->request->data;
if($this->Product->saveall($d, array('deep' => 'true'))){
$this->Session->setFlash("Le produit a bien été enregistré","notif");
}
}
Product Model ;
public $hasMany = array(
'AttributsProduct' => array(
'className' => 'AttributsProduct',
'foreignKey' => 'product_id',
'dependent' => false
),
'ImagesProduct' => array(
'className' => 'ImagesProduct',
'foreignKey' => 'product_id',
'dependent' => false
)
);
Array
(
[Product] => Array
(
[company_id] => 18
[name] => iWatch
[category_id] => 1
[description] => iWatch
[id] => 4
[main_image_file] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
[AttributsProduct] => Array
(
[1] => Array
(
[attribut_id] => 2
[description] => sceen
)
[2] => Array
(
[attribut_id] => 1
[description] => monitor
)
)
)
The new generated array for the HABTM record needs to know the existent id if it already exists, or a null if it's supposed to create a new record. In any case, it should have the belongsTo id as well for both tables in the relationship.

Cakephp: Contain Behavior, conditions on array of array

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));

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));

CakePHP: HABTM relationship returns join table as an object - how do I get rid of it?

I have a 2 CakePHP models, Articles and Categories, attached using a hasAndBelongsToMany relationship, like so:
$category = new Category();
$category->bindModel(array('hasAndBelongsToMany' => array(
'Article' => array(
'className' => 'Article',
'joinTable' => 'articles_categories',
'foreignKey' => 'category_id',
'associationForeignKey' => 'article_id',
'fields' => 'id,name'
))));
$this->set('categories', $category->find('all',
array(
'fields' => 'id,name'
)));
...but then, when I print out $categories, I get the following:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 31
[name] => category name
[article_count] => 1
)
[Article] => Array
(
[0] => Array
(
[id] => 1445
[name] => article name
[teaser] =>
[author_id] => 3
[ArticlesCategory] => Array
(
[id] => 6634
[article_id] => 1445
[category_id] => 31
[author_id] => 3
[created] => 2014-03-10 12:27:26
[modified] => 2014-03-10 12:27:26
)
)
)
)
I really don't need the [ArticlesCategory] member of [Article]. This just leads me back to information I already have. I tried limiting recursion but that didn't help.
How would I get rid of this?
You have two options:
[1] Reduce recursive value to 0 (CakePHP default: 1)
$category->recursive = 0;
$category->find('all',
array(
'fields' => 'id,name'
))
[2] Start using ContainableBehaviour (my own preference), which gives you more control over model data retrieved
Add following to AppModel for App-wide coverage:
public $actsAs = array('Containable');
The find method becomes:
$category->find('all',
array(
'fields' => 'id, name',
'contain' => 'Article'
))
That should do it!

cakephp model association (view)

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
)
)
)
)

Resources