mysql - 如何在 CakePHP 2 中为 3 个连接的 mysql 表编写模型

标签 mysql cakephp cakephp-2.0 cakephp-2.3 cakephp-2.1

我在理解如何在 CakePHP 2.x 中针对这种情况编写模型时遇到问题:

我有一个表“订单”,其中“产品”表中有许多产品。该产品表有一个“尺寸”表。

这个 mysql 查询显示了我想从我的模型中得到什么。我知道如何为“订单”和“产品”表创建模型,但我不知道如何包含“尺寸”表。

SELECT 
orders.id, 
orders.name, 
products.id, 
products.name, 
sizes.id, 
sizes.name
FROM
orders 
INNER JOIN products ON orders.id = products.order_id
INNER JOIN sizes ON sizes.id = products.size_id

最佳答案

我在 CakePHP - How do I join a table on a joined table? 中没有找到解决方案

我确实喜欢这样:

 $options = array('conditions' => array('Order.' . $this->Order->primaryKey => $id));

 $this->request->data = $this->Order->find('first', $options);

 $options = array('conditions' => array('Product.order_id' => $id));

 $Products= $this->Order->Product->find('all', $options);

从那里我将获得该产品的尺寸数组:

[Size] => Array
    (
        [id] => 1
        [name] => 'BIG'
    )

关于mysql - 如何在 CakePHP 2 中为 3 个连接的 mysql 表编写模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147967/

相关文章:

php - CakePHP 2.x 插件和插件文件夹之间有什么区别?

mysql - 从一个表中选择项目,记录总和与子查询中的 HAVING 匹配

php - Cakephp,在加载 View 时动态地将变量写入CSS文件?

cakephp - 找出(在 Controller 中)哪个特定的验证规则失败

php - Cakephp 2.0 : array data in controller (from post) is not editable

cakephp - 如何处理违反完整性约束的错误

cakephp - 在身份验证用户 session 中包含字段子集

php - 在多开发 nginx 设置中保护密码

php SQL 数组的动态列表 - 如何设置顺序?

mysql - 根据 id 从 2 个不同的表中获取乘法总和