尝试获取非对象 cakephp 3.5



我是cakephp 3.5的新手。我最近刚刚读了同样的问题,我使用这些解决方案。问题是我尝试了过去问题中回答的解决方案,但它没有显示所需的属性。

view.ctp (client_transaction(

<div class="related">
    <?php if (!empty($clientTransaction->service_detail)): ?>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th scope="col"><?= __('Service Detail ID') ?></th>
            <th scope="col"><?= __('Barber') ?></th>
            <th scope="col"><?= __('Service') ?></th>
            <th scope="col"><?= __('Price') ?></th>
            <th scope="col" class="actions"><?= __('Actions') ?></th>
        </tr>
        <!--pre>
        <?php print_r($clientTransaction->service_detail); ?>
      </pre-->
        <?php foreach ($clientTransaction->service_detail as $serviceDetail): ?>
        <tr>
            <td><?= $this->Number->format($serviceDetail->id) ?></td>
            <!-- the first solution-->
            <td><?= h($serviceDetail->users->first_name)?></td>
            <!-- the second solution-->
            <td><?= h($serviceDetail->service->name) ?></td>
            <td><?= h($serviceDetail->service->price) ?></td>
            <td class="actions">
                <?= $this->Form->postLink(__('Cancel'), ['controller' => 'ServiceDetail','action' => 'delete', $serviceDetail->id], ['confirm' => __('Are you sure you want to cancel # {0}?', $serviceDetail->id)]) ?>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php endif; ?>
</div>

错误的屏幕截图

我只是在寻求帮助。我也在努力弄清楚。谢谢!

PS:我刚刚编辑了代码和屏幕截图,以便您了解问题。 谢谢!

<?php foreach ($clientTransaction->service_detail as $serviceDetail): ?>
    <tr>
        <td><?= $this->Number->format($serviceDetail->id) ?></td>
        <!-- the first solution-->
        <td><?= $serviceDetail->user!==null ? h($serviceDetail->user->first_name) : ''?></td>
        <!-- the second solution-->
        <td><?= h($serviceDetail['service']['name']) ?></td>
        <td><?= h($serviceDetail['service']['price']) ?></td>
        <td class="actions">
          <!--  <?= $this->Html->link(__('View'), ['controller' => 'Users', 'action' => 'view', $user->id]) ?>
            <?= $this->Html->link(__('Edit'), ['controller' => 'Users', 'action' => 'edit', $user->id]) ?> -->
            <?= $this->Form->postLink(__('Cancel'), ['action' => 'delete', $serviceDetail->id], ['confirm' => __('Are you sure you want to cancel # {0}?', $serviceDetail->id)]) ?>
    </tr>
    <?php endforeach; ?>

那应该是 - 记得删除你的 IF:(我希望这有帮助(

<?php foreach ($clientTransactions as $serviceDetails): ?>
    <?php foreach ($serviceDetails->service_detail as $serviceDetail): ?>
           //Code to excute here
    <?php endforeach; ?>
<?php endforeach; ?>

不:

<?php foreach ($clientTransaction->service_detail as $serviceDetail): ?>

最新更新