【问题标题】:Different find results between cakephp 2.5 and 3.0cakephp 2.5 和 3.0 之间的查找结果不同
【发布时间】:2015-05-26 21:46:20
【问题描述】:

我正在将我的应用程序从 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'

}]

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

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

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

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

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

    $recentOpen[0]->employee->name
    

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

    【讨论】:

    • 谢谢,我已经更新了 cakephp,现在我得到了对象形式的结果。我已经阅读了 Cookbook,但我仍然很困惑我会如何使用 $entity-> 我试图替换 $this-> 但我确定这不是使用它的方式。
    • @KeithPower 抱歉,我不太明白您的问题。只需使用您的 $recentOpen 变量,对其进行迭代,或通过它们在数组中的索引直接访问实体对象,或者...我已经更新了示例。
    • 完美,我仍然认为它是一个数组,而不是作为一个对象访问它->我现在可以遍历结果了,谢谢。
    【解决方案2】:

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

    【讨论】:

    • 是的,你是对的,我在 3.0 中添加了 auth,所以我改为使用 user_id。我已经更新了代码以消除混淆。结果是一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2015-12-22
    • 2018-07-30
    • 2020-11-03
    相关资源
    最近更新 更多