restrict selected columns in cakephp query - sql

I want to reduce the number of fields returned by cakephp's find('all') but don't know if this is possible.
$this->Group->find('all', $params);
where $params
$params = array(
'conditions' => array(
'Group.featured' => 1,
),
'contain' => array(
'User',
'Class' => array(
'conditions' => array(
'Class.exp IS NOT NULL',
'Class.tb <20',
)
)
)
));
The problem is that my Class table has many columns that i don't need and that take a long time to load, so i would line to only select 5 fields.
Can this be done in Cakephp or am i better off writing a regular query?
something like
$params = array(
'conditions' => array(
'Group.featured' => 1,
),
'contain' => array(
'User',
'Class.a',
'Class.b',
'Class.exp',
'Class.tb',
'Class' => array(
'conditions' => array(
'Class.exp IS NOT NULL',
'Class.tb <20',
)
)
)
));
Thank you

That's what the fields parameter is for.
$params = array(
...
'contain' => array(
'Class' => array(
'conditions' => array(
'Class.exp IS NOT NULL',
'Class.tb <20',
),
'fields' => array('a', 'b')
)
)
);

Related

join 3 tables on cakephp

I'm trying to join 3 tables using cakephp but I don't understand why when I execute my query there is the result of the main tables without the attributes from the other 2 tables . this is my php code :
$flightSchedule = $this->FlightSchedules->find('all', array(
'join' => array(
array(
'table' => 'WeeklySchedules',
'alias' => 'ws',
'type' => 'INNER',
'conditions' => array(
'FlightSchedules.code = ws.flight_plan_code'
),
array(
'table' => 'Structures',
'alias' => 's',
'type' => 'LEFT',
'conditions' => array(
'FlightSchedules.code = s.flight_plan_code',
)),
)),
'conditions' => array(
'FlightSchedules.plane_code' => 'xxx'
),
));
the result of this is always the FlightSchedules record without the join of the other tables . Any ideas how to fix this ?
use "fields" for display the data of an associated table .
$flightSchedule = $this->FlightSchedules->find('all', array(
'join' => array(
array(
'table' => 'WeeklySchedules',
'alias' => 'ws',
'type' => 'INNER',
'conditions' => array(
'FlightSchedules.code = ws.flight_plan_code'
),
array(
'table' => 'Structures',
'alias' => 's',
'type' => 'LEFT',
'conditions' => array(
'FlightSchedules.code = s.flight_plan_code',
)),
)),
'conditions' => array(
'FlightSchedules.plane_code' => 'xxx'
),
'fields'=>'FlightSchedules.*,ws.*,s.*'
));
Add this in after condition array
'fields' => 'FlightSchedules.*, WeeklySchedules.*, Structures.*'

Yii, CGridView not showing records of a certain attribute

So I have created records and each has their own particular Service field. They are all filled up (not null).
In the MySQL database, there are also records of that field Service
But in my CGridView, my Service field is all blank. All the Service fields are blank for all the records. When I click on the button View - The one with the magnifying glass, it says Service - Not Set
Can I know what may cause the problem? Please help me Thanks!
UPDATED WITH CGRIDVIEW CODE
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'booking-grid',
'htmlOptions'=>array('class'=>'grid-view grid-size'),
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name'=>'date',
'htmlOptions'=>array('style'=>'text-align:center', 'height'=>'50px', 'width'=>'80px'),
'headerHtmlOptions'=>array('height'=>'30px'),
),
array(
'name'=>'timeStart',
'header'=>"Time",
'htmlOptions'=>array('style'=>'text-align:center', 'width'=>'80px'),
),
array(
'name'=>'client_id',
'value'=>'GxHtml::valueEx($data->client)',
'filter'=>GxHtml::listDataEx(Client::model()->findAllAttributes(null, true)),
'htmlOptions'=>array('width'=>'200px'),
),
array(
'name'=>'service_id',
'value'=>'GxHtml::valueEx($data->service)',
'filter'=>GxHtml::listDataEx(Service::model()->findAllAttributes(null, true)),
),
array(
'name' => 'complete',
'header'=>'Status',
'value' => '($data->complete == 0) ? Yii::t(\'app\', \'Open\') : Yii::t(\'app\', \'Close\')',
'filter' => array('0' => Yii::t('app', 'Open'), '1' => Yii::t('app', 'Close')),
'htmlOptions'=>array('width'=>'60px', 'style'=>'text-align:center'),
),
'remarks',
array(
'class' => 'CButtonColumn',
'visible'=>true,
'htmlOptions'=>array('width'=>'70px'),
'template' => '{view}{update}{delete}{changeComplete}',
'buttons'=>array(
'view' => array(
'visible'=>"true",
),
'update' => array(
'visible'=>"true",
),
'delete' => array(
'visible'=>"UserIdentity::isRoleMember('Admin')",
),
'changeComplete' => array(
'visible' => '$data->complete=="0"',
'imageUrl' => 'images/complete.png',
'options' => array(
'title' => 'Complete',
),
'url' => 'Yii::app()->createUrl("booking/changeComplete", array("id"=>$data->id))',
'click' => "function(){
$.fn.yiiGridView.update(\"booking-grid\", {
type:'POST',
url:$(this).attr('href'),
success:function(data) {
$.fn.yiiGridView.update(\"booking-grid\");
}
})
return false;
}",
),
),
),
))); ?>
I have tried looking at other CGridViews with the same field, they look the same and they're working perfectly fine.
I think the problem is $data->service. Probably your data has a relation with the Service entity. So for accessing attributes of Service you should use $data->service->attributeName.

CakePHP Multiple NOT Find's

I've got the below code which will not 'work'... How can I go about excluding multiple items from the SQL query? I've tried many different combinations without any luck :(
$this->autoLayout = false;
$this->set('songs', $this->CcFile->find('all', array(
'fields' => array(
'track_title',
'artist_name',
'lptime',
'id'
),
'conditions' => array(
'AND' => array(
'NOT' => array(
'artist_name' => 'Jam FM Bed',
),
'NOT' => array(
'artist_name' => 'Airtime Show Recorder',
),
'NOT' => array(
'artist_name' => 'Jam FM Jingles',
),
'NOT' => array(
'artist_name' => 'Kent Scout Jingles',
),
),
),
'order' => array(
'lptime' => 'desc nulls last',
'artist_name' => 'asc'
)
)));
Try that:
'NOT' => array(
'artist_name' => array(
'Jam FM Bed',
'Airtime Show Recorder',
// ...
)
)
or
'artist_name NOT IN' => array(
'Jam FM Bed',
'Airtime Show Recorder',
// ...
)
Try this
fixed code
$this->autoLayout = false;
$this->set('songs', $this->CcFile->find('all', array(
'fields' => array(
'track_title',
'artist_name',
'lptime',
'id'
),
'conditions' => array(
'NOT' => array(
'artist_name' => Array(
'Jam FM Bed',
'Airtime Show Recorder',
'Jam FM Jingles',
'Kent Scout Jingles')
),
),
'order' => array(
'lptime' => 'desc nulls last',
'artist_name' => 'asc'
)
)));

CakePHP many conditions to one field

This code:
$conditions = array(
'fields' => array(
'User.id'
),
'conditions' => array(
'AND' => array(
'UsersProblem.problem_id' => 38,
'UsersProblem.problem_id' => 34,
),
),
'recursive' => -1
);
$conditions['joins'][] = array(
'table' => 'users_problems',
'alias' => 'UsersProblem',
'type' => 'INNER',
'conditions' => array(
'User.id = UsersProblem.user_id',
));
Is transforming to this SQL query:
SELECT `User`.`id`
FROM `lawyers`.`users`
AS `User`
INNER JOIN `lawyers`.`users_problems`
AS `UsersProblem`
ON (`User`.`id` = `UsersProblem`.`user_id`)
WHERE `UsersProblem`.`problem_id` = 34
Where is AND UsersProblem.problem_id = 38"?
How to create correct find condition to get users with some list of problems? Which are linked as many to many relationship.
Using $this->Problem->find is not possible, beause I need to use 2 joins: users_problems and users_practices. And use for them AND condition, like this:
'AND' => array(
'UsersProblem.problem_id' => 38,
'UsersProblem.problem_id' => 34,
'UsersPractices.practice_id' => 1,
'UsersPractices.practice_id' => 2,
),
You're overwriting 'UsersProblem.problem_id'. Just think about it like the array it is. CakePHP is just using the PHP array structure to do these finds, and that means it follows array rules. But to get around that in cakephp, you're supposed to use a two dimensional array
Like this:
'AND' => array(
array('UsersProblem.problem_id' => 38),
array('UsersProblem.problem_id' => 34)
),
Instead of this:
'AND' => array(
'UsersProblem.problem_id' => 38,
'UsersProblem.problem_id' => 34
),
That way, instead of trying to set the key 'UsersProblem.problem_id' twice, you're making an array that looks like
0 -> array('UsersProblem.problem_id' => 38)
1 -> array('UsersProblem.problem_id' => 34)
And that will work, it's a workaround the CakePHP guys built into the system for this kind of situation.
I think you need to use OR instead of AND:
$conditions = array(
'fields' => array(
'User.id'
),
'conditions' => array(
'OR' => array(
array('UsersProblem.problem_id' => 38),
array('UsersProblem.problem_id' => 34),
),
),
'recursive' => -1
);
You can also roll this up as follows:
$conditions = array(
'fields' => array(
'User.id'
),
'conditions' => array(
'OR' => array(
'UsersProblem.problem_id' => array(34, 38)
),
'recursive' => -1
);
Change your conditions array as shown below:
$options = array(
'fields' => array('User.id'),
'conditions' => array('UsersProblem.problem_id' => array(38,34)),
'recursive' => -1
);
$options['joins'] = array(
array(
'table' => 'users_practices',
'alias' => 'UsersPractices',
'type' => 'INNER',
'conditions' => array(
/CONDITION OF JOIN/
),
array(
'table' => 'users_problems',
'alias' => 'UsersProblem',
'type' => 'INNER',
'conditions' => array(
'User.id = UsersProblem.user_id',
)
);
From User Model:
$this->find('list',$options);
Do not use AND/ OR because they are not optimized for above case.
For reference:
MYSQL OR vs IN performance
and
CakePHP right way to do this (get value from setting's table)
**Try the code for join tables in cakephp.......**
$options = array('joins' => array(
array(
'table' => 'modelname1',
'alias' => 'modelName1',
'type' => 'LEFT',
'foreignKey' => false,
'conditions' => array('modelname1.id = modelname2.mid')
)
),
'fields' => array('modelName1.field1', 'modelName1.field2', 'modelName2.field1', 'modelName2.field2'),
'conditions' => array('modelname.id' => 1, 'modelname2.field1' => ''),
'limit' => 10, 'page' => 1);
$this->modelName2->find($options);

try to implement the join query in Cakephp

i am working on a cakephp and try to implement the join . or inner join query ... what i am doing right now is this
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
)
)
)
), false);
return $message_details = $this->find('all', array(
'conditions' => array(),
'fields' => array('DISTINCT mobileNo')
));
this query is doing LEFT JOIN the table .. what i want is join or inner join between two tables
You can specify the type of join in your belongsTo configuration, as stated in the documentation. The default is left, but you can use any valid join type. Simply add 'type' => 'inner' to the configuration array, so you should get something like this:
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
),
'type' => 'inner' // Simply add this
)
)
), false);
Alternatively, you can add the joins to your query without using bingModel:
return $message_details = $this->find('all', array(
'conditions' => array(),
'fields' => array('DISTINCT mobileNo'),
'joins'=>array(
array(
'table'=>'contacts,
'alias'=>'Contact',
'type'=>'INNER',
'conditions'=>array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
)
)
)
));