cakephp在一行中从父表获取多个值
我有这些桌子 1-产品类别 2-产品 3-产品模型 及 4-报价项目 现在,报价项的每一行都有这些值 id、产品类别、产品id、产品型号id、支持产品id、支持产品型号id 现在,正如您所看到的,Quote Item中有两个product和product model实例。我需要产品名称和产品型号名称显示在屏幕上 当我尝试在controller中检索数据时,它只从产品和产品模型中获取一个值,而我需要每个产品和产品模型的两个实例cakephp在一行中从父表获取多个值,cakephp,cakephp-2.3,Cakephp,Cakephp 2.3,我有这些桌子 1-产品类别 2-产品 3-产品模型 及 4-报价项目 现在,报价项的每一行都有这些值 id、产品类别、产品id、产品型号id、支持产品id、支持产品型号id 现在,正如您所看到的,Quote Item中有两个product和product model实例。我需要产品名称和产品型号名称显示在屏幕上 当我尝试在controller中检索数据时,它只从产品和产品模型中获取一个值,而我需要每个产品和产品模型的两个实例 array (size=4) 'QuoteItem' =&
array (size=4)
'QuoteItem' =>
array (size=9)
'product_category_id' => string '3' (length=1)
'product_id' => string '30' (length=2)
'product_model_id' => string '79' (length=2)
'support_product_id' => string '28' (length=2)
'support_product_model_id' => string '73' (length=2)
'quantity' => string '33' (length=2)
'length' => string '1' (length=1)
'unit_price' => string '456' (length=3)
'total_price' => string '15198' (length=5)
'Product' =>
array (size=2)
'name' => string 'p3' (length=2)
'id' => string '30' (length=2)
'ProductCategory' =>
array (size=2)
'name' => string 'Iron' (length=4)
'id' => string '3' (length=1)
'ProductModel' =>
array (size=2)
'name' => string 'm1' (length=2)
'id' => string '79' (length=2)
下面是我如何尝试检索数据的
$quote_items = $this->QuoteItem->find('all', array(
'fields' => array(
'product_category_id',
'product_id',
'product_model_id',
'support_product_id, Product.name',
'support_product_model_id',
'quantity',
'length',
'unit_price',
'total_price',
'ProductCategory.name',
'ProductModel.name'
),
'conditions' => array(
'quote_id' => $this->data['Quote']['quote_id']
),
'contain' => array('Product', 'ProductModel', 'ProductCategory')
));
这就是我看到的。正如您所注意到的,它只获取第一个产品的产品名,而对产品型号也是如此
array (size=4)
'QuoteItem' =>
array (size=9)
'product_category_id' => string '3' (length=1)
'product_id' => string '30' (length=2)
'product_model_id' => string '79' (length=2)
'support_product_id' => string '28' (length=2)
'support_product_model_id' => string '73' (length=2)
'quantity' => string '33' (length=2)
'length' => string '1' (length=1)
'unit_price' => string '456' (length=3)
'total_price' => string '15198' (length=5)
'Product' =>
array (size=2)
'name' => string 'p3' (length=2)
'id' => string '30' (length=2)
'ProductCategory' =>
array (size=2)
'name' => string 'Iron' (length=4)
'id' => string '3' (length=1)
'ProductModel' =>
array (size=2)
'name' => string 'm1' (length=2)
'id' => string '79' (length=2)
这是您获得的数据的正确形式。可能是在你的模型中,在你的联想中
belongsTo = array(
'Product' => array(..),
'ProductCategory', array(...),
'ProductModel' => array(...),
);
因此,添加以下关联
belongsTo = array(
'Product' => array(..),
'SupportProduct', array(
'className' => 'SupportProduct',
'foreignKey' => 'support_product_id',
),
'ProductCategory', array(...),
'ProductModel' => array(...),
'SupportProductModel' => array(
'className' => 'ProductModel',
'foreignKey' => 'support_product_model_id',
),
);
因此,您可以使用不同的关联名称对同一类进行多个关联。没有仍然相同的结果,没有任何更改。很抱歉,我忘了告诉您,您还必须更新“查找”查询。对于新的关联,我告诉过您,
$this->QuoteItem->find('all')
也带来了关联的SupportProduct
和SupportProductModel
;