cakephp contain - condition - cakephp

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

Related

How to get rid of null associated model in cakephp 2.x

I am trying to get the item types that Order.Item.ItemType.show_type = 1. I have written the query but I want to show only Items that their ItemType.show_type = 1 not all items.
$brief = $this->Order->find('first', array(
'fields' => array(
'Order.*'
),
'conditions' => array(
'Order.order_id' => $orderId,
),
'contain' => array(
'Item' => array(
'fields' => array(
'Item.*', 'CHAR(64 + Item.num) AS letter'
),
'conditions' => array(
'Item.deleted' => 0,
),
'ItemType' => array(
'conditions' => array(
'ItemType.show_type' => 1
),
)
),
)
));
The query shouldn't show Item id = 25741
Associations:
// Order
public $hasMany = array(
'BriefInstalment' => array(
'foreignKey' => 'order_id'
)
);
// Item Model
public $belongsTo = array(
'Order',
'ItemType' => array(
'type' => 'inner'
)
);
// ItemType Model
public $hasMany = array('Item');
Print:
array(
'Order' => array(
'order_id' => '67817',
'service' => '',
),
'Item' => array(
(int) 0 => array(
'id' => '25741',
'order_id' => '67817',
'num' => '2',
'item_type_id' => '8',
'name' => '3-5 titles active',
'deleted' => false,
'ItemType' => array(), // <= how to remove this empty model
'Item' => array(
(int) 0 => array(
'letter' => 'B'
)
)
),
(int) 1 => array(
'id' => '25742',
'order_id' => '67817',
'num' => '3',
'item_type_id' => '2',
'name' => '1,000 pro active',
'deleted' => false,
'ItemType' => array(
'id' => '2',
'name' => 'Part Instalment',
'show_type' => true,
'deleted' => false
),
'Item' => array(
(int) 0 => array(
'letter' => 'C'
)
)
)
)
)
This could not be done using Countaible behaviour, but iwth the joins method, set the recursive to -1
$brief = $this->Order->find('first', array(
'recursive' => -1,
'fields' => array(
'Order.*'
),
'conditions' => array(
'Order.order_id' => $orderId,
),
'joins' => array(
array(
'table' => 'items',
'alias' => 'Item',
'type' => 'inner',
'conditions' => array(
'Order.id = Item.order_id'
)
),
array(
'table' => 'item_types',
'alias' => 'ItemType',
'type' => 'inner',
'conditions' => array(
'ItemType.id = Item.item_type_id'
)
),
)
));
You have to check if the table names are correct and also the foreign keys names.
Another solution would be to go through your results, and unset the empty ones
foreach($brief as $k => $v){
foreach($v['Item'] as $kk => $vv){
if(empty($vv['ItemType'])){
unset($brief[$k]['Item'][$kk];
}
}
}
debug($brief);

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

CakePHP Upload plugin – not creating thumbnail

I use the Upload plugin from https://github.com/josegonzalez/cakephp-upload
The problem is, while the main image is uploaded fine, it is not creating thumbnails.
Here is my model code
public $actsAs = array(
'Upload.Upload' => array(
'image' => array(
'thumbnailSizes' => array(
'featured' => '720x400',
'xsmall' => '98x98',
'small' => '152x110',
'medium' => '400x222',
'large' => '225x145',
'medium_home' => '232x128',
'xlarge' => '720x632',
'editorial' => '199x300',
'medium_editorial' => '180x249',
'small_editorial' => '152x211',
'xsmall_editorial' => '98x136'
),
'path' => '{ROOT}webroot{DS}uploads{DS}{model}{DS}{field}{DS}'
)
)
);
Any idea what should I change?
Add 'thumbnails' => true and also 'thumbnailMethod' => 'php' in you array and the code will look like:
public $actsAs = array(
'Upload.Upload' => array(
'image' => array(
'thumbnails' => true,
'thumbnailMethod' => 'php',
'thumbnailSizes' => array(
'featured' => '720x400',
'xsmall' => '98x98',
'small' => '152x110',
'medium' => '400x222',
'large' => '225x145',
'medium_home' => '232x128',
'xlarge' => '720x632',
'editorial' => '199x300',
'medium_editorial' => '180x249',
'small_editorial' => '152x211',
'xsmall_editorial' => '98x136'
),
'path' => '{ROOT}webroot{DS}uploads{DS}{model}{DS}{field}{DS}'
)
)
);
I used the code below and it works fine for me:
public $actsAs = array(
'Upload.Upload' => array(
'photo' => array(
'fields' => array(
'dir' => 'photo_dir'
),
'deleteOnUpdate' => true,
'thumbnails' => true,
'thumbnailSizes' => array(
'64x64' => '64x64'
),
'thumbnailMethod' => 'php'
)
)
);

Cakephp group by and order by together not working

I am trying to get the employees and their posting details with largest posting_from date.
My query is:
$emp = $this->EmployeePersonal ->find(
'all',
array(
'fields' => array('EmployeePersonal.*', 'PermanentDist.name','PresentDist.name','EmployeePosting.*','Designation.name','Department.name','Office.name' ),
'conditions' => $condition,
//'order' => array('Designation.id'),
'group' => 'EmployeePersonal.id',
'order' => 'EmployeePosting.posting_from DESC',
'recursive' => -1,
'joins' => array(
array(
'table' => 'employee_postings',
'alias' => 'EmployeePosting',
'type' => 'LEFT',
'conditions' => array(
'EmployeePosting.employee_personal_id = EmployeePersonal.id',
)
),
)
)
);
But the above query shows the lowest posting_from value. Why isn't the order working in my case?
some commas and will be ready:
$emp = $this->EmployeePersonal ->find(
'all',
array(
'joins' => array(
array(
'table' => 'employee_postings',
'alias' => 'EmployeePosting',
'type' => 'LEFT',
'conditions' => array(
'EmployeePosting.employee_personal_id = EmployeePersonal.id'
)
)
),
'fields' => array('EmployeePersonal.*', 'PermanentDist.name','PresentDist.name','EmployeePosting.*','Designation.name','Department.name','Office.name'),
'conditions' => $condition,
//'order' => array('Designation.id'),
'group' => 'EmployeePersonal.id',
'order' => array(
'EmployeePosting.posting_from' => 'DESC'
),
'recursive' => -1
)
);

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