Reputation: 14141
I am migrating my app from cakephp 2.5 to 3.0 and I am doing a ->find()
method and then looping over the results in the controller. However I am getting different results returned in 3.0. I seem to be getting the query settings that cakephp uses before going to the database rather than the results.
EmployeesController.php
2.5 Code:
//Open courses
$options = array(
'conditions' => array('Employee.id' => 1, 'CoursesEmployee.completed' => false),
'limit' => 3
);
$recentOpen = $this->CoursesEmployee->find('all', $options);
debug($recentOpen);
// get next module for each open course
foreach ($recentOpen as $key => &$value) {
//do something
}
and the results:
array(
(int) 0 => array(
'CoursesEmployee' => array(
'id' => '1',
'employee_id' => '1',
'course_id' => '1',
'course_module_id' => '5',
'progress' => '10',
'modified' => '2014-12-16 22:40:42',
'created' => '2014-11-18 00:00:00',
'completed' => false
),
'Employee' => array(
'id' => '1',
'user_id' => '1',
'hotel_id' => '1',
'name' => 'Keith Power',
'email' => '',
'surname' => 'Power',
'employee_num' => '',
'modified' => '2014-11-19 12:52:36',
'created' => '2014-11-18 00:00:00'
),
'Course' => array(
'id' => '1',
'name' => 'Manual Handling Training',
'course_lenght' => '02:12:00'
),
'CourseModule' => array(
'id' => '5',
'course_id' => '1',
'name' => 'Module 5',
'type' => 'video',
'video_url' => '',
'video_lenght' => '00:00:00'
)
),
(int) 1 => array(
'CoursesEmployee' => array(
'id' => '3',
'employee_id' => '1',
'course_id' => '2',
'course_module_id' => '5',
'progress' => '100',
'modified' => '2014-12-05 15:13:47',
'created' => '2014-11-20 00:00:00',
'completed' => false
),
'Employee' => array(
'id' => '1',
'user_id' => '1',
'hotel_id' => '1',
'name' => 'Keith Power',
'email' => '',
'surname' => 'Power',
'employee_num' => '',
'modified' => '2014-11-19 12:52:36',
'created' => '2014-11-18 00:00:00'
),
'Course' => array(
'id' => null,
'name' => null,
'course_lenght' => null
),
'CourseModule' => array(
'id' => '5',
'course_id' => '1',
'name' => 'Module 5',
'type' => 'video',
'video_url' => '',
'video_lenght' => '00:00:00'
)
)
)
3.0 Code:
$this->loadModel('CoursesEmployees');
//Open courses
$options = [
'conditions' => ['Employees.id' => 1, 'CoursesEmployees.completed' => false],
'limit' => 3,
'contain' => 'Employees'
];
$recentOpen = $this->CoursesEmployees->find('all', $options)->toArray();
debug($recentOpen);
But I do not get results returned expected, the actual values seem contained inside an array called properties
there is no mention the in cake book about accessing properties:
[
(int) 0 => object(App\Model\Entity\CoursesEmployee) {
'new' => false,
'accessible' => [
'employee_id' => true,
'course_id' => true,
'course_module_id' => true,
'progress' => true,
'completed' => true,
'employee' => true,
'course' => true,
'course_module' => true
],
'properties' => [
'id' => (int) 1,
'employee_id' => (int) 1,
'course_id' => (int) 1,
'course_module_id' => (int) 5,
'progress' => (int) 10,
'modified' => object(Cake\I18n\Time) {
'time' => '2014-12-16T22:40:42+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'created' => object(Cake\I18n\Time) {
'time' => '2014-11-18T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'completed' => false,
'employee' => object(App\Model\Entity\Employee) {
'new' => false,
'accessible' => [
'user_id' => true,
'hotel_id' => true,
'name' => true,
'email' => true,
'surname' => true,
'employee_num' => true,
'hotel' => true,
'courses' => true
],
'properties' => [
'id' => (int) 1,
'user_id' => (int) 1,
'hotel_id' => (int) 1,
'name' => 'Keith Power',
'email' => '',
'surname' => 'Power',
'employee_num' => '',
'modified' => object(Cake\I18n\Time) {
'time' => '2014-11-19T12:52:36+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'created' => object(Cake\I18n\Time) {
'time' => '2014-11-18T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Employees'
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'CoursesEmployees'
},
(int) 1 => object(App\Model\Entity\CoursesEmployee) {
'new' => false,
'accessible' => [
'employee_id' => true,
'course_id' => true,
'course_module_id' => true,
'progress' => true,
'completed' => true,
'employee' => true,
'course' => true,
'course_module' => true
],
'properties' => [
'id' => (int) 3,
'employee_id' => (int) 1,
'course_id' => (int) 2,
'course_module_id' => (int) 5,
'progress' => (int) 100,
'modified' => object(Cake\I18n\Time) {
'time' => '2014-12-05T15:13:47+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'created' => object(Cake\I18n\Time) {
'time' => '2014-11-20T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'completed' => false,
'employee' => object(App\Model\Entity\Employee) {
'new' => false,
'accessible' => [
'user_id' => true,
'hotel_id' => true,
'name' => true,
'email' => true,
'surname' => true,
'employee_num' => true,
'hotel' => true,
'courses' => true
],
'properties' => [
'id' => (int) 1,
'user_id' => (int) 1,
'hotel_id' => (int) 1,
'name' => 'Keith Power',
'email' => '',
'surname' => 'Power',
'employee_num' => '',
'modified' => object(Cake\I18n\Time) {
'time' => '2014-11-19T12:52:36+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'created' => object(Cake\I18n\Time) {
'time' => '2014-11-18T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Employees'
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'CoursesEmployees'
}
]
Is there another step required in 3.0. It is finding the two records in both, but the format of 3.0 is so different I don't know how to access the values like I did in 2.5 to do my loop.
*****Update
As suggested I have updated to Cakephp 3.05. Now all the extra properties are hidden and I am left with the results in object form. I am using ->toArray()
but it does not seem to change the object.
[
(int) 0 => object(App\Model\Entity\CoursesEmployee) {
'id' => (int) 1,
'employee_id' => (int) 1,
'course_id' => (int) 1,
'course_module_id' => (int) 5,
'progress' => (int) 10,
'modified' => object(Cake\I18n\Time) {
'time' => '2014-12-16T22:40:42+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'created' => object(Cake\I18n\Time) {
'time' => '2014-11-18T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'completed' => false,
'[new]' => false,
'[accessible]' => [
'employee_id' => true,
'course_id' => true,
'course_module_id' => true,
'progress' => true,
'completed' => true,
'employee' => true,
'course' => true,
'course_module' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'CoursesEmployees'
},
(int) 1 => object(App\Model\Entity\CoursesEmployee) {
'id' => (int) 2,
'employee_id' => (int) 1,
'course_id' => (int) 3,
'course_module_id' => (int) 8,
'progress' => (int) 100,
'modified' => object(Cake\I18n\Time) {
'time' => '2014-12-08T00:07:18+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'created' => object(Cake\I18n\Time) {
'time' => '2014-11-20T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'completed' => true,
'[new]' => false,
'[accessible]' => [
'employee_id' => true,
'course_id' => true,
'course_module_id' => true,
'progress' => true,
'completed' => true,
'employee' => true,
'course' => true,
'course_module' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'CoursesEmployees'
}]
Upvotes: 2
Views: 850
Reputation: 60463
You are looking at custom formatted debug output, not a logical representation of the object (use for example var_dump()
to see the difference).
Until recently, when dumping an entity via debug()
, various hidden properties were being made visible, including the $_properties
property, which holds a map of the available properties and their values.
The fact that you're seeing this, means that your codebase is outdated, you should update it to see the new format, where the properties are no longer being shown nested anymore, and "special" stuff is being shown as [name]
: https://github.com/cakephp/cakephp/pull/6564
In any case, just access the properties as described in the Cookbook and everything's fine.
$recentOpen[0]->employee->name
See also Cookbook > Database Access & ORM > Entities > Accessing Entity Data
Upvotes: 1
Reputation: 6767
In 2.5 code your condition is Employee.id => 1
, whereas your condition in 3.0 query is Employees.user_id => 1
Upvotes: 0