cakephp null value on condition - cakephp

$this->paginate['Member'] = array(
'conditions' => array($conditions,'Member.division_id' => $currentTeam['Division']['id'],'Member.team_id'=> array(null,0)),
'group' => 'Member.id',
);
This query does not get the NULL team id.. It gets just the team id 0.

Using the IS NULL keyword usually works.
array(
$conditions,
'Member.division_id' => $currentTeam['Division']['id'],
'OR' => array(
'Member.team_id' => 0,
'Member.team_id IS NULL'
)
)

Can you try this?
$this->paginate['Member'] = array(
'conditions' => array($conditions,
'Member.division_id' => $currentTeam['Division']['id'],
'OR'=> array(
array('team_id'=>null),
array('team_id'=>0),
),
'group' => 'Member.id',
);
Edit: Wrapped each team_id condition in it's own array. This prevents the issue of a duplicated 'team_id' array key, while still using a consistent format for the conditions.

array(
$conditions,
'Member.division_id' => $currentTeam['Division']['id'],
'OR' => array(
'Member.team_id' => 0,
'IS' => array( 'Member.team_id' => NULL )
)
)
This is what you want.

$this->paginate['Member'] = array(
'conditions' => array($conditions,
'Member.division_id' => $currentTeam['Division']['id'],
'OR'=> array('Member.team_id'=> null, 'Member.team_id'=> 0),
'group' => 'Member.id',
);

Related

TableAlias__ColumnAlias syntax in virtual fields not being parsed as documented

Docs state that we can name calculated fields using a syntax like Timelog__TotalHours and PHP will parse it as:
[0] => Array
(
[ProductsItems] => Array
(
[Total] => 50
)
)
In my CakePHP/2.9.4 set up I'm trying it in several places, such as pagination, but I get the column name handled as any other string:
[0] => Array
(
[ProductsItems__Total] => 50
)
My test code in the controller is:
$this->Paginator = $this->Components->load(
'Paginator',
array(
'fields' => array(
"CONCAT_WS(' ', Usuario.nombre, Usuario.apellidos) AS Usuario__Empleado",
),
'contain' => array(
'Usuario',
),
'limit' => 2,
)
);
debug($this->Paginator->paginate());
... and prints:
array(
(int) 0 => array(
(int) 0 => array(
'Usuario__Empleado' => 'Juan García'
),
'Usuario' => array(
'id' => '56'
)
),
(int) 1 => array(
(int) 0 => array(
'Usuario__Empleado' => 'María López'
),
'Usuario' => array(
'id' => '385'
)
)
)
Do I need explicitly to enable this feature somewhere?
First you should define the virtual field before your query (can be model or controller).
$this->CurrentModel->Usuario->virtualFields['Empleado'] = 0;
$this->Paginator = $this->Components->load(
'Paginator',
array(
'fields' => array(
"CONCAT_WS(' ', Usuario.nombre, Usuario.apellidos) AS Usuario__Empleado",
),
'contain' => array(
'Usuario',
),
'limit' => 2,
)
);
debug($this->Paginator->paginate());
It should work.

How to assign mysql functions on join fields in Cakephp

I want to get last_deadline and the count of instalments of all instalments but obviously this query will show me 1 order and the last_deadline.
$orders = $this->find('all', array(
'fields' => array(
'Order.order_id', 'Order.summary', 'Order.fee',
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline'
),
'conditions' => $conditions,
'joins' => array(
array(
'table' => $this->getTableName('default', 'brief_instalments'),
'alias' => 'BriefInstalment',
'type' => 'RIGHT',
'conditions' => array(
'Order.order_id = BriefInstalment.order_id',
'BriefInstalment.deleted' => 0,
),
'order' => 'BriefInstalment.deadline ASC',
)
),
'order' => 'BriefInstalment.deadline ASC'
));
I have tried 'contain' and doesn't work.
'contain' => array(
'BriefInstalment' => array(
'fields' => 'BriefInstalment.id',
'fields' => array(
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline', 'COUNT(`BriefInstalment`.`id`) AS total_instalments'
),
'conditions' => array(
'BriefInstalment.deleted' => 0
)
)
),
By the way I don't want to use loop to get last_intalments and cout brief_instalments. e.g.
// Determine deadlines
foreach ($orders as $i => $order) {
$deadline = $this->BriefInstalment->getLastDeadline($order['Order']['order_id']);
$orders[$i] += array(
...
'last-deadline' => $deadline,
'total-instalments' => count($order['BriefInstalment'])
);
}
The reason is it decrease the speed of loading.
Any help plz
Here is how to create custom query
$conditionsSubQuery['"User2"."status"'] = 'B';
$db = $this->User->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('"User2"."id"'),
'table' => $db->fullTableName($this->User),
'alias' => 'User2',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->User
);
$subQuery = ' "User"."id" NOT IN (' . $subQuery . ') ';
$subQueryExpression = $db->expression($subQuery);
$conditions[] = $subQueryExpression;
$this->User->find('all', compact('conditions'));
Reference:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries

CakePHP 2.1 Custom Pagination

I am using cakephp 2.1 and defined 3 tables as follows.
`industries(id, name);`
`movies(id, name, industry_id),`
`trailers(id, name, movie_id);`
I want to paginate the trailers for particular industry. So the code I have written is below:
$this->paginate = array(
'Industry' => array(
'contain' => array(
'Movie' => array(
'order' => 'Movie.release DESC',
'Trailer' => array(
'conditions' => array(
'Trailer.id !=' => $trailer_id
)
)
)
),
'conditions' => array(
'Industry.id' => $id
)
)
);
If I would specify 'limit' for 'Trailer', I get correct number of results but some null array for a movie which doesn't contain any trailers. Please help me to get the number of specified limit of trailers for particular industry. The work will be most appreciated.
It looks to me like you've got your contain key in the wrong place. It should be defined before the models.
<?php
$this->paginate = array(
'contain' => array(
'Industry' => array(
'Movie' => array(
'order' => 'Movie.release DESC',
'Trailer' => array(
'conditions' => array(
'Trailer.id !=' => $trailer_id
)
)
)
)
),
'conditions' => array(
'Industry.id' => $id
)
);

How do I get CakePHP 2.2.4 to contain and return data for 4th level association?

I have four models (Location->StatisticCategory->StatisticItem->Statistic) that I'm trying to return in a single query. The results are as expected until the query starts at the level of Location. At that point the data return contains everything down to the 3rd association in StatisticItem, but every single Statistic is null. Any idea why 4th level associations (at least here) are not returning any data?
Below is the code I'm using in my StatisticsController with Containable behavior turned on in AppModel:
$stats = $this->Location->find('all', array(
'fields' => array(
'Location.id',
'Location.location'
),
'conditions' => array(),
'recursive' => 2,
'contain' => array(
'StatisticCategory' => array(
'fields' => array(
'StatisticCategory.id',
'StatisticCategory.location_id',
'StatisticCategory.category'
),
'StatisticItem' => array(
'fields' => array(
'StatisticItem.id',
'StatisticItem.statistic_category_id',
'StatisticItem.item'
),
'Statistic' => array(
'fields' => array(
'Statistic.id',
'Statistic.statistic_item_id',
'Statistic.date',
'Statistic.number'
),
'conditions' => array(
'Statistic.date' => $date
)
),
'order' => array('StatisticItem.item')
),
'order' => array('StatisticCategory.category')
)
),
'order' => array('Location.location')
));
The documentation for containable describes how to contain deeper associations. The following should be about what you're looking for:
$this->Location->find('all', array(
'contain' => array(
'StatisticCategory' => array(
'StatisticItem' => array(
'Statistic'
)
)
)
));

find all albums by ids + find all non-private how to?

I search for all albums by id using:
$this->Album->find('all', array(
'conditions' => array(
'Album.id' => $albums_ids,
'Album.galleries_id' => $id
)
));
But I also would like to find all non-private albums (private == 0) as well. I tried:
$this->Album->find('all', array(
'conditions' => array(
'Album.id' => $albums_ids,
'Album.galleries_id' => $id,
'OR'=> array(
array('Album.private' => 0),
array('Album.galleries_id' => $id)
)
)
));
but no success...
it should be gallery_id
$this->Album->find('all', array(
'conditions' => array(
'Album.galleries_id' => $id,
'OR'=> array(
'Album.private' => 0,
'Album.id' => $albums_ids
)
)
));
Would you not simply be looking for the following logic instead?
$this->Album->find('all', array(
'conditions' => array(
'Album.id' => $albums_ids,
'Album.galleries_id' => $id,
'Album.private' => 0,
)
));

Resources