Paging in cakePHP not working with page - php

I am using following query to get details.
$moods = $this->Mood->find('all', array('conditions'=>$conditions,
'joins' => array(
array(
'table' => 'users',
'alias' => 'user',
'type' => 'INNER',
'conditions' => array(
'user.id = Mood.userId',
)
)
),
'fields' => array('user.id','user.name','user.email','user.imagePath',
'Mood.id', 'Mood.moods', 'Mood.body', 'Mood.created', 'Mood.group', 'Mood.mediaName', 'Mood.mediaType', 'Mood.isPrivate'),
'order' => array('Mood.created DESC'),
'limit' => 10,
'page' => $offset
));
However the page is not workign no mater what offset i put the result is same , what is going on

hope this help
$this->Paginator->settings = array('conditions'=>$conditions,.........);
$moods = $this->Paginator->paginate('Mood');

Related

Custom query pagination in cakephp

I have a query as below in controller with multiple joins
$deals = $this->Deals->find('all', array(
'fields' => array(
'Deals.id',
'Deals.deed_amount',
'Deals.other_amount',
'Deals.cash_amount',
'Deals.total_amount',
'Deals.client_id',
'Deals.project_id',
'Deals.property_id',
'Deals.properties_flat_id',
'Deals.date',
'Deals.status',
'Deals.invoice_no',
'Deals.remain_cash_amount',
'Deals.remain_deed_amount',
'Deals.remain_other_amount',
'Deals.remain_deed_amount',
'Property.name',
'Client.name',
'PropertyFlat.name',
'PropertyFlat.status'
),
'joins' => array(
array(
'table' => 'properties',
'alias' => 'Property',
'type' => 'LEFT',
'conditions' => array(
'Deals.property_id = Property.id'
)
),
array(
'table' => 'clients',
'alias' => 'Client',
'type' => 'LEFT',
'conditions' => array(
'Deals.client_id = Client.id'
)
),
array(
'table' => 'properties_flats',
'alias' => 'PropertyFlat',
'type' => 'LEFT',
'conditions' => array(
'Deals.properties_flat_id = PropertyFlat.id'
)
)
),
// 'conditions'=>$condition
));
I want to add pagination to the view. I tried following
$this->set('deals', $this->Paginator->paginate($deals));
But no use. Please help me in how to add pagination for the custom query. I searched many links but did not get any help.
You can try like this:
$paginate = [
'joins' => array(
array(
'table' => 'properties',
'alias' => 'Property',
'type' => 'LEFT',
'conditions' => array(
'Deals.property_id = Property.id'
)
),
array(
'table' => 'clients',
'alias' => 'Client',
'type' => 'LEFT',
'conditions' => array(
'Deals.client_id = Client.id'
)
),
array(
'table' => 'properties_flats',
'alias' => 'PropertyFlat',
'type' => 'LEFT',
'conditions' => array(
'Deals.properties_flat_id = PropertyFlat.id'
)
)
),
'fields' => array(
'Deals.id',
'Deals.deed_amount',
'Deals.other_amount',
'Deals.cash_amount',
'Deals.total_amount',
'Deals.client_id',
'Deals.project_id',
'Deals.property_id',
'Deals.properties_flat_id',
'Deals.date',
'Deals.status',
'Deals.invoice_no',
'Deals.remain_cash_amount',
'Deals.remain_deed_amount',
'Deals.remain_other_amount',
'Deals.remain_deed_amount',
'Property.name',
'Client.name',
'PropertyFlat.name',
'PropertyFlat.status'
)
];
$this->set('deals', $this->Paginator->paginate($this->Deals->find('all'), $paginate)->toArray());
Firstly, I would recommend to rethink the way you have constructed your query. Why don't you just use the contain() method to retrieve the associated data?
Secondly, You can use custom finders with the paginator. Just create one in your Table class and return constructed query from it and ask the paginator to use it. Read more here. https://book.cakephp.org/3.0/en/controllers/components/pagination.html

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

cant convert a find all over many records to paginatein cakephp

I cant convert this find all to a paginate statement. The find all works fine as below. Then I converted this to paginate with error
Column not found: 1054 Unknown column 'Lesson.id' in 'on clause'. Not sure what to do as I have done paginate before without issues.
$lessons = $this->find('all', array(
// 'conditions' =>$default_array,
'joins' => array(
array('table' => 'lessons_students',
'alias' => 'LessonStudents',
'type' => 'INNER',
'conditions' => array(
'LessonStudents.student_id' => $studentId,
'LessonStudents.lesson_id = Lesson.id'
)
)
),
'group' => 'Lesson.id',
'order' => array('Lesson.lesson_date' => 'asc'),
));
$this->Paginator->settings = array(
// 'conditions' =>$default_array,
'joins' => array(
array('table' => 'lessons_students',
'alias' => 'LessonStudents',
'type' => 'INNER',
'conditions' => array(
'LessonStudents.student_id' => $id,
'LessonStudents.lesson_id = Lesson.id'
)
)
),
'group' => 'Lesson.id',
'order' => array('Lesson.lesson_date' => 'asc'),
);
$lessons =$this->Paginator->paginate('Student'); //also tried lesson
the join should cal a variable which does work. I solved it this way

HABTM Cake php find

I have tables below with HABTM associations.
user, group , groups_users
group , project, projects_groups
public $hasAndBelongsToMany = array(
'Group' =>
array(
'className' => 'Group',
'joinTable' => 'groups_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'group_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => ''
)
);
I am trying to get all users, as a list for checkboxes, that are on a project using project id using query below
$users = $this->User->Group->Project->find('list',
array(
'conditions' => array('Project.id' => $this->Session->read('Projectid'))
,'contain' => array(
'User' => array( 'fields' => 'User.id', 'User.email')
)
)
);
But error below
Model "Project" is not associated with model "User"
I have actually tried different ways to get this working but cant seem to get it right, how can i achieve what i want. cheers
I achieved what i wanted with joins , is there a way to do this by using model associations, instead of the manual joins option?
$options['joins'] = array(
array('table' => 'groups_users',
'alias' => 'gu',
'type' => 'LEFT',
'conditions' => array(
'gu.user_id = User.id',
)
),
array('table' => 'projectGroups',
'alias' => 'pg',
'type' => 'LEFT',
'conditions' => array(
'pg.group_id = gu.group_id',
)
)
);
$this->User->recursive = -1;
$options['conditions'] = array(
'pg.project_id = ' => $this->Session->read('Projectid'),
'User.Active = ' => true
);
$users = $this->User->find('list', $options);

Cakephp bug : in paginator joins generated query

When below code below it generate query with single quote in join query
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'businesses_categories',
'alias' => 'BusinessesCategory',
'type' => 'LEFT',
'conditions' => array('Business.id' => 'BusinessesCategory`.`business_id'),
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'LEFT',
'conditions' => array('BusinessesCategory.category_id' => 'Category.id'),
),
),
'conditions' => array(
'Category.id' => 24),
'limit' => 10
);
$businesses = $this->Paginator->paginate('Business');
I have added query that's generated by above paginator query. query works fine, when i use ON (Business.id = BusinessesCategory.business_id) instead of ON (Business.id = 'BusinessesCategory.business_id')
how do i fix this. so, it does not include single quote on values
SELECT `Business`.`id`,
`Business`.`state`,
`Business`.`slug`,
`Business`.`city`,
`Business`.`suburb`,
`Business`.`user_id`,
`Business`.`business_name`,
`Business`.`business_address`,
`Business`.`business_postal`,
`Business`.`business_postal_id`,
`Business`.`business_phone`,
`Business`.`business_phone1`,
`Business`.`business_phone2`,
`Business`.`business_email`,
`Business`.`business_website`,
`Business`.`business_details`,
`Business`.`business_openinghours`,
`Business`.`business_service`,
`Business`.`business_addtionalinfo`,
`Business`.`business_lat`,
`Business`.`business_lng`,
`Business`.`identity`,
`Business`.`status`
FROM `yuldicom`.`businesses` AS `Business`
LEFT JOIN `yuldicom`.`businesses_categories` AS `BusinessesCategory` ON (`Business`.`id` = 'BusinessesCategory`.`business_id')
LEFT JOIN `yuldicom`.`categories` AS `Category` ON (`BusinessesCategory`.`category_id` = 'Category.id')
WHERE `Category`.`id` = 24 LIMIT 10
Here you go :--
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'businesses_categories',
'alias' => 'BusinessesCategory',
'type' => 'LEFT',
'conditions' => array('Business.id=BusinessesCategory.business_id'),
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'LEFT',
'conditions' => array('BusinessesCategory.category_id=Category.id'),
),
),
'conditions' => array(
'Category.id' => 24),
'limit' => 10
);
$businesses = $this->Paginator->paginate('Business');

Categories