Question

I have a project that i have inherited and was poorly designed and i am just trying to bring some sanity into it. Here is the issue i am having. I have a table that stores projects, contacts and another table that stores writers attached to that project(although there are still more tables). I have managed to write the below script.

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

When i check the sql script generated it contains error. Below is what is generated

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 

With this error "Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'wp3_order_writers OrderWriter LEFT Array LEFT JOIN wp3_contacts AS Client ON' at line 1 [CORE\cake\libs\model\datasources\dbo_source.php, line 684]"

Your help will be greatly appreciated

Was it helpful?

Solution

I would make it with Containable (assuming that you have models like Order, OrderWriter, Writer and defined relations inside)

Then your query will look like:

$this->paginate['Order'] = array('contain' => array('Writer'));
$this->paginate['conditions'] = array(...);
$this->paginate['fields'] = array(...);

$orders = $this->paginate($this->Order);

I am not sure if the Order section should be like above or:

$this->paginate['Order'] = array('contain' => array('OrderWriter'=>array('Writer')));

Also I would advise you don't use 100000 limit, since it's probably a way to skip the pagination, but then just use find() instead of paginate.

Although avoiding pagination will slow down the request, since it will make a query for each row of Orders, so having default limit (20) or some reasonable number should speed-up the site/app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top