我有一个项目,我已经继承并且设计很差,我只是想为它带来一些理智。这是我遇到的问题。我有一个表存储项目,联系人和另一个存储附加到该项目的作者的表(尽管还有更多的表)。我设法编写了以下脚本。
$conditions = $this->paginate = array('joins' => array(
'table' => 'wp3_order_writers',
'alias' => 'OrderWriter',
'type' => 'LEFT',
'conditions' => array(
'OrderWriter.order_id=Order.id'
)
),
'conditions' => array(
array('Order.status_id' => array(5,6,7,8),
'OR' => array('Order.assigned_to_writer' => $this->_user['Contact']['id'],
'OrderWriter.writer_id' => $this->_user['Contact']['id'],
)
),
),
'fields' => array(
'Order.id', 'Order.urgent', 'Order.writer_deadline', 'Order.subject_title', 'Client.name', 'Client.email', 'Service.*', 'Order.writer_fee', 'Order.count_of_words', 'Status.*', 'Order.pay_status', 'Subject.*'
)
);
$this->paginate['limit'] = 100000;
$orders = $this->paginate($this->Order);
当我检查生成的sql脚本时,它包含错误。以下是生成的内容
SELECT `Order`.`id`, `Order`.`urgent`, `Order`.`writer_deadline`, `Order`.`subject_title`, `Client`.`name`, `Client`.`email`, `Service`.*,`Order`.`writer_fee`, `Order`.`count_of_words`, `Status`.*, `Order`.`pay_status`, `Subject`.* FROM `wp3_orders` AS `Order` wp3_order_writers OrderWriter LEFT Array LEFT JOIN `wp3_contacts` AS `Client` ON (`Order`.`client_id` = `Client`.`id`) LEFT JOIN `wp3_keys_values` AS `Service` ON (`Order`.`service_id` = `Service`.`id` AND `Service`.`key` = 'services') LEFT JOIN `wp3_keys_values` AS `PayMethod` ON (`Order`.`pay_method_id` = `PayMethod`.`id` AND `PayMethod`.`key` = 'pay_methods') LEFT JOIN `wp3_subjects` AS `Subject` ON (`Order`.`subject_id` = `Subject`.`id`) LEFT JOIN `wp3_keys_values` AS `Status` ON (`Order`.`status_id` = `Status`.`id` AND `Status`.`key` = 'statuses') LEFT JOIN `wp3_clients_sessions` AS `ClientsSession` ON (`ClientsSession`.`current_order_id` = `Order`.`id`) WHERE ((`Order`.`status_id` IN (5, 6, 7, 8)) AND (((`Order`.`assigned_to_writer` = 1087) OR (`OrderWriter`.`writer_id` = '1087')))) AND `Order`.`completed` = 1 LIMIT 100000
出现此错误“警告(512):SQL错误:1064:您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在'wp3_order_writers'附近使用正确的语法OrderWriter LEFT Array LEFT JOIN第1行wp3_contacts
AS Client
ON'[CORE \ cake \ libs \ model \ datasources \ dbo_source.php,第684行]“
非常感谢您的帮助
答案 0 :(得分:0)
我会用Containable制作它(假设你有像Order,OrderWriter,Writer和里面定义的关系这样的模型)
然后您的查询将如下所示:
$this->paginate['Order'] = array('contain' => array('Writer'));
$this->paginate['conditions'] = array(...);
$this->paginate['fields'] = array(...);
$orders = $this->paginate($this->Order);
我不确定订单部分是否应该如上所述:
$this->paginate['Order'] = array('contain' => array('OrderWriter'=>array('Writer')));
另外我建议你不要使用100000限制,因为它可能是一种跳过分页的方法,但只是使用find()而不是paginate。
虽然避免分页会降低请求速度,因为它会对每行订单进行查询,因此具有默认限制(20)或一些合理的数字可以加速网站/应用。