I have a little problem. My Commodity model has Has and belongs to many association with User model. In the DB they are related through commodities_users table. So, I want Cakephp to create new records in commodities_users table in DB when the new Commodity was created. But it doesn't work.
My $this->request->data array (when I want saving one new commodity) looks like this
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
(int) 0 => array(
'id' => '23'
),
(int) 1 => array(
'id' => '24'
),
(int) 2 => array(
'id' => '30'
),
(int) 3 => array(
'id' => '31'
)
));
And I am saving the Commodity model this way $this->Commodity->saveAll($this->request->data);
My cakePhp version is 2.4.
The relationship is
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'commodities_users',
'foreignKey' => 'commodity_id',
'associationForeignKey' => 'user_id',
),
);
What am I doing wrong ?
You are not feeding the right data into saveAll().
As per the CakePHP docs, the proper way of structuring your data is as follows:
array(
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
'User' => array('23','24','30','31')
)
)
);
You then save the data with:
$this->Commodity->saveAll($this->request->data, array('deep' => true))
For further reference, see
Saving Related Model Data (HABTM)
Ok, I solved it by myself. I was just so dumm and wrote the HABTM association just in Commodity Model. Adding the same for User Model fixed the problem. And the $this->reuqest->data array looked so
array(
'Commodity' => array(
'commodity_type' => '0',
'commoditygroup_id' => '',
'name' => 'asdfad',
'code' => '',
'ean' => '',
'costprice' => '',
'saleprice' => '12512,123',
'default_vatrate' => '',
'saleprice_gross' => '',
'sync_cashconnector' => '1',
'commoditysetting_id' => '',
'default_amount_enabled' => '0',
'default_amount' => '',
'stock_min' => '',
'stock' => '',
'comment' => ''
),
'User' => array(
'id' => array(
'0' => '23',
'1' => '24',
'2' => '25'
));
Thanks for your time!
Related
I have this code in model User:
$hasOne = array(
'Member' => array(
'className' => 'Member',
'foreignKey' => 'user_id',
'dependent' => true
)
);
function rest_findUserById($id = NULL) {
$row = $this->find(
'first',
array(
'contain' => array(
'UserSession'=>array(
'fields'=>array('user_id', 'session_token')
) ,
'Member' => array(
'fields' => array("*")
)
),
'fields' => array('username', 'email'),
'conditions' => array('User.id' => $id)
));
if (!$row) {
return FALSE;
}
debug($row);exit;
}
and in model Member:
$virtualFields = array(
'full_name' => 'CONCAT(Member.first_name, " ",Member.last_name)'
);
$belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
?>
When I call fuction rest_findUserById() I get this output:
/app/Model/User.php (line 378)
array(
'User' => array(
'username' => 'testuser',
'email' => 'test#gmail.com',
'id' => '71'
),
'Member' => array(
'id' => '11',
'user_id' => '71',
'first_name' => 'Whjc',
'last_name' => 'test_user_lname',
'email' => '',
'phone' => '778899554455',
'image_dir' => '11',
'image' => '92e20fd0260dcc5ea289611221723b6a.jpg',
'address_line_1' => 'Shhd',
'address_line_2' => 'sdf',
'city' => 'Los Angeles Area',
'state' => 'delhi',
'country_id' => '0',
'zip_code' => '56456',
'is_address_different' => false,
'date_of_birth' => '0000-00-00',
'referred_by' => 'asdf',
'created' => '2016-07-27 13:52:30',
'created_by' => '3',
'modified' => '2016-08-02 12:25:22',
'modified_by' => '3',
'status' => false
),
'UserSession' => array(
(int) 0 => array(
'user_id' => '71',
'session_token' => 'a685b3db5ab87a928716c7d8b3272572'
)
)
)
but when I call this code:
$this->Member->recursive = -1;
debug($this->Member->find('first'));
I get this output:
/app/Model/User.php (line 377)
array(
'Member' => array(
'id' => '4',
'user_id' => '64',
'first_name' => 'SDFS SDF',
'last_name' => 'test_user_lname',
'email' => '',
'phone' => '778899554455',
'image_dir' => '',
'image' => '',
'address_line_1' => 'sdf',
'address_line_2' => 'sdf',
'city' => 'Los Angeles Area',
'state' => 'delhi',
'country_id' => '0',
'zip_code' => '56456',
'is_address_different' => false,
'date_of_birth' => '1970-01-01',
'referred_by' => 'asdf',
'created' => '2016-07-22 15:25:30',
'created_by' => '0',
'modified' => '2016-07-22 15:25:30',
'modified_by' => '0',
'status' => false,
'full_name' => 'SDFS SDF test_user_lname'
)
)
as you can see the virtual field i.e.'full_name' is coming in the second output but not in first output.
How can I fix this?
From the manual
you cannot use virtualFields on associated models
the proposed solution is to copy the virtual field into your model, this way
/app/Model/User.php
$this->virtualFields['member_full_name'] = $this->Member->virtualFields['full_name'];
Of course this does not work for hasMany or HABTM relationships. Also note that in this way you'll find the virtual field in the User data array and not in the Member data array
I cant use a join to get the required data from the table relationship of
Student HABTM Subject, and
Guardian 1 to many Student
Without giving all the code,my find gets the required data but it adds another table (AvailabilityForStudent)which has a HABTM relationship with Student, along with other fields. I simply get too much data.
I have to add Guardian2 to the Guardian table to avoid a conflict which i dont understand.
What is the correct join to display data from 3 tables only?
$students = $this->find('all', array(
'joins'=>$joins,
'conditions' => $conditions,
'fields'=> $fields,
'order'=>$order,
));
$joins = array(
array('table' => 'students_subjects',
'alias' => 'StudentsSubject',
'type' => 'LEFT',
'conditions' => array(
'Student.id=StudentsSubject.student_id',
)
),
array('table' => 'subjects',
'alias' => 'Subject',
'type' => 'LEFT',
'conditions' => array(
'StudentsSubject.subject_id=Subject.id',
)
),
array('table' => 'guardians',
'alias' => 'Guardian2',
'type' => 'LEFT',
'conditions' => array(
'Student.guardian_id=Guardian2.id',
)
),
);
(int) 0 => array(
'Student' => array(
'id' => '267',
'last_name' => 'xxx',
'first_name' => 'xxx',
'address_suburb' => 'xxx',
'student_inactive' => false,
'student_mobile' => '0'
),
'Guardian' => array(
'guardian_last_name' => 'xx',
'guardian_first_name' => 'xxxx',
'id' => '267',
'guardian_mobile' => 'xxxx',
'guardian_email' => 'xx#yahoo.com.au'
),
'Subject' => array(
'name' => 'English: Year 7 - 10',
(int) 0 => array(
'id' => '9',
'name' => 'English: Year 7 - 10',
'StudentsSubject' => array(
'id' => '1079',
'student_id' => '267',
'subject_id' => '9',
'created' => null,
'modified' => null
)
)
),
'StudentsSubject' => array(
'id' => '1079'
),
'AvailabilityForStudent' => array(
(int) 0 => array(
Update- added this line $this->recursive = -1 instead of $this->Student->recursive = -1; and it works
Update- added this line $this->recursive = -1 instead of $this->Student->recursive = -1; and it works
I have Story related to a Chapter with a many to many relation
I have a StoryChapter Model .
I have this find all stories result :
array(
(int) 0 => array(
'Story' => array(
'id' => '111',
'title' => 'First Story',
'question' => 'What do you want ?',
'description' => 'ezrsrfgq ergtqergq',
'date' => '2014-06-10',
'image' => '/uploads/stories/111.jpg',
'created' => '2014-06-10 07:51:35',
'modified' => '2014-06-13 12:45:43',
'created_by' => '1',
'original' => null,
'tags' => ''
),
'StoryChapter' => array(
(int) 0 => array(
'id' => '110',
'story_id' => '111',
'chapter_id' => '81',
'chapter_title' => 'Second Chapter',
'created' => '2014-06-11 00:00:00'
),
(int) 1 => array(
'id' => '109',
'story_id' => '111',
'chapter_id' => '80',
'chapter_title' => 'First Chapter',
'created' => '2014-06-13 00:00:00'
)
),
'StoryUser' => array(),
'StoryGroup' => array(),
'Favorite' => array(),
'Tag' => array()
),
(int) 1 => array(
'Story' => array(
'id' => '112',
'title' => 'Second Story',
'question' => 'What do you want ?',
'description' => 'edghs rthsghsx ghs rhsgrhsrtgh',
'date' => '2014-06-13',
'image' => '/uploads/stories/112.jpg',
'created' => '2014-06-13 07:43:18',
'modified' => '2014-06-13 07:43:18',
'created_by' => '1',
'original' => null,
'tags' => ''
),
'StoryChapter' => array(),
'StoryUser' => array(),
'StoryGroup' => array(),
'Favorite' => array(),
'Tag' => array()
)
)
I want the find function to order only the StoryChapter by created desc without affecting the order of the found stories .
I hope you understand what I mean .
Thank you
I solved the problem by adding the order in the hasMany array in the Story model
public $hasMany = array(
'StoryChapter' => array(
'className' => 'StoryChapter',
'foreignKey' => 'story_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => 'created ASC',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
I'm working on website using cakephp , the project work perfectly on localhost but when i upload to my CONTABO VPS i found a problem in the fonction (hasmany) i.e i lose relation ships between models
Model example :
public $belongsTo = array(
'category' => array(
'className' => 'categories',
'foreignKey' => 'id_category'
)
);
public $hasMany = array(
'model_globale' => array(
'className' => 'model_globales',
'foreignKey' => 'id_type'
),
'typejobe' => array(
'className' => 'typejobes',
'foreignKey' => 'id_type'
)
);
so whene i debug on localhost i get this :
\app\Controller\ModelesController.php (line 70)
array(
(int) 0 => array(
'type' => array(
'id' => '1',
'name' => 'carte de visite',
'id_category' => '1',
'created' => '0000-00-00 00:00:00'
),
'category' => array(
'id' => '1',
'name' => 'Imprimerie',
'created' => '0000-00-00 00:00:00'
),
'model_globale' => array(
(int) 0 => array(
'id' => '22',
'id_type' => '1',
'id_job' => '1',
'id_chart_graphique' => '0',
'id_user' => '8',
'id_subtype' => '3',
'source' => '0',
'created' => '2014-01-07 09:57:39',
'modified' => '2014-01-07 09:57:39'
)
),
'typejobe' => array(
(int) 0 => array(
'id_type' => '1',
'id_jobe' => '1'
),
(int) 1 => array(
'id_type' => '1',
'id_jobe' => '2'
),
(int) 2 => array(
'id_type' => '1',
'id_jobe' => '5'
)
)
)
)
but on my CONTABO VPS when i debug i get only :
array(
(int) 0 => array(
'type' => array(
'id' => '1',
'name' => 'carte de visite',
'id_category' => '1',
'created' => '0000-00-00 00:00:00'
)
)
)
The main problem when you hosted your projects is the change in system.
WAMP localhost: not case sensitive
LINUX server: case sensitive
90% of problems on migration is due to this problem. Thank you check for missing caps or do you have a set without purpose.
PS: There is also the recursive property that you must check.
I'm stuck in Cakephp insert, here is what I have
-Order HABTM Address
-Address model
Here is my association in Address model :
public $hasAndBelongsToMany = array(
'Address' => array(
'className' => 'Address',
'joinTable' => 'address_customers',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'address_id'
)
);
I want to save/insert a new customer with one new address
So, I thought the array below would work :
array(
'Customer' => array(
'nom' => 'Khiami',
'prenom' => 'TEST',
'tel' => '0945454545',
'ptel' => '',
'commentaire' => '',
'email' => '',
'anniversaire' => array(
'day' => '08',
'month' => '12',
'year' => '2012'
),
'createdFrom' => 's'
),
'Address' => array(
(int) 0 => array(
'adresse' => 'ADDR TEST',
'cp_id' => '1',
'etage' => '',
'residence' => '',
'batiment' => '',
'appartement' => '',
'type' => 'l',
'code_sonette' => '',
'code_portail' => '',
'code_batiment' => '',
'code_asc' => ''
)
)
)
The only thing that is working is when I save the Customer first and then use the array below :
array(
(int) 0 => array(
'Customer' => array(
'customer_id' => '394'
),
'Address' => array(
'adresse' => 'ADDR TEST',
'cp_id' => '1',
'etage' => '',
'residence' => '',
'batiment' => '',
'appartement' => '',
'type' => 'l',
'code_sonette' => '',
'code_portail' => '',
'code_batiment' => '',
'code_asc' => ''
)
)
)
But, I still don't have the association table filled ! It only adds an entry in the Address table.
Yes you should specify it in both:
Address model:
public $hasAndBelongsToMany = array(
'Customer' => array(
'className' => 'Customer',
'joinTable' => 'address_customers',
'foreignKey' => 'address_id',
'associationForeignKey' => 'customer_id'
));
Customer Model:
public $hasAndBelongsToMany = array(
'Address' => array(
'className' => 'Address',
'joinTable' => 'address_customers',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'address_id'
));
Since you specify the jointable in your HABTM relationship I doubt its a naming convention problem.
Are you saving in the Address or the Customer model ? Because if you are calling it from the Address model I suppose there should already be a customer, in that case you only have to set:
$this->request->data['Customer']['Customer'] = $customer_id;
And save the address data, it should create the HABTM association in the table.