cakephp paginate post data - php

i need to create search method with pos data :
search method :
public function find_estate () {
if(isset($this->request->data['type'])){
$type =$this->request->data['type'] ;
}else{$type="";}
if($type!=""){
if ($type != "all"){
$where[] = "Estate.melk_type LIKE '$type'";
}
}
}
if(isset($where)){
$this->paginate =
array(
'Estate'=>
array(
'limit' => 4,
'order' => array(
'Estate.eid' => 'desc'
),
'fields' => array('eid','melk_type','status ','image'),
'conditions' => implode(" AND ",$where),
));
$this->set('estate',$this->paginate());
}
}
but not working in other page of search result
How do I paging with post data ?

You need to send $type param when you're switching pages, also I would use get method if it's a simple search with one field, from what I see you don't test for the method so you could easily add search param to the URLs in pagination
here's another question about this, you can also try official documentation

Related

Two paginations not working independently in one page - CakePHP

I have one web page and in this page I need total 2 paginations for different different 2 tables.
I have tried with different pagination but it's dependent with each other.
For ex., If I select second page of first table then second table automatically changed with 2nd page.
Here is my controller code code :
$page = $this->pageForPagination('User');
$this->paginate = array(
'User' => array(
'fields' => array(
'User.*',
),
'conditions' => $userConditions,
'page' => $page,
),
);
$this->set('users', $this->paginate('User'));
$page1 = $this->pageForPagination('Game');
$this->paginate = array(
'Game' => array(
'fields' => array(
'Game.*',
),
'conditions' => $conditions,
'page' => $page1,
),
);
$this->set('games', $this->paginate('Game'));
Here is the function that is used in above code :
function pageForPagination($model) {
$page = 1;
$sameModel = isset($this->params['named']['model']) && $this->params['named']['model'] == $model;
$pageInUrl = isset($this->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
Here is my view code :
echo $this->element('paging', array('model' => 'User'));
echo $this->element('paging', array('model' => 'Game'));
I have referred above code from this url : http://debuggable.com/posts/how-to-have-multiple-paginated-widgets-on-the-same-page-with-cakephp:48ad241e-b018-4532-a748-0ec74834cda3
Can any one help me because I didn't get any solution still?
The solution in the link you mentioned is to override the default pagination urls and doesn't display both models at the same time. You haven't implemented the solution completely.
You have to extract the page number manually for each model like this:
function pageForPagination($model) {
if (isset($this->params['named']['pagefor'.$model])
return $this->params['named']['pagefor'.$model];
else
return 1;
}
The code below:
echo $this->element('paging', array('model' => 'User'));
echo $this->element('paging', array('model' => 'Game'));
causes the cakephp to use the default pagination view and the urls in the default pagination are the same for both models. You have to use custom pagination view and specify the model for the page link.
For the edited function above you can use a url pattern like this:
/Controller/Action/pageforUser:215/pageforGame:35
for each page link in your view.
I think best practice to use this url pattern is to override the pagination helper so that the links that are created form the following pattern:
'/Controller/Action/pagefor[Model]:215'.[other parameters]

CakePHP - Pagination Query append default condition "TableName.deleted" != 1"

I'm using Cakephp V2.0 and having huge application working on it. Below is the issue I'm facing.
Select Query is what automatically set condition like "TableName.deleted" != 1
Which does the creating issue actually, I want to add my custom condition in this query to get all soft-deleted records i.e.: "TableName.deleted" == 1.
But when we are using $this->paginate() function, it will append default condition at the end of MySQL query condition and then MySQL query will look like this:
("TableName.deleted" == 1) AND "TableName.deleted" != 1 Order By xyz
So it's retrieving the record set only by taking last condition and return only records which are not deleted.
How do I remove this default CakePHP condition ("TableName.deleted" != 1)?
Edited (added code):
if (isset($this->passedArgs['showdeleted']) && $this->passedArgs['showdeleted'] == 1) {
$displayConditions['AND']['tableName.deleted'] = "1";
} else {
$displayConditions['AND']['tableName.deleted'] = "0";
}
$this->paginate = array(
'conditions' => $displayConditions,
'fields' => $this->displayFields,
'limit' => $show_page,
'group' => 'tableName.id',
'contain' => array(
'tbl1',
'tbl2',
'tbl3',
'tbl4',
'tbl5',
),
);
$returnRecords = $this->paginate();
What should I do to resolve this?
Thanks for your suggesting and comments.
Actually i got the answer for the same. we have created behavior for the same
SoftDeletableBehavior.php
In which we have defined related methods which was appending the condition "TableName.deleted" != 1. and it was also mentioned in Model as well like below:
var $actsAs = array(
'SoftDeletable' => array(
'field' => 'deleted',
'find' => true
),
);
so i just made one very minor change and it's working.
var $actsAs = array(
'SoftDeletable' => array(
'field' => 'deleted',
'find' => false
),
);

How to use findAllBy() with pagination?

I'm working on a project where I want to search for people based on skills they say they have on their profile. I want to use the findAllBy(); function but I'm not sure how this works with the Pagination component packaged with CakePHP.
Just use conditions in Paginator, like :
public function skills($skill = null){
$this->Paginator->settings = array(
'conditions' => array(
'skill LIKE' => '%'.$skill.'%'
)
);
$people = $this->Paginator->paginate('People');
$this->set('people', $people);
}

CakePHP Pagination Routing Issues

Within CakePHP 2 I am using pagination which works great until I see the URL which is page:2, how can I make this ?page=2 ?
The next question is that I use this code for my controller which powers /domain.com/offers/top, /domain.com/offers/newest, /domain.com/offers/popular and then the categories like /domain.com/offers/tv-and-video. The thing is when it is paginated for /domain.com/offers/top instead of being /offers/top/page:2 it goes to /offers/bycategory/top/page:2.
public function bycategory($slug = null)
{
$userId = $this->Session->read("UserAuth.User.id");
if ($slug == 'top') {
//Get the top rated offers
$this->paginate = array(
'limit' => 15,
'order' => array(
'Offer.vote' => 'desc'
)
);
} elseif ($slug == 'newest') {
//Get the latest offers
$this->paginate = array(
'limit' => 15,
'order' => array(
'Offer.created' => 'desc'
)
);
} elseif ($slug == 'popular') {
//Get the most talked about offers
} else {
//This is the categories, so just get the category slug.
$this->paginate = array(
'conditions' => array('Category.slug =' => $slug),
'limit' => 15,
'order' => array(
'Offer.created' => 'desc'
)
);
}
$offers = $this->paginate('Offer');
// pass the value to our view.ctp
$this->set('offers', $offers);
$this->set('userId', $userId);
$this->render('/Offers/index');
}
This is my custom route:
Router::connect(
'/offers/:catslug',
array('controller' => 'offers', 'action' => 'bycategory'),
array(
'pass' => array('catslug')
));
how can I make this ?page=2 ?
By setting the paramType option in paginator component options as mentioned in manual.
You second issue looks like reverse routing issue. Have you setup any custom routes?

Cakephp Pagination on containable relationship

I have a model association that works as followed:
Users hasAndBelongsToMany Tags
I want to be able to paginate a group of users based on their tags. Users can have many tags and filter requested can be complex i.e. a query can ask for all the users with tags A and B but not C. Its way more manageable to handle this by using pagination on tags (so that the condiditions array can apply to the Tag model) and getting back the users that are within that tags subset.
The issue then comes when trying to paginate, because I set up the pagination as followed:
$this->paginate = array(
'conditions' => $conditions,
'contain' => array('User'),
'limit' => 5,
);
$this->paginate('Tag')
So then pagination does exactly what is expected, it returns the array for pagination based on tags, but what I want is to paginate by the users. Switching the pagination to hinge on the users creates its own set of problems ('that is what I was using before'). Just pagination exist for model relates like this? I feel like this is the type of thing cakephp might be able to do. Thank you to anyone who helps.
Place this into the model.
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$parameters = compact('conditions');
$this->recursive = $recursive;
$count = $this->find('count', array_merge($parameters, $extra));
if (isset($extra['group'])) {
$count = $this->getAffectedRows();
}
return $count;
}
You should use custom finder: cakephp documentation link. In you example in Users Model create protected function:
protected function _findUsersByTag($state, $query, $results = array()) {
if($state == 'before') {
$query['joins'] = array(
'Tag' => array('type' => 'inner', 'alias' => 'Tag', 'table' => 'tags', 'conditions' => array('Users.tag_id = Tag.id')
);
return $query;
}
return $results;
}
Second add in Users Model:
public $findMethods = array('usersByTag' => true);
And in the controller you definie you pagination to use custom finder using 0 index of array or in 2.3 and higther cake version property called findType:
public $pagination = array('User' => array(
'usersByTag',
'limit' => 5,
'conditions' => $conditions
));

Categories