cakephp 2.5 和 3.0 之间的不同查找结果

Different find results between cakephp 2.5 and 3.0

我正在将我的应用程序从 cakephp 2.5 迁移到 3.0,我正在执行 ->find() 方法,然后循环访问控制器中的结果。但是我在 3.0 中得到了不同的结果。我似乎在进入数据库之前获取了 cakephp 使用的查询设置,而不是结果。

EmployeesController.php

2.5代码:

//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
}

结果:

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代码:

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

但我没有得到预期返回的结果,实际值似乎包含在一个名为 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'

}
]

3.0.0 还需要一个步骤吗?它在两者中都找到了两条记录,但是 3.0 的格式非常不同,我不知道如何像在 2.5 中那样访问这些值来执行我的循环。

*****更新

按照建议我已经更新到 Cakephp 3.05。现在所有额外的属性都被隐藏了,我只剩下对象形式的结果。我正在使用 ->toArray() 但它似乎并没有改变对象。

[
(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'

}]

在 2.5 代码中你的条件是 Employee.id => 1,而你在 3.0 查询中的条件是 Employees.user_id => 1

您正在查看自定义格式的调试输出,而不是对象的逻辑表示(例如使用 var_dump() 来查看差异)。

直到最近,当通过 debug() 转储实体时,各种隐藏的属性会变得可见,包括 $_properties 属性,它包含可用属性及其映射值。

你看到这个的事实意味着你的代码库已经过时了,你应该更新它以查看新格式,属性不再嵌套显示,"special" 东西是显示为 [name]https://github.com/cakephp/cakephp/pull/6564

在任何情况下,只要访问 Cookbook 中描述的属性就可以了。

$recentOpen[0]->employee->name

另见 Cookbook > Database Access & ORM > Entities > Accessing Entity Data