Cakephp: belongsto relation don't working in hasmany relation - php

I have 3 models: messages, forums and users
A forum may have several messages and each message has posted by one user.
I would like to have in my forum model all messages and their owner.
So, in my Forum.php (model), I write:
public $belongsTo=array(
'User' => array(
'className' => 'User',
'foreignKey'=>'id_user'
),
);
public $hasMany=array(
'Message' => array(
'className' => 'Message',
'foreignKey'=>'id_forum'
),
);
and in my Message.php (model) :
public $belongsTo=array(
'User' => array(
'className' => 'User',
'foreignKey'=>'id_user'
),
);
With "debug($this->Forum->find('all'));", I get :
array(
(int) 0 => array(
'Forum' => array(
'id' => '3',
'titre' => 'rooo',
'message' => 'tooo',
'id_user' => '2',
'date_create' => '2014-07-20 17:24:07'
),
'User' => array(
'password' => '*****',
'id' => '2',
'username' => 'member',
'date_sign' => '2014-07-04 11:34:52'
),
'Message' => array(
(int) 0 => array(
'id' => '5',
'message' => 'hi',
'id_user' => '3',
'id_forum' => '3',
'date_add' => '2014-07-20 18:53:51'
)
)
)
)
But with "debug($this->Message->find('all'));", I get :
array(
(int) 0 => array(
'Message' => array(
'id' => '5',
'message' => 'hi',
'id_user' => '3',
'id_forum' => '3',
'date_add' => '2014-07-20 18:53:51'
),
'User' => array(
'password' => '*****',
'id' => '3',
'username' => 'membre2',
'date_sign' => '2014-07-20 18:26:41'
)
)
)
I don't understand why I don't get my user informations on my 1st model but it's working in the 2nd.
Thanks for helping.

You need to set recursive property of Forum modal to 2
$this->Forum->recursive=2;
For more information check here cakephp docs

Related

Saving HABTM associated data in cakephp

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!

How to create a virtual field with containable behavior in cakephp 2.6

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

cant join tables HABTM and many to one ,cakephp

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

cant get data to group in find in a cakephp HABTM

I have a HABTM relationship of Students and Subjects. I simply want to display all the subjects associated with a student without repetition of student details. I dont want the student details to keep repeating in the data output. How do I get the student details as shown below to output once and then all the subjects associated with this student to output?
Group didnt work. I didnt see the answer in the docs but I am sure this is a simple task to do.
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
$this->Student->recursive = -1;
$joinoptions = array(
// $options['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',
)
),
);
$fieldoptions = array('Student.id,Student.last_name,Student.first_name,Student.address_street,Student.address_suburb,'
. 'Subject.name,Subject.id, StudentsSubject.id, Student.first_name,Student.last_name,Student.address_lat,Student.address_long,Student.tutor_gender_preference'
);
$student=$this->Student->find('all',array(
'conditions'=> array( 'Student.id' => $student_id),
'fields'=>$fieldoptions,
'joins'=> $joinoptions,
// 'group' => 'Student.id',
'recursive' =>-1,
));
array(
(int) 0 => array(
'Student' => array(
'id' => '216',
'last_name' => 'Ncc',
'first_name' => 'Acc',
'address_street' => '8 sdsdt',
'address_suburb' => 'Hasdsdk',
'address_lat' => 'xx',
'address_long' => 'xx',
'tutor_gender_preference' => 'No Preference'
),
'Subject' => array(
'name' => 'English: Year 7 - 10',
'id' => '9'
),
'StudentsSubject' => array(
'id' => '531'
)
),
(int) 1 => array(
'Student' => array(
'id' => '216',
'last_name' => 'Ncc',
'first_name' => 'Acc',
'address_street' => '8 sdsdt',
'address_suburb' => 'Hasdsdk',
'address_lat' => 'xx',
'address_long' => 'xx',
'tutor_gender_preference' => 'No Preference'
),
'Subject' => array(
'name' => 'Maths: Year 7 - 10',
'id' => '16'
),
'StudentsSubject' => array(
'id' => '532'
)
),
(int) 2 => array(
'Student' => array(
'id' => '216',
'last_name' => 'Ncc',
'first_name' => 'Acc',
'address_street' => '8 sdsdt',
'address_suburb' => 'Hasdsdk',
'address_lat' => 'xx',
'address_long' => 'xx',
'tutor_gender_preference' => 'No Preference'
),
'Subject' => array(
'name' => 'Physics: Year 11',
'id' => '28'
),
'StudentsSubject' => array(
'id' => '583'
)
)
)

CakePHP and HABTM insert/save doesn't work

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.

Categories