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

时间:2015-05-26 21:46:20

标签: cakephp cakephp-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中是否还需要另一个步骤。它在两者中找到了两个记录,但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 个答案:

答案 0 :(得分:1)

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

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

您看到这一点的事实意味着您的代码库已过时,您应该更新它以查看新格式,其中属性不再被嵌套显示,并且“特殊”内容显示为{ {1}}: https://github.com/cakephp/cakephp/pull/6564

在任何情况下,只需按照食谱中的描述访问属性,一切都很好。

[name]

另请参阅 Cookbook > Database Access & ORM > Entities > Accessing Entity Data

答案 1 :(得分:0)

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