如果 table 为空,如何显示 'No Record found'?

How to show 'No Record found' if table is empty?

我是 cakephp 3 的新手。现在,我正在从成功显示所有 table 数据的数据库中获取数据,但如果 table 为空,我想显示 'No record found' 消息.那么,我该怎么做呢?请帮助我。

控制器:PostsController

<?php
    namespace App\Controller;
    use App\Controller\AppController;

    Class PostsController extends AppController {
        Public function index(){
            $this->set('data',$this->Posts->find('all'));
        }
    }
?>

布局:Index.cpt

<div class="row">
    <table class="table">
        <thead>
            <tr>
                <th>Title</th>
                <th>Description</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php
                if(!empty($data))
                {
                    foreach($data as $row)
                    {
            ?>
                        <tr>
                            <td><?php echo $row->title; ?></td>
                            <td><?php echo $row->description; ?></td>
                            <td></td>
                        </tr>
            <?php
                    }
                }
                else
                {
                    echo '<p>No record found</p>'; 
                }
            ?>
        </tbody>
    </table>
</div>

作为cakephpreturn对象中的find('all')方法,正常检查不会为空

而不是

if(!empty($data))

试试这条线

if (!$data->isEmpty()) {

应该可以。

请查找更新的文档 link