简体   繁体   English

迁移cakephp 2.5代码以使用cakephp 3.0对象

[英]migrating cakephp 2.5 code to use cakephp 3.0 objects

I migrating a 2.5 app to cakePHP 3.0 and I am updating my code to use the ORM objects after find() rather than the arrays. 我将2.5应用程序迁移到cakePHP 3.0,并且正在更新代码以在find()之后使用ORM对象,而不是使用数组。

In my 2.5 view I do an array find and count the results. 在我的2.5视图中,我进行数组查找并计算结果。 But I cannot seem to access the object level I need to do the count in the new code. 但是我似乎无法访问需要在新代码中进行计数的对象级别。

2.5 Code: 2.5代码:

<?php foreach ($recentEmployees as $recentEmployee): ?>
    <tr>
        <td>
            <?php echo ($recentEmployee['Employee']['name']); ?>
        </td>
        <td><?php echo h($this->Time->format('d/m/Y', $recentEmployee['Employee']['created'])); ?></td>
        <td>
            <?php
                $completedCourses = count(
                                        array_filter(           
                                            $recentEmployee['Course'], 
                                            function($item){return $item['CoursesEmployee']['completed'];}  
                                                    )
                                        );
                $totalCourse = count($recentEmployee['Course']);      
            ?>
            <span class="label <?php echo($label); ?>">
            <?php 
                echo ($completedCourses); 
            ?>      
            /<?php echo ($totalCourse); ?>
        </td>
    </tr>
<?php endforeach; ?>

Now my 3.0 code and the object I am trying access and count: 现在我的3.0代码和我尝试访问和计数的对象:

<?php foreach ($recentEmployees as $recentEmployee): ?>
    <tr>
        <td><?= h($recentEmployee->name) ?></td>
        <td><?= h($this->Time->format($recentEmployee->created, 'd/M/Y')) ?></td>
        <td><?= debug($recentEmployee->courses) ?></td>
    </tr>
<?php endforeach; ?>

Object: 宾语:

[
(int) 0 => object(App\Model\Entity\Course) {

    'id' => (int) 1,
    'name' => 'Manual Handling Training',
    'course_lenght' => object(Cake\I18n\Time) {

        'time' => '2015-05-28T02:12:00+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    '_joinData' => object(App\Model\Entity\CoursesEmployee) {

        'course_id' => (int) 1,
        'id' => (int) 1,
        'employee_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'

    },
    '[new]' => false,
    '[accessible]' => [
        'name' => true,
        'course_lenght' => true,
        'course_files' => true,
        'course_modules' => true,
        'employees' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Courses'

},
(int) 1 => object(App\Model\Entity\Course) {

    'id' => (int) 3,
    'name' => 'Treacys Hotel Induction Training',
    'course_lenght' => object(Cake\I18n\Time) {

        'time' => '2015-05-28T01:25:00+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    '_joinData' => object(App\Model\Entity\CoursesEmployee) {

        'course_id' => (int) 3,
        'id' => (int) 2,
        'employee_id' => (int) 1,
        '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'

    },
    '[new]' => false,
    '[accessible]' => [
        'name' => true,
        'course_lenght' => true,
        'course_files' => true,
        'course_modules' => true,
        'employees' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Courses'

},
]

I have tried this but it is a bad mess of objects and arrays 我已经尝试过了,但是对象和数组很烂

$completedCourses = count(
                        array_filter(
                            $recentEmployee->courses, 
                            function($item){return $item['CoursesEmployee']['completed'];}  
                                    )
                            );

****UPDATE **** UPDATE

I have accessed the data by pointing in _jointable. 我已经通过指向_jointable访问数据。 This is surely not the best practice? 这肯定不是最佳实践吗?

 <?= $completedCourses = count(
                            array_filter(
                                $recentEmployee->courses,
                                function($item){return $item['_joinData']['completed'];}
                                        )
                            );?>

Seems okay to me, but you could do the count via find in your action code. 对我来说似乎还可以,但是您可以通过在操作代码中进行查找来进行计数。

to avoid the join you could use the object access method... 为了避免联接,您可以使用对象访问方法...

<?= $completedCourses = count(
                            array_filter(
                                $recentEmployee->courses,
                                function($item){return $item->completed;}
                                        )
                            );?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM