cakephp3中的关系

问题描述 投票:1回答:1

我正在尝试建立表格之间的关系我读了cakephp3中的文档,以及stackoverflow中的大量帖子,但仍然没有任何结果。我有两个桌子。

  • category (id, name, published, created, modified)

  • portfolio (id, category_id, name, created, modified)

我想显示类别的portofolio的每个元素:

Category Cars

* element 1: Bmw
* element 2: Audi
* element 3: Ford

如何建立这种关系并显示?

我写的类别模型:

 $this->belongsTo('Portfolios', [
              'foreignKey' => 'id'
          ]);

这是查询:

$portfolios_category = $this->Categories->find()
            ->contain(['Portfolios'])
            ->where(['Categories.published' => 1])
            ->order(['Categories.created' => 'desc'])
            ->all();
php cakephp associations cakephp-3.x
1个回答
0
投票

开始使用命令提示符。转到您的project_folder / bin /并打开命令提示符。

然后键入

cake bake model

然后您将获得带有型号名称的表格列表,然后输入

cake bake model <ModelName>

ex:

cake bake model Category
cake bake model Portfolio 
© www.soinside.com 2019 - 2024. All rights reserved.