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

时间:2015-05-28 11:42:46

标签: php cakephp cakephp-3.0

我将2.5应用程序迁移到cakePHP 3.0并且我正在更新我的代码以使用find()之后的ORM对象而不是数组。

在我的2.5视图中,我做了一个数组查找并计算结果。但我似乎无法访问我需要在新代码中进行计数的对象级别。

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; ?>

现在我的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; ?>

对象:

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

},
]

我试过这个,但它是一堆乱七八糟的对象和数组

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

**** UPDATE

我通过指向_jointable来访问数据。这肯定不是最好的做法吗?

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

1 个答案:

答案 0 :(得分:1)

似乎没关系,但你可以通过动作代码中的find来计算。

为了避免加入,你可以使用对象访问方法......

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