【问题标题】:Joining tables CakePHP连接表 CakePHP
【发布时间】:2016-06-25 08:57:01
【问题描述】:

我有一个表 batch_detail_process 有外键 batch_detail_id 并且它与模型中的 belongsTo 连接。现在我想根据article_id和item_id得到ArticleRatio。

我的表外键如下:

batch_details_processes = id,batch_detail_id

batch_details = id、batch_id、item_id

batch = id, article_id,

article_ratios = id, article_id, item_id,ratio

$conditions = array();

$conditions['joins'] = array(
                array(
                    'table' => 'batches',
                    'alias' => 'Batch',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Batch.id = BatchDetail.batch_id'
                    )
                ),
                array(
                    'table' => 'article_ratios',
                    'alias' => 'ArticleRatio',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'ArticleRatio.article_id = Batch.article_id',
                        'ArticleRatio.item_id = BatchDetail.item_id'
                    )
                )
            );

        $bdp = $this->BatchDetailsProcess->find('all',$conditions);

pr($bdp); 

返回以下内容:

  Array
(
    [0] => Array
        (
            [BatchDetailsProcess] => Array
                (
                    [id] => 1
                    [batch_detail_id] => 1
                    [qty_in] => 40
                    [qty_out] => 
                    [process_step_id] => 1
                    [qc_test] => 
                    [qc_note] => 
                    [qc_accept] => 
                    [qc_reject] => 
                    [moved_further] => 
                    [assigned_to] => 3
                    [receive_confirmation] => 0
                    [created_by] => 2
                    [updated_by] => 
                    [created] => 2016-06-18 17:39:21
                    [updated] => 0000-00-00 00:00:00
                )

            [BatchDetail] => Array
                (
                    [id] => 1
                    [batch_id] => 1
                    [po_transaction_id] => 1
                    [current_position] => 1
                    [out_date] => 2016-06-21
                    [item_id] => 36
                )

            [ProcessStep] => Array
                (
                    [id] => 
                    [article_id] => 
                    [production_step_id] => 
                    [sequence] => 
                    [rate_per] => 
                    [amount_per] => 
                )

        )

    [1] => Array
        (
            [BatchDetailsProcess] => Array
                (
                    [id] => 4
                    [batch_detail_id] => 4
                    [qty_in] => 1
                    [qty_out] => 
                    [process_step_id] => 4
                    [qc_test] => 
                    [qc_note] => 
                    [qc_accept] => 
                    [qc_reject] => 
                    [moved_further] => 
                    [assigned_to] => 3
                    [receive_confirmation] => 0
                    [created_by] => 1
                    [updated_by] => 
                    [created] => 2016-06-22 00:24:25
                    [updated] => 0000-00-00 00:00:00
                )

            [BatchDetail] => Array
                (
                    [id] => 4
                    [batch_id] => 3
                    [po_transaction_id] => 2
                    [current_position] => 4
                    [out_date] => 2016-06-30
                    [item_id] => 36
                )

            [ProcessStep] => Array
                (
                    [id] => 
                    [article_id] => 
                    [production_step_id] => 
                    [sequence] => 
                    [rate_per] => 
                    [amount_per] => 
                )

        )

    [2] => Array
        (
            [BatchDetailsProcess] => Array
                (
                    [id] => 2
                    [batch_detail_id] => 2
                    [qty_in] => 50
                    [qty_out] => 
                    [process_step_id] => 1
                    [qc_test] => 
                    [qc_note] => 
                    [qc_accept] => 
                    [qc_reject] => 
                    [moved_further] => 
                    [assigned_to] => 3
                    [receive_confirmation] => 0
                    [created_by] => 2
                    [updated_by] => 
                    [created] => 2016-06-18 17:39:21
                    [updated] => 0000-00-00 00:00:00
                )

            [BatchDetail] => Array
                (
                    [id] => 2
                    [batch_id] => 1
                    [po_transaction_id] => 3
                    [current_position] => 1
                    [out_date] => 2016-06-30
                    [item_id] => 37
                )

            [ProcessStep] => Array
                (
                    [id] => 
                    [article_id] => 
                    [production_step_id] => 
                    [sequence] => 
                    [rate_per] => 
                    [amount_per] => 
                )

        )

    [3] => Array
        (
            [BatchDetailsProcess] => Array
                (
                    [id] => 3
                    [batch_detail_id] => 3
                    [qty_in] => 11
                    [qty_out] => 
                    [process_step_id] => 1
                    [qc_test] => 
                    [qc_note] => 
                    [qc_accept] => 
                    [qc_reject] => 
                    [moved_further] => 
                    [assigned_to] => 3
                    [receive_confirmation] => 0
                    [created_by] => 2
                    [updated_by] => 
                    [created] => 2016-06-18 17:44:42
                    [updated] => 0000-00-00 00:00:00
                )

            [BatchDetail] => Array
                (
                    [id] => 3
                    [batch_id] => 2
                    [po_transaction_id] => 3
                    [current_position] => 1
                    [out_date] => 2016-07-15
                    [item_id] => 37
                )

            [ProcessStep] => Array
                (
                    [id] => 
                    [article_id] => 
                    [production_step_id] => 
                    [sequence] => 
                    [rate_per] => 
                    [amount_per] => 
                )

        )

    [4] => Array
        (
            [BatchDetailsProcess] => Array
                (
                    [id] => 5
                    [batch_detail_id] => 5
                    [qty_in] => 9
                    [qty_out] => 
                    [process_step_id] => 5
                    [qc_test] => 
                    [qc_note] => 
                    [qc_accept] => 
                    [qc_reject] => 
                    [moved_further] => 
                    [assigned_to] => 4
                    [receive_confirmation] => 0
                    [created_by] => 2
                    [updated_by] => 
                    [created] => 2016-06-22 13:05:56
                    [updated] => 0000-00-00 00:00:00
                )

            [BatchDetail] => Array
                (
                    [id] => 5
                    [batch_id] => 4
                    [po_transaction_id] => 38
                    [current_position] => 5
                    [out_date] => 2016-07-20
                    [item_id] => 17
                )

            [ProcessStep] => Array
                (
                    [id] => 
                    [article_id] => 
                    [production_step_id] => 
                    [sequence] => 
                    [rate_per] => 
                    [amount_per] => 
                )

        )

    [5] => Array
        (
            [BatchDetailsProcess] => Array
                (
                    [id] => 6
                    [batch_detail_id] => 6
                    [qty_in] => 85
                    [qty_out] => 
                    [process_step_id] => 3
                    [qc_test] => 
                    [qc_note] => 
                    [qc_accept] => 
                    [qc_reject] => 
                    [moved_further] => 
                    [assigned_to] => 4
                    [receive_confirmation] => 0
                    [created_by] => 1
                    [updated_by] => 
                    [created] => 2016-06-22 14:36:42
                    [updated] => 0000-00-00 00:00:00
                )

            [BatchDetail] => Array
                (
                    [id] => 6
                    [batch_id] => 5
                    [po_transaction_id] => 39
                    [current_position] => 3
                    [out_date] => 2016-06-30
                    [item_id] => 17
                )

            [ProcessStep] => Array
                (
                    [id] => 
                    [article_id] => 
                    [production_step_id] => 
                    [sequence] => 
                    [rate_per] => 
                    [amount_per] => 
                )

        )

)

由它生成的SQL:

SELECT  `BatchDetailsProcess`.`id`, `BatchDetailsProcess`.`batch_detail_id`,
        `BatchDetailsProcess`.`qty_in`, `BatchDetailsProcess`.`qty_out`,
        `BatchDetailsProcess`.`process_step_id`, `BatchDetailsProcess`.`qc_test`,
        `BatchDetailsProcess`.`qc_note`, `BatchDetailsProcess`.`qc_accept`,
        `BatchDetailsProcess`.`qc_reject`, `BatchDetailsProcess`.`moved_further`,
        `BatchDetailsProcess`.`assigned_to`, `BatchDetailsProcess`.`receive_confirmation`,
        `BatchDetailsProcess`.`created_by`, `BatchDetailsProcess`.`updated_by`,
        `BatchDetailsProcess`.`created`, `BatchDetailsProcess`.`updated`,
        `BatchDetail`.`id`, `BatchDetail`.`batch_id`, `BatchDetail`.`po_transaction_id`,
        `BatchDetail`.`current_position`, `BatchDetail`.`out_date`,
        `BatchDetail`.`item_id`, `ProcessStep`.`id`, `ProcessStep`.`article_id`,
        `ProcessStep`.`production_step_id`, `ProcessStep`.`sequence`,
        `ProcessStep`.`rate_per`, `ProcessStep`.`amount_per`
    FROM  `star_impact`.`batch_details_processes` AS `BatchDetailsProcess`
    LEFT JOIN  `star_impact`.`batch_details` AS `BatchDetail`
           ON (`BatchDetailsProcess`.`batch_detail_id` = `BatchDetail`.`id`)
    LEFT JOIN  `star_impact`.`process_steps` AS `ProcessStep`
           ON (`BatchDetailsProcess`.`process_step_id` = `ProcessStep`.`id`)
    LEFT JOIN  `star_impact`.`batches` AS `Batch`
           ON (`Batch`.`id` = `BatchDetail`.`batch_id`)
    LEFT JOIN  `star_impact`.`article_ratios` AS `ArticleRatio`
           ON (`ArticleRatio`.`article_id` = `Batch`.`article_id`
              AND  `ArticleRatio`.`item_id` = `BatchDetail`.`item_id`
                          )
    WHERE  1 = 1 

我想根据 article_id 和 item_id 匹配来选择文章比率。但结果不返回 ArticleRatio,而只返回 BatchDetailProcess、BatchDetail 和 ProcessStep。

【问题讨论】:

  • @AD7six 我没有添加其他条件,请参阅更新后的问题。
  • 你还没有解决我评论的最后一部分。问题中的sql没有where,只有left join;如果没有结果,那是因为 BatchDetailsProcess 中没有任何内容(尝试直接对 db 运行 sql)存在不同的误解,例如查询被延迟执行。
  • 我认为 join 中的 cconditions 会产生 where 条件。实际上我是第一次使用 Joins,直到现在我通常使用 Containable 或默认 cakephp 查询。
  • @AD7six 你能帮我查询一下吗
  • 查看 $bdp 的输出。

标签: mysql cakephp join cakephp-2.8


【解决方案1】:

结果不返回 ArticleRatio

联接不是关联。

CakePHP 的 ORM 不会发出 SELECT * 查询,字段总是从问题中指定:

SELECT 
    ... <- No ArticleRatio here 
FROM 
    `star_impact`.`batch_details_processes` AS `BatchDetailsProcess` 
...
WHERE 
    1 = 1

如果 select 中没有提及,则连接表中不会返回任何字段,并且这些表中不会有 CakePHP 的数据。

要么:

指定查询中的字段

不要让 CakePHP 决定返回哪些字段,而是指定它们:

$conditions['fields'] = '*';
$bdp = $this->BatchDetailsProcess->find('all',$conditions);

SELECT 
    * <- Lazy, all fields. Or be more specific with 'fields'
FROM 
    `star_impact`.`batch_details_processes` AS `BatchDetailsProcess` 
...
WHERE 
    1 = 1

注意要查找的第二个参数是parameters 不是条件。

或者:

使用关联。

使用临时 associations 代替连接:

$this->BatchDetailsProcess->bindModel('hasOne', [
    'Batch' =>  [
        'conditions' => [
            'Batch.id = BatchDetail.batch_id'
        ]
    ],
    'ArticleRatio' => [
        'conditions' => [
            'ArticleRatio.article_id = Batch.article_id',
            'ArticleRatio.item_id = BatchDetail.item_id'
        ]
    ]
]);
$this->BatchDetailsProcess->find();

这将创建一个左连接,并在响应中添加填充字段/实体;调整传递给关联定义的参数(并使用containable)以适应。

【讨论】:

  • 第一种方法工作正常,但关联不能正常工作。我很乐意和第一个一起去。
猜你喜欢
  • 2012-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多