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'
)
)
)
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 some difficulties to merge many multidimensional array in php. I tried to do it by many way, but each time, I don't get the result wanted. I tried with array_merge(array_unique,...) and in different post I found a way with array_map, but I don't understand everything...
I can have many multi array like below:
array(
(int) 0 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'AM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
),
(int) 1 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'PM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
)
And I would like to get this kind of array :
array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'Full day'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
Do you have some advices to give me to get this result ?
Thank you very much !
I don't know if the best solution but it seems to work like this, and fastly :
foreach ($users as $k=>$v){
//$r[$k] = array_merge($v,$users[$k]);
//$unique[] = array_map("unserialize", array_unique(array_map("serialize", $users[$k])));
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => $v['Calendar']['period']
);
if ($k > 0) {
if (in_array($v['User']['username'],$s[$k])) {
unset($s[$k-1]);
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => "FD"
);
}
}
}
Do you have another idea or this one is enough good ?
thank you !
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
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' => ''
),