how to write cakephp and and or query - php

$tmp_sales = $this->tmp_sale->find('all', array(
'conditions' => array(
array(
'no' => $no,
'barcode' => $barcode,'employee' => $emp, 'store' => $store_name
)
)
));
This is my present query.
select * from tmp_sale where no = '$no' and
'employee' ='$emp' and store = '$store_name' and ( barcode='$barcode' or name='$barcode')
I want to make change like above . how to write query like this in cakephp

'conditions' => array(
'no' => $no,
'employee' => $emp,
'store '=> $store_name,
'OR' => array(
array('barcode' => $barcode),
array('name' => $barcode)
)
)

Cakephp Model name should be Singular,tmp_sale Model name should be replaced by TmpSale name. The query of cakephp shown in below.
You should try this
$tmp_sales = $this->TmpSale->find('all', array(
'conditions' => array( 'no' => $no,
'employee' => $emp,
'store '=>$store_name,
'or'=>array('barcode'=>$barcode,
'name'=>$barcode)
)
));
Thanks

Related

Cakephp 2.8.x Query access violation when i try the CakePHP way. Normal query works

I created a query, but now i need to refactor it to the CakePHP standard. Only i can't seem to get it working.
This is my working query:
$query = $this->Transfer->query( "SELECT DISTINCT emailaddresses.emailaddress
FROM transfers
JOIN emailaddresses
ON (emailaddresses.transfer_id = transfers.transfer_id AND emailaddresses.type <> 'SENDER' AND emailaddresses.received_state = 'DELIVERED')
WHERE transfers.created_user_id = $created_user_id " );
This query is working, but when i try to Cakeify it i get access denied for table emailaddress. This is the CakePHP query:
$query = $this->Transfer->find('all', array(
'fields' => 'DISTINCT Emailaddress.emailaddress ',
'conditions' => array(
'transfers.created_user_id' => $created_user_id
),
'joins' => array(
array(
'table' => 'Emailaddress.emailaddress',
'type' => 'INNER',
'conditions' => array(
'Emailaddress.transfer_id' => 'Transfer.transfer_id',
'Emailaddress.type' => 'SENDER',
'Emailaddress.received_state' => 'DELIVERED'
)
)
)
));
What do i need to change to get this query working with the Cake standards?
Your query would be like:
$query = $this->Transfer->find('all', array(
//'fields' => 'DISTINCT Emailaddress.emailaddress ',
'fields' => array('DISTINCT Emailaddress.emailaddress '),
'conditions' => array(
//'transfers.created_user_id' => $created_user_id
'Transfer.created_user_id' => $created_user_id
),
'joins' => array(
array(
//'table' => 'Emailaddress.emailaddress',
'table' => 'emailaddresses',
'alias' => 'Emailaddress', // add alias
'type' => 'INNER',
'conditions' => array(
'Emailaddress.transfer_id' => 'Transfer.transfer_id',
//'Emailaddress.type' => 'SENDER',
'Emailaddress.type <>' => 'SENDER',
'Emailaddress.received_state' => 'DELIVERED'
)
)
)
));
Read more:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

conditions OR in join cakephp

I am a novice in cake and I'm having trouble with a query I want to do in a tables.
$options['joins'] = array(
array('table'=>'users_views',
'alias' => 'UserView',
'type' => 'inner',
'conditions' => array(
'View.id = UserView.view_id',
'UserView.user_id' => $user_id,
'UserView.like' => 1
),
)
);
$options['order'] = array('View.created' => 'desc');
$options['limit'] = 10;
That's my query so far, but what I need now add an OR condition. In this condition OR need to add
'UserView.user_id' => $user_id,
'UserView.like' => 0
I need the query returns the data that matches or are mine and the LIKE value is 0 if it is very difficult to understand please tell me.
Any help or advice will be well received.
Build the conditions like -
'conditions' => array(
0 => array('View.id = UserView.view_id'),
1 => array('UserView.user_id' => $user_id),
3 => 'UserView.like => 1 OR UserView.like => 0'
),
Or with using OR -
'conditions' => array(
0 => array('View.id = UserView.view_id'),
1 => array('UserView.user_id' => $user_id),
2 => 'UserView.like IN (0, 1)'
),
The following should work:
$options['joins'] = array(
array('table'=>'users_views',
'alias' => 'UserView',
'type' => 'inner',
'conditions' => array(
'View.id = UserView.view_id',
'OR' => array(
'UserView.user_id' => $user_id,
'UserView.like' => 1
)
),
)
);
$options['order'] = array('View.created' => 'desc');
$options['limit'] = 10;

cant paginate with containable

In cakephp I can't get paginate to work with containable.
I followed the docs and I can't get it to work with just 2 tables. Instead I get every associated table.
I get this error as well:
Notice (8): Indirect modification of overloaded property StudentsController::$paginate has no effect
The code works fine with a find all.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#using-containable
$this->Student->Behaviors->load('Containable');
$this->paginate['Student'] = array(
'conditions' => array( 'student_inactive' => false,'Student.student_enq' => true ),
'limit'=>10,
'fields' => array('Student.id,last_name,first_name') ,
'contain' => array('Guardian' => array(
'fields' => array('id', 'guardian_email', 'guardian_first_name', 'guardian_last_name' ))) ,
'order' => 'Student.id'
);
$st = $this->paginate('Student');
array(
(int) 0 => array(
'Student' => array(
'id' => '233',
'student_inactive' => false,
'student_enq' => false,
'student_unallocated' => false,
'first_name' => 'Aadil',
.....
),
'TutoringType' => array(
'id' => '1',
'value' => 'Individual Tuition'
),
'Referral' => array(
'id' => '1',
'title' => 'Google - Organic',
'details' => ''
),
Try with -
$this->Paginator->settings = array(
'conditions' => array( 'student_inactive' => false,'Student.student_enq' => true ),
'limit'=>10,
'fields' => array('Student.id,last_name,first_name') ,
'contain' => array('Guardian' => array(
'fields' => array('id', 'guardian_email', 'guardian_first_name', 'guardian_last_name' ))) ,
'order' => 'Student.id'
);
$st = $this->Paginator->paginate('Student');

Using Containable behavior to filter results in Cakephp 2.2.4

I'm using containable behavior and the result of my find('all') is:
array(
(int) 0 => array(
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Lead' => array(
'id' => '6'
),
'Estimate' => array(
(int) 0 => array(
'lead_id' => '6',
'Estimate' => array(
(int) 0 => array(
'TOT_count' => '2'
)
)
)
)
)
)
I need to to count how many estimates there are in the lead.
The total (2) is correct, but i see nested 'Estimated' array, why ?
The result i would like to get is:
array(
(int) 0 => array(
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Lead' => array(
'id' => '6'
),
'Estimate' => array(
'TOT_count' => '2'
)
)
)
This is the find:
$options = array(
'contain' => array(
'User',
'Estimate' => array(
'fields' => 'COUNT(*) AS TOT_count'
)
),
'conditions' => array('Lead.id' => 6),
'fields' => 'User.*',
'limit' => 1
);
debug($this->Lead->find('all', $options));
How can i do it?
Thanks!
When you use a "custom" AS statement, in your case TOT_count, Cake will always put this in a result key called 0. You can avoid this by defining TOT_count as a virtualField in your model. That way it will be nested directly under the model name in your resultset.
Secondly, the lead_id is forcedly retrieved, because it is "needed" to make the join with the Lead model. It can not properly retrieve all the data without that piece of information there.

Lithium framework multiple 'or' statements in a query (using mongoDB)

I have started work with lithium framework + mongoDB recently. I want to do a really simple query which contains multiple or statements.
I have articles in the DB with publish_up and publish_down fields. I want to fetch only those records/documents which pulbis_down field is highert than now OR null AND publish_up field lower than now OR null.
$items = $article::find('all', array(
'conditions' => array(
'$or' => array(
'$gt' => array('publish_down', $mongoDateNow),
'publish_down' => $mongDateNull
),
'$or' => array(
'$lt' => array('publish_up', $mongoDateNow),
'publish_up' => $mongDateNull
),
)
));
Of course this snippet is wrong hence the second or statement overwrites the first one (because the same array key).
I tried to wrap them into an individual array but gives error.
Any idea?
This query will fetch articles with (publish_down > now OR publish_down = null) AND (publish_up < now OR publish_up = null)
$items = Articles::find('all', array(
'conditions' => array(
'$or' => array(
array('publish_down' => array('$gt' => $mongoDateNow)),
array('publish_down' => null)
),
'$or' => array(
array('publish_up' => array('$lt' => $mongoDateNow)),
array('publish_up' => null)
),
)
));
I don't know about lithium but in PHP you can use multiple $OR as below
$items = $article::find('all', array(
'conditions' => array(
'$or' => array(
array(
'$gt' => array('publish_down', $mongoDateNow),
'publish_down' => $mongDateNull
),
array(
'$lt' => array('publish_up', $mongoDateNow),
'publish_up' => $mongDateNull
)
),
)
));
I think the correct answer is:
'$and' => array(
array(
'$or'=>array(
array('publish_up' => null),
array('publish_up' => array('$lt' => $mongoDateNow))
)
),
array(
'$or'=>array(
array('publish_down' => null),
array('publish_down' => array('$gt' => $mongoDateNow))
)
)
)

Categories