3
select campaign,lead_status,COUNT(id)
from buyers 
where lead_status IN('Very Hot','Hot', 'Warm', 'Cold', 'Not Contacted') 
    and campaign IN('stage1','stage2','stage3','stage4') and created_on >='2012-10-01' 
    and is_put_for_exchange='0' and transaction_type='First Sale' 
    and propertytype='Residential' 
group by campaign,lead_status 
ORDER BY campaign asc, field(lead_status,'Very Hot','Hot', 'Warm', 'Cold', 'Not Contacted')

convert in cakephp Syntax plz It Argent?

3 Answers 3

10

You can modify your raw SQL into CakePHP format by going through the documentation.

Or for any reason you cannot / will not, then you can use the raw SQL query as follows:

In controller:

$this->ModelName->query('SELECT * FROM table');

In model:

$this->query('SELECT * FROM table');

Documentation of query()

Sign up to request clarification or add additional context in comments.

5 Comments

Trust me it doesn't prevent SQL injection. :(
@Vineet You must be talking about the raw query, right? Because when query is formulated using CakePHP, it sanitizes etc to prevent SQL injection. Also, for raw query see prepared statements at book.cakephp.org/2.0/en/models/…
Yes exactly I am using prepared statements now.
@Vineet Glad to know, and this discussion will help future readers also.
This is helpful for me
6

Try this

$this->Buyer->find('all', array(
'fields' => $arrayOfFields,
'conditions' => array(
    'Buyer.lead_status' => $arrayOfLeadStatus,
    'Buyer.compaign' => $arrayOfCompaign,
    'Buyer.is_put_for_exchange' => 0,
    'Buyer.transaction_type' => 'First Sale',
    'Buyer.propertytype' => 'Residential'   
    ),
'group' => array('Buyer.campaign','Buyer.lead_status'),
'order' => 'Buyer.campaign'
));

Comments

0

For reference to others, this query will be look like this if we convert into CakePHP method

$result = $this->ModelName->find("all",array(
    'fields' => array('campaign','lead_status','count(id') as counts),
    'conditions' => array(
        'lead_status' => array('Very Hot','Hot', 'Warm', 'Cold', 'Not Contacted'),
        'campaign' => array('stage1','stage2','stage3','stage4'),
        "created_on >='2012-10-01' ",
        'is_put_for_exchange' => 0,
        'transaction_type' => 'First Sale',
        'propertytype' => 'Residential'
    ),
    'group' => array('Buyer.campaign','Buyer.lead_status'),
    'order' => 'Buyer.campaign'
));

Comments

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.