使用更新的代码我试图绑定支付和网站之间的关系,但似乎分组不工作。而且我得到了很多重复的结果。
已更新:
$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'Repport.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 :(得分: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
答案 1 :(得分: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'
)
)
[...]
}
我发现在模型中尽可能低地声明事物确实比尝试使用查询动态执行它更好。如果在模型本身中正确声明了关联,则不应该使用dork与绑定/解除绑定..
答案 2 :(得分: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
)
)
)
)
);
参考