When i use 'group' in cakephp , pagination not work - php

I have model IndividualSystemSerialNumber in cakephp1.3 and when i try to group by mac_address pagination not working.
But when i remove group from paginate , it's working fine.
$this->paginate = array(
'order' => $order_by,
'group' => 'IndividualSystemSerialNumber.mac_address',
'page' => intval($page),
'limit' => $this->params['url']['iDisplayLength']
);
$systems = $this->paginate('IndividualSystemSerialNumber');

I had same problem as you have now i don't know but paginate has some problem when you use group option.
So here is alternative solution that will work just awesome.
Example
$systems = $this->IndividualSystemSerialNumber->find('all',array
(
'order' => $order_by,
'group' => 'IndividualSystemSerialNumber.mac_address',
'page' => intval($page),
'limit' => $this->params['url']['iDisplayLength'],
'fields' => array
(
'IndividualSystemSerialNumber.mac_address',
//other related fields.
)
));
You have $page that is number of page you are requesting and Model::find() has option called page that accept currently requested page number and you also have limit set so above code will work as $this->paginate();
Cheers Enjoy Coding.

Related

Group By within contain cakephp

Hellow , I want to use group by within contain in cakephp. In the following case i want to take only distinct organization within organizationUser array..
$options = array(
'conditions' => array('User.' .$this->User->primaryKey => $userId),
'contain' => array(
'OrganizationUser'=>array(
'conditions'=>['status'=>3],
'group'=> array( 'OrganizationUser.organization_id')),
'OrganizationUser.Organization',
'OrganizationUser.Organization.Noticeboard',
'OrganizationUser.Organization.Newsboard',
'OrganizationUser.Organization.Noticeboard.Branch',
),
'page'=>$page,
'limit'=>$limit
);
$org = $this->User->find('all', $options);
But this is throwing error like 'Column not found', and 'conditions' is working fine within OrganizationUser but 'group' not working.I am using cakephp version 2.Thanks in advance.
I don't think cakephp 2+ offer something like you are doing to make field distinct within contain. So better to try following..
Replace :
'group'=> array( 'OrganizationUser.organization_id')
By
'fields'=> array( 'DISTINCT OrganizationUser.organization_id')
that might work for you.
In my case, I'm using cake version 4+.
In my table the relation I've made
$this->belongsTo('CreatedOperator')
->setClassName(USERS_PLUGIN . '.Users')
->setForeignKey('created_by')
->setJoinType('INNER')
;
and I'm calling the relation like
$query
->disableHydration()
->select([
'CreatedOperator.id',
'CreatedOperator.first_name',
'CreatedOperator.last_name',
'full_name' => $query->func()->concat(['CreatedOperator.first_name' => 'identifier',' ','CreatedOperator.last_name' => 'identifier']),
'total' => $query->func()->count('CreatedOperator.id')])
->contain(['CreatedOperator'])
->group(['CreatedOperator.id'])
;
return $query->toList();

get_comments not working properly after WordPress v4.4 update

I just found a wired thing on my website after v4.4 update. In my blog pages I use 2 functions to fetch comments.
First I use
wp_list_comments( array( "callback" => "checkout_comment", "type" => "comment") );
To fetch the comments only. Then I run a count check if any trackbacks exists by using
$trackback_count = get_comments( array(
'status' => 'approve',
'post_id'=> get_the_ID(),
'type'=> 'pings',
'count' => true)
);
and if the do exists the I show the trackbacks/pingsbacks like this
wp_list_comments( array( "callback" => "checkout_comment", "type" => "pings", "reply_text" => null, "format" => "html5") );
Now, after v4.4 update the comments are showing fine but the trackbacks list are not showing.
Can anyone tell me why? What I might need to change to fix this?

How to get which pagination is getting navigated to another page if two paginations in one page view Cakephp

I have recently built some admin dashboard for managing projects and blog posts together. I wanted to put both in one dashboard and show as two tables with pagination. So in controller, I put something like this:
$this->Paginator->settings = array(
'Project'=>array(
'limit' => 10,
'order' => array(
'Project.created_time' => 'desc'
),
'paramType' => 'querystring'
),
'Post'=>array(
'limit' => 10,
'order' => array(
'Post.created_time' => 'desc'
),
'paramType' => 'querystring'
)
);
$projects = $this->Paginator->paginate('Project');
$posts = $this->Paginator->paginate('Post');
$this->set('projects', $projects);
$this->set('posts', $posts);
and in the view I use
<?php echo $this->Paginator->numbers(array('model'=>'Post')); ?>
and
<?php echo $this->Paginator->numbers(array('model'=>'Project')); ?>
to manage the pages link of each model.
But the problem is when I click one page link, the other one is changing to that page as well. The url only give the paged link as dashboard?page=5
Is there anyway that I can access them separately? Like dashboard?project_page=5 ordashboard?post_page=5.
Thanks for any ideas.

CakePHP paginate and order by

It feels like I've tried everything so I now come to you.
I am trying to order my data but it isn't going so well, kinda new to Cake.
This is my code:
$this->set('threads', $this->paginate('Thread', array(
'Thread.hidden' => 0,
'Thread.forum_category_id' => $id,
'order' => array(
'Thread.created' => 'desc'
)
)));
It generates an SQL error and this is the last and interesting part:
AND `Thread`.`forum_category_id` = 12 AND order = ('desc') ORDER BY `Thread`.`created` ASC LIMIT 25
How can I fix this? The field created obviously exists in the database. :/
You need to pass in the conditions key when using multiple filters (i.e. order, limit...). If you just specify conditions, you can pass it as second parameter directly.
This should do it:
$this->set('threads', $this->paginate('Thread', array(
'conditions' => array(
'Thread.hidden' => 0,
'Thread.forum_category_id' => $id
),
'order' => array(
'Thread.created' => 'desc'
)
)));
or perhaps a little clearer:
$this->paginate['order'] = array('Thread.created' => 'desc');
$this->paginate['conditions'] = array('Thread.hidden' => 0, ...);
$this->paginate['limit'] = 10;
$this->set('threads', $this->paginate());
if you get an error, add public $paginate; to the top of your controller.
Try
$this->set('threads', $this->paginate('Thread', array(
'Thread.hidden' => 0,
'Thread.forum_category_id' => $id
),
array(
'Thread.created' => 'desc'
)
));
I'm not a Cake master, just a guess.
EDIT. Yes, thats right. Cake manual excerpt:
Control which fields used for ordering
...
$this->paginate('Post', array(), array('title', 'slug'));
So order is the third argument.
try
$all_threads = $this->Threads->find('all',
array(
'order' => 'Threads.created'
)
);
$saida = $this->paginate($all_threads,[
'conditions' => ['Threads.hidden' => 0]
]);
There are a few things to take note of in paginate with order. For Cake 3.x, you need :
1) Ensure you have included the fields in 'sortWhitelist'
$this->paginate = [
'sortWhitelist' => [
'hidden', 'forum_category_id',
],
];
2) for 'order', if you put it under $this->paginate, you will not be able to sort that field in the view. So it is better to put the 'order' in the query (sadly this wasn't stated in the docs)
$query = $this->Thread->find()
->where( ['Thread.hidden' => 0, 'Thread.forum_category_id' => $id, ] )
->order( ['Thread.created' => 'desc'] );
$this->set('threads', $this->paginate($query)

Specifying record criteria on more than one model in one pagination call

I am stuck on pagination in CakePHP 1.3. I am trying to paginate feePayment records based on certain criteria.
feePayments belongs to Students which in turn belongs YearGroups.
I want to paginate 'unpaid' feePayments for each year group. The problem I am having is that the SQL query seems to only take into account the conditions I specified for the FeePayment model and ignores the YearGroup criteria so only overdue unpaid records are returned regardless of the year group specified.
Here is my code:
function unpaidClass($id) {
$this->paginate = array(
'FeePayment' => array ('recursive' => 1, 'conditions' => array('FeePayment.status' => 'Unpaid', 'FeePayment.due_date <= ' => date("Y-m-d"))),
'YearGroup' => array ('recursive' => 1, 'conditions' => array('YearGroup.school_year' => $id))
);
$this->set('feePayments', $this->paginate());
}
Hope this makes sense, appreciate any help.
Thanks,
Sid.
You should consider using the Containable behavior. This behavior allows you to group the necessary data you want without relaying on the "recursiveness" of your query. You can place conditions on your contained data similar to the way you would in your queries and is a more permanent way to structure data across your application instead of specifying conditions over and over again in each query.
Paginate should automatically pick up these associations when you Paginate your main model. Here's an example of what I mean here: http://cakephp.1045679.n5.nabble.com/Paginate-with-Containable-td1300971.html#a1300971
These should make your task easier.
After a lot of searching the net and reading CakePHP's documentation, here is the solution I came up with:
function unpaidClass($id) {
$this->FeePayment->unbindModel(array(
'belongsTo' => array('Student')
), $reset = 0);
$this->FeePayment->bindModel(array(
'belongsTo' => array(
'Student' => array(
'foreignKey' => false,
'conditions' => array('Student.id = FeePayment.student_id')
),
'YearGroup' => array(
'foreignKey' => false,
'conditions' => array('YearGroup.id = Student.year_group_id')
)
)
), $reset = 0);
$this->paginate = array(
'contain' => array('Student','YearGroup'),
'conditions' => array('YearGroup.school_year' => $id,
'FeePayment.status' => 'Unpaid',
'FeePayment.due_date <= ' => date("Y-m-d")));
$this->set('feePayments', $this->paginate());
}

Categories