2017-06-12 22 views
0

與更新後的代碼我試圖綁定付款和網站之間的關係,但似乎group by不起作用。我得到很多重複的結果。Cakephp運行時綁定關聯組通過不起作用

更新時間:

$this->Website->unBindModel(array('hasMany' => array('User'))); 
$this->Website->bindModel(array(
           'hasMany' => array(
            'Payment' => array(
             'className' => 'Payment', 
             'foreignKey' => 'website_id', 
             'fields' => array(
              'Payment.id', 
              'Payment.website_id', 
              'Payment.created', 
              'Payment.user_id', 
              'Payment.is_get_invoice' 
             ) , 
             'conditions' => array(
              'Payment.website_id <>' => '0', 
              'Payment.is_get_invoice' => '0' 
             ), 
             'order' => array(
              'Payment.id DESC' 
             ), 
             'group' => array(
              'Payment.website_id' //Its not working 
             ) 
            ) 
           ) 
          ) 
         ); 


$this->Website->recursive = 1; 
$webPayments = $this->Website->find('all', array('fields' => array('Website.id'),'contain' => array('Payment'))); 

老:

我試圖綁定和取消綁定模式運行。 但它給了我錯誤。即使關聯也不起作用。

$this->Website->unBindModel(array(
    'hasMany' => array(
     'User' 
    ) 
)); 
$this->Website->bindModel(array(
    'hasMany' => array(
     'Payment' => array(
      'className' => 'Payment', 
      'foreignKey' => 'website_id' 
     ) 
    ) 
)); 
$this->Website->recursive = 1; 
$webPayments = $this->Website->find('all', array(
    'fields' => array(
     'Website.id', 
     'Payment.id', 
     'Payment.website_id', 
     'Payment.created', 
     'Payment.user_id' 
    ) , 
    'conditions' => array(
     'Payment.website_id <>' => '0', 
     'Payment.is_get_invoice' => '0' 
    ) , 
    'order' => array(
     'Payment.id DESC', 
     'group' => array(
      'Payment.website_id' 
     ) 
    ) 
)); 

pr($webPayments); 

錯誤:

Columnnotfound:1054 Unknowncolumn 'Payment.id' 在 '字段列表'

SELECT`Website` . `id`, `Payment` . `id`, `Payment` . `website_id`, `Payment` . `created`, `Payment` . `user_id`, FROM`websites` AS `Website`WHERE`Payment` . `website_id` <> 0 AND `Payment` . `is_get_invoice` = '0' AND `Payment` . `created` > '2017-04-13 07:07:54' GROUPBY `Payment` . `website_id` ORDERBY `Payment` . `id` DESC 
+0

是否使用的是蛋糕的版本? –

+0

@GeorgeM Cakephp 2.6 –

+0

你在更新中做得很好,把'訂單'和'組'放在同一層。 –

回答

2

嘗試使用含有這樣的:

$webPayments = $this->Website->find('all', array(
    'fields' => array(
     'Website.id',  
    ), 
    'contain' => array(
     'Payment' => array(
      'fields' => array(
       'Payment.id', 
       'Payment.website_id', 
       'Payment.created', 
       'Payment.user_id' 
      ), 
      'conditions' => array(
       'Payment.website_id <>' => '0', 
       'Payment.is_get_invoice' => '0' 
      ), 
      'order' => array(
       'Payment.id DESC', 
       'group' => array(
        'Payment.website_id' 
       ) 
      ) 
     ) 
    ) 
)); 

https://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

+0

包含對相關模型無效。 –

1

您的模型是否適用於關聯?你應該有

class Payment extends AppModel { 
    var $name = 'Payment'; 
    public $actAs = array('Containable'); 

public $belongsTo = array (
    'Website' => array(
     'className' => 'Website', 
     'foreignKey' => 'website_id' 
    ) 
) 
[...] 
} 

class Website extends AppModel { 
    var $name 'Website'; 
    public $actAs = array('Containable'); 

    public $hasMany = array(
     'Payment' => array(
      'className'=>'Payment', 
      'order' => 'Payment.id DESC' 
     ) 
    ) 
[...] 
} 

我發現,在模型中聲明的東西儘可能低的水平真的作品比試圖用一個查詢做對飛。如果該協會在你不該模型本身正確申報更好不必像這樣綁定/解除綁定。

+0

嘗試將「遞歸」設置爲2。我知道你只需要1,但1是一個特殊的價值,如果你把它設置爲不必要的2,你有更好的機會獲得你需要的數據正確。在我的經驗:-) –

+0

呃哦..另見https://stackoverflow.com/questions/10746506/cakephp-wont-apply-group-by-condition沒有暗示的版本,但它是令人不安。請注意,直接修改內核需要麻煩,您必須製作自己的副本並對其進行修改。 –

+0

Thankyou ,.但我正在尋找與綁定和解除綁定的解決方案。如果我將改變付款模式和網站模型,那麼它可能會影響另一方,並且查詢可能會像最新的關聯也會起作用,這就是爲什麼我選擇綁定和解綁 –

0

hasMany協會不支持group by密鑰。

在這種特殊情況下,您通過錯誤使用group而試圖實現的目標是返回一個結果。請注意,使用支持的limit密鑰可以實現相同的功能。

的代碼將是如下:

$this->Website->unBindModel(array('hasMany' => array('User'))); 
$this->Website->bindModel(array(
          'hasMany' => array(
           'Payment' => array(
            'className' => 'Payment', 
            'foreignKey' => 'website_id', 
            'fields' => array(
             'Payment.id', 
             'Payment.website_id', 
             'Payment.created', 
             'Payment.user_id', 
             'Payment.is_get_invoice' 
            ) , 
            'conditions' => array(
             'Payment.website_id <>' => '0', 
             'Payment.is_get_invoice' => '0' 
            ), 
            'order' => array(
             'Payment.id DESC' 
            ), 
            'limit' => 1 
            ) 
           ) 
          ) 
         ) 
        ); 

參考

+0

@困惑男孩,你試過這個答案嗎? –