![](/img/trans.png)
[英]Migrating existing cakephp 3.0 database to postgres from mysql
[英]migrating cakephp 2.5 code to use cakephp 3.0 objects
我將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'];}
)
);?>
對我來說似乎還可以,但是您可以通過在操作代碼中進行查找來進行計數。
為了避免聯接,您可以使用對象訪問方法...
<?= $completedCourses = count(
array_filter(
$recentEmployee->courses,
function($item){return $item->completed;}
)
);?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.