cakePHP - 通过belongsTo在第二次递归中拉模型关联

时间:2011-07-16 11:35:38

标签: cakephp

我尝试在cakePHP 1.3中具有许多关联的模型上获得一个查找('all'...),它在第二级递归中具有查询的过滤条件架构。简单来说,它看起来像这样,我想过滤UserId:

Delivery belongsTo Order, Order belongsTo User.

以下是关联:

  

订单:   var $ belongsTo = array(

  'User' => array(
      'className' => 'User',
      'foreignKey' => 'user_id',
      'conditions' => '',
      'fields' => '',
      'order' => ''
  ),....
     

送货:   var $ belongsTo = array(

   'Order' => array(
       'className' => 'Order',
       'foreignKey' => 'order_id',
       'conditions' => '',
       'fields' => '',
       'order' => ''
   ),...

产生的错误是:

  

SQL错误:1054:'where子句'中的未知列'User.id'[CORE / cake / libs / model / datasources / dbo_source.php,第684行]

这里是完整的查询,只是为了好玩:

  

选择DeliveryidDeliveryorder_idDeliverydelivery_address_idDelivery。{{1} },deliver_dateDeliverycreatedDeliverymodifiedDeliverydeliver_runDeliveryproduct_mix_id1Deliveryproduct_mix_id2Deliveryproduct_mix_id3Deliveryproduct_mix_id4Delivery,{{ 1}}。assembledDeliveryshippedDeliveryratedDeliveryprice,{{1} }。Deliveryproduct_lines_idOrderidOrderuser_idOrderproduct_lines_idOrderorder_dateOrderdeliver_mondayOrderdeliver_tuesdayOrderdeliver_wednessday。{{ 1}},Orderdeliver_thursdayOrderdeliver_fridayOrderdeliver_saturdayOrder。{{1} },delivery_address_idOrderpayment_delayOrderactiveOrdercancle_dateOrderreplaced_order_idOrdercreatedOrdermodifiedDeliveryAddressidDeliveryAddress,{{ 1}}。delivery_companyDeliveryAddressdelivery_titleDeliveryAddressdelivery_first_nameDeliveryAddressdelivery_last_name,{{1} }。DeliveryAddressdelivery_streetDeliveryAddressdelivery_house_nrDeliveryAddressdelivery_postal_codeDeliveryAddressdelivery_townDeliveryAddressdelivery_countryDeliveryAddresscreatedDeliveryAddressdeletedDeliveryAddressmodified。{{ 1}},ProductLineidProductLinenameProductLinedescriptionProductMix1。{{1} },idProductMix1nameProductMix1descriptionProductMix1image_small_pathProductMix1image_normal_pathProductMix1product_categories_idProductMix1depricatedProductMix1createdProductMix1,{{ 1}}。modifiedProductMix2idProductMix2nameProductMix2description,{{1} }。ProductMix2image_small_pathProductMix2image_normal_pathProductMix2product_categories_idProductMix2depricatedProductMix2createdProductMix2modifiedProductMix3idProductMix3name。{{ 1}},ProductMix3description,{{ 1}}。ProductMix3image_small_pathProductMix3image_normal_pathProductMix3product_categories_idProductMix3,{{1} }。depricatedProductMix3createdProductMix3modifiedProductMix4idProductMix4name来自ProductMix4 AS description LEFT JOIN ProductMix4 AS image_small_path ON(ProductMix4image_normal_path = ProductMix4product_categories_id)LEFT JOIN ProductMix4 AS depricated ON(ProductMix4created = ProductMix4modified)LEFT JOIN {{1 } deliveries ON(Deliveryorders = OrderDelivery)LEFT JOIN order_id AS Order ON({ {1}}。id = delivery_addressesDeliveryAddress)LEFT JOIN Delivery AS delivery_address_id ON(DeliveryAddressid = product_linesProductLine)LEFT JOIN Delivery AS product_lines_id ON(ProductLineid = product_mixesProductMix1 )LEFT JOIN Delivery AS product_mix_id1 ON(ProductMix1id = product_mixesProductMix2)WHERE Delivery。{{1 }} = 1

有没有人知道为什么蛋糕没有拉到第二级,在这种情况下是User模型,甚至递归时都设置为5?

非常感谢。

1 个答案:

答案 0 :(得分:0)

编辑:我刚刚想到,在您的情况下,您实际上不需要第二级JOIN,因为您可以按Order.user_id过滤(而不是{{ 1}})!你明白我的意思吗?

所以你可能不需要下面的解决方案。


据我所知,Cake从不做第二级User.id本身,所以对于第二级(和更深层次)的过滤(条件),我使用joins

对于你的例子:

JOIN