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

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,
)
));

Related

Cakephp join table in my View

I need you help please!
My view didn't work with my INNER JOIN
public function view($bk_id = null)
{
$data = $this->Book->findBybk_id($bk_id, array(
'recursive' => -1,
'fields' => array('Book.*', 'Article.*'),
'joins' => array(
array(
'table' => 'Articles',
'alias' => 'Article',
'type' => 'INNER',
'conditions' => array('Article.bk_id = Book.bk_id')
)
)
));
$this->set('Book', $data);
}
Can anyone help me?
You appear to be using Cake's findBy magic functions which don't take a second parameter. Try using find('first') with the relevant conditions instead:-
$data = $this->Book->find('first', array(
'recursive' => -1,
'fields' => array('Book.*', 'Article.*'),
'joins' => array(
array(
'table' => 'Articles',
'alias' => 'Article',
'type' => 'INNER',
'conditions' => array('Article.bk_id = Book.bk_id')
)
),
'conditions' => array(
'Book.bk_id' => $bk_id
)
));

cakePHP Paginate and sort by DESC

At the moment i query my db with this query below and it works great , it joins with another table.
$tapplicant = $this->Tapplicant->find(
'all',
array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'order' => array('Tapplicant.AppDate' => 'DESC'),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'limit' => 15
)
);
I have setup a Pagenator :
public $paginate = array (
'order' => array('Tapplicant.AppID' => 'desc'),
'limit' => 15,
);
What i need to know is how do i :
Add Paginator
Using on the current date CURDATE()
I jsut dont know where to add it in.
In your controller:-
$this->paginate = array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'conditions' => array(
'Tapplicant.AppDate' => date('Y-m-d')
),
'order' => array('Tapplicant.AppDate' => 'DESC'),
'limit' => 15
);
$this->set('tapplicant', $this->paginate());
You can do this right in the find:
$tapplicant = $this->Tapplicant->find(
'all',
array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'limit' => 15,
'order' => array('Tapplicant.AppID' => 'desc'),
)
);
And leave paginate like this:
public $paginate = array('limit' => 15);
If you only want the posts from the current day, just add this to the find:
'conditions' => array('Tapplicant.created' => date('Y-m-d')),

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
)
);

cakephp contain - condition

my problem is that can i give condition [ 'BookTitleMaster.id' => $xtitid, ] as like below
$bbookinfs = $this->BookStockin->BookIssue->find('all', array(
'conditions' => array('return_status' => 2),
'contain' => array(
'BookStockin' => array(
'BookTitleMaster' => array(
'BookTitleMaster.id' => $xtitid,
'fields' => array('id','title','sub_title','book_material_type_id','book_author_id','course_detail_id','isbn_no','book_publisher_id','pub_year','pub_place','desc','no_pages','volume'),
'BookMaterialType' => array('name'),
'CourseDetail' => array('name'),
'BookPublisher' => array('name'),
'BookAuthor' => array('name')
)
)
)
));
I believe that you miss is:
'conditions' => array('BookTitleMaster.id' => $xtitid),
So, your final code should be:
$bbookinfs = $this->BookStockin->BookIssue->find('all', array(
'conditions' => array('return_status' => 2),
'contain' => array(
'BookStockin' => array(
'BookTitleMaster' => array(
'conditions' => array('BookTitleMaster.id' => $xtitid),
'fields' => array('id','title','sub_title','book_material_type_id','book_author_id','course_detail_id','isbn_no','book_publisher_id','pub_year','pub_place','desc','no_pages','volume'),
'BookMaterialType' => array('name'),
'CourseDetail' => array('name'),
'BookPublisher' => array('name'),
'BookAuthor' => array('name')
)
)
)
));
HTH
Shouldn't it be:
'conditions' => array('BookTitleMaster.id' => $xtitid),

Resources