试图获得非对象cakephp 3.5

时间:2018-03-07 21:49:26

标签: cakephp-3.x cakephp-3.4

我是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>

The screenshot of the error

我只是在寻求帮助。我也想弄明白。谢谢!

P.S:我刚编辑了代码和截图,以便您了解问题。谢谢!

1 个答案:

答案 0 :(得分:0)

<?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): ?>