0

I want to select data like this SQL query:

SELECT discounts.product_id, products.product_name,
sum(products.product_price - discounts.product_discount) as total_Amount,
count(orders.order_id) as total_Number
FROM products 
inner join discounts on products.product_id = discounts.product_id
inner join orders on discounts.discount_id = orders.discount_id

where discounts.start_time >= @from and discounts.end_time <= @to 

group by discounts.product_id,products.product_name

@from and @to are values that I will pass to.

Here is what I did in Controller:

$from='27 November 2012';
        //$this->layout = 'customer-backend';
        $this->Order->recursive=-1;
        $this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)';
        $this->Order->virtualFields['number']='COUNT(Order.order_id)';
        $option['joins'] = array(
            array('table'=>'discounts',
                'alias'=>'Discount',
                'type'=>'INNER',
                'conditions'=>array(
                    'Order.discount_id = Discount.discount_id',
                )
            ),
            array('table'=>'products',
                'alias'=>'Product',
                'type'=>'INNER',
                'conditions'=>array(
                    'Discount.product_id = Product.product_id'
                )
            )
        );
        $option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
        $option['conditions']=array('Discount.start_time >='=>$from, 'Discount.end_time <= ' => $to);   //I guess it's wrong here
        $option['group'] = array('Discount.product_id','Product.product_name');
        //$option['limit']=20;
        $products = $this->Order->find('all',$option);
        $this->set('products',$products);

I think this line is wrong:

$option['conditions']=array('Discount.start_time >='=>$from, 'Discount.end_time <= ' => $to);

When I deleted it, it worked. But I dont know how to fix it. Please help me.

3
  • On this one too - if you're going to be using MySQL, learn how to debug it. What query is CakePHP generating? If you show that, it will be easy to solve. Commented Dec 3, 2013 at 2:53
  • @Dave Im new to Cakephp. Can you tell me how to debug it? Commented Dec 3, 2013 at 3:05
  • 1
    I can't, but my good buddy Google would be glad to help. He has lots of friends who have asked that same question many many many times. ;) (not trying to be an ass - just... it's a very common question, and this isn't the place for that) Commented Dec 3, 2013 at 3:52

1 Answer 1

1

Not CakePHP's problem but they way MYSQL is receiving your query. Try formatting the dates.

$from = date('Y-m-d', strtotime($from));
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer. The date is in format 'dd month yyyy'. Eg 08 November 2013. When I type it directly in mySQL it worked.
what is the column type? datetime? date?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.