如何在cakepp中的一个页面中显示来自不同数据库表实体的数据


How to display data from different database table entities in one page in cakephp?

假设我有一个mysql数据库,并且我有3个实体,

  1. 电视
  2. 笔记本电脑
  3. 附件

每个实体都包含特定的信息
我想把这些数据打印到一个ctp页面上。我该怎么做
我不想使用foreign_key

您可以使用Cake的loadModel函数将任何表中的数据调用到当前控制器中。更多信息请点击本页底部。

$this->loadModel('YourModel');

然后设置变量,使其在您的视图中可用:

$this->set('yourModel', $this->YourModel->find('all', $settings);
$this->loadModel('YourOtherModel');
$this->set('yourOtherModel', $this->YourOtherModel->findI('all', $settings);

现在已经从模型中设置了两个变量,它们在视图中对您可用。

(使用debug查看所有可用的数据,即debug($yourModel);。您可以将其放在控制器操作中,它将在页面顶部或视图文件中的任何位置输出)

如果在表中显示数据,代码可能类似于这样:

<table>
    <th>Title</th>
    <th>Title</th>        
        <?php foreach ($array as $temp_variable):?>
    <tr>
        <td><?php  echo $temp_variable['ModelName']['ColumnName']; ?></td>
        <td><?php  echo $temp_variable['ModelName']['ColumnName']; ?></td>
        <?php endforeach;?>
    </tr>
</table>

对于一个数组。。对其余的数组执行类似的操作。。。我希望你能理解。