【问题标题】:cakephp Get multiple values from parent table in one rowcakephp 在一行中从父表中获取多个值
【发布时间】:2014-11-02 19:21:10
【问题描述】:

我有这些表

1 - 产品类别 2 - 产品 3 - 产品型号

4 - 引用项目

现在每行报价项都有这些值

id、product_category、product_id、product_model_id、support_product_id、support_prouct_model_id

现在您可以看到报价项目中有产品和产品模型的两个实例。我需要产品名称和产品型号名称才能显示在屏幕上。

当我尝试在控制器中检索数据时,它只从产品和产品模型中带来一个值,而我需要每个产品和产品模型的两个实例。

这是我尝试检索数据的方式。

$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)

【问题讨论】:

    标签: cakephp cakephp-2.3


    【解决方案1】:

    这是您获得的数据的正确形式。可能在您的模型 QuoteItem 在您的关联中,您有

    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') 还带来了关联的 SupportProductSupportProductModel
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多