Using the paginator directly is not recognising the configuration - cakephp

CakePHP Version: 4.0.1
Introduction
I have 2 methods that both use the index view, index and search. On index the column can be selected from a select list and a value can be inputted via an input form control enabling a search by column and value. This data is sent via GET to the search method where empty values are checked and the query is executed and the index view is rendered.
In the later 3x versions with the below configuration the index view had the sort on the selected column which is what it is meant to do.
IE: Index view has due_date sorted on the initial load and I select task_name then submit the form to the search method. The task_name has the sort when the view is rendered.
TASKS CONTROLLER
Public pagination property:
public $paginate = [
'sortWhitelist' => [
'Tasks.due_date',
'Tasks.task_name',
'Tasks.type',
'Tasks.priority',
'Tasks.related_to_name',
'Contacts.first_name',
'Contacts.last_name',
'Accounts.account_name',
'Tasks.task_desc'
]
];
Search Method
I initialise the data received from the index method and apply the config to the pagination property and send the query object to the view.
$this->setPage('');
$this->setSort($this->request->getQuery('column'));
$this->setDirection('asc');
// Validation of the page, sort, direction and limit is done here.
// IE: The $this->getSort() must be a string and not be numeric and has a strlen check
// and the $this->getDirection() can only be a string with values 'asc' or 'desc' etc.
if (!empty($this->getPage())) {
$this->paginate['page'] = $this->getPage();
}
$this->paginate['sort'] = $this->getSort();
$this->paginate['direction'] = $this->getDirection();
$this->paginate['limit'] = $this->getLimit();
debug($this->paginate);
$tasks = $this->paginate($query);
$this->set(compact('tasks'));
The result of debug is:
[
'sortWhitelist' => [
(int) 0 => 'Tasks.due_date',
(int) 1 => 'Tasks.task_name',
(int) 2 => 'Tasks.type',
(int) 3 => 'Tasks.priority',
(int) 4 => 'Tasks.related_to_name',
(int) 5 => 'Contacts.first_name',
(int) 6 => 'Contacts.last_name',
(int) 7 => 'Accounts.account_name',
(int) 8 => 'Tasks.task_desc'
],
'sort' => 'Tasks.task_name',
'direction' => 'asc',
'limit' => (int) 25
]
Result
The sort is on the task_name.
A couple of months ago I upgraded to 4 and have just revisted this functionality to find the sort is on the column that was present on index and not the column that was selected. I tried the below to fix the problem:
I referenced this information in the cookbook. And this from SO.
$config = $this->paginate = [
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit()
];
debug($config);
$tasks = $this->Paginator->paginate($query, $config);
debug($this->Paginator);
$this->set(compact('tasks'));
The result of debug $config is:
[
'page' => '',
'sort' => 'Tasks.task_name',
'direction' => 'asc',
'limit' => (int) 25
]
The result of debug $this->Paginator is:
object(Cake\Controller\Component\PaginatorComponent) {
'components' => [],
'implementedEvents' => [],
'_config' => [
'page' => (int) 1,
'limit' => (int) 20,
'maxLimit' => (int) 100,
'whitelist' => [
(int) 0 => 'limit',
(int) 1 => 'sort',
(int) 2 => 'page',
(int) 3 => 'direction'
]
]
}
NOTE: The whitelist contains limit, sort, page and direction? And the limit is 20 and I don't even have a selection of 20?
Result
The sort is on the due_date and I need it on the task_name.
Extra Info
If I then click the sort on task_name the sort is on the task_name. All the sorts work just not on the initial load?
Question
How can I configure the pagination property so the sort is on the task_name from the initial load of the search method.
Thanks Z.

The fix is a bit costly and not ideal but it does work. I do a redirect on the initial load. Basically submit the form to search then redirect back to search. IE:
if ($this->request->getQuery('initial') === 'yes') {
$redirect = $this->request->getQuery('redirect', [
'action' => 'search',
'?' => [
'method' => 'search',
'column' => $this->getColumn(),
'input' => $this->getInput(),
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit(),
'filter' => $this->getFilter(),
]
]);
return $this->redirect($redirect);
exit(0);
}
$config = $this->paginate = [
'sortWhitelist' => [
'Tasks.due_date',
'Tasks.task_name',
'Tasks.type',
'Tasks.priority',
'Tasks.related_to_name',
'Contacts.first_name',
'Contacts.last_name',
'Accounts.account_name',
'Tasks.task_desc'
],
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit()
];
$tasks = $this->Paginator->paginate($query, $config);
$this->set(compact('tasks'));
The sort is now on the task_name.
This negates the initial load problem and simulates usage after the page initially loads where I know the sorts work.

Related

CakePHP How to Get Associated Data and COUNT/GROUP BY

I'm having some trouble with Cakephp Query builder and try to get results I'm looking for. I have three tables 'Users', 'Items', 'Owns', where Owns belongs to both 'Users' and 'Items'.... A user can own many items (and many of the same items).
So I'm wanting to return the "owned" items by a user along with the count... However whenever I add count into my query I lose the associated Items data.
example - this returns the user data along with associated owns data and associated items data as shown below the query
$owns = $this->Users->get($id, [
'contain' => [
'Owns' => function($q) { return $q->find('all')->group(['Owns.item_id']); },
'Owns.Items'
],
]);
SQL Generated from this is
SELECT
Owns.id AS Owns__id,
Owns.item_id AS Owns__item_id,
Owns.user_id AS Owns__user_id,
Items.id AS Items__id,
Items.name AS Items__name,
Items.description AS Items__description,
Items.created AS Items__created,
Items.modified AS Items__modified
FROM
owns Owns
INNER JOIN items Items ON Items.id = (Owns.item_id)
WHERE
Owns.user_id in (1)
GROUP BY
Owns.item_id
Data results look like this:
'owns' => [
'id' => (int) 1,
'username' => 'myusername',
'owns' => [
(int) 0 => object(App\Model\Entity\Own) id:0 {
'id' => (int) 2
'item_id' => (int) 1
'user_id' => (int) 1
'item' => object(App\Model\Entity\Item) id:1 {
'id' => (int) 1
'name' => 'Item Name'
'description' => 'Item Description'
However in the 'owns' query I want to add in the count (i.e. the number owned). I can get the count by using this query but then I lose the associated item object from my results. I've tried this many different ways but always seems that if I want to use SQL count I can't get the associated data.
$owns2 = $this->Users->get($id, [
'contain' => [
'Owns' => function($q) { return $q->select(['count' => $q->func()->count('Owns.id'),'Owns.id', 'Owns.user_id', 'Owns.item_id'])->group(['Owns.item_id']); },
'Owns.Items'
],
]);
SQL Generated from this is
SELECT
(COUNT(Owns.id)) AS count,
Owns.id AS Owns__id,
Owns.user_id AS Owns__user_id,
Owns.item_id AS Owns__item_id
FROM
owns Owns
INNER JOIN items Items ON Items.id = (Owns.item_id)
WHERE
Owns.user_id in (1)
GROUP BY
Owns.item_id
Data results look like this:
'owns2' => [
'id' => (int) 1,
'username' => 'myusername',
'owns' => [
(int) 0 => object(App\Model\Entity\Own) id:36 {
'count' => (int) 4
'id' => (int) 2
'user_id' => (int) 1
'item_id' => (int) 1
Any insight into how I can get "count" into the first $user query or the associated items into the $user2 would be appreciated.
To keep the association table's fields that are returned in the $owns = find('all') query along with the SQL count function I needed to add the associated fields into the select statement
$owns2 = $this->Users->get($id, [
'contain' => [
'Owns' => function($q) { return $q->select(['count' => $q->func()->count('Owns.id'),'Owns.id', 'Owns.item_id','Owns.user_id', 'Items.id', 'Items.collection_id','Items.name', 'Items.description'])->group(['Owns.item_id']); },
'Owns.Items'
],
]);

CakePHP 3.x OR In Find Condition With Multiple Same Fields

I would like to query the database for
Plates.page_number => 1 OR Plates.page_number => Cover
I am attempting to use the following code, but I am not getting the results I am looking for because of a duplicate array key, how can I search the same field for two different values?
$query = $this->Ledgers->find('all', array(
'contain' => array(
'Plates' => [
'conditions' => [
'OR' => [
'Plates.plate_title' => 'Front Cover',
'Plates.page_number' => '1',
'Plates.page_number' => 'Cover' // Duplicate Array Key
]
]
], 'Plates.PlateImages', 'Tribes'
),
'conditions' => array(
'Ledgers.disabled' => 'n', 'Ledgers.id IN' => $ledgerIds
)
))->orderAsc('ledger_title');
Please try wrapping your conditions in separate arrays, eg:
'OR' => [
['Plates.page_number' => '1'],
['Plates.page_number' => 'cover'],
...
]
More info can be found in CakePHP docs:
Query Builder -> Advanced Conditions

Filter data from 2 tables and get results from both tables

How can I filter data-set in 2 Tables where second table have more results 1:n.
In first table I can use orWhere and I am getting right data, but my another Table contain multiple results and if I use contain or matching I am getting only data from second Table.
So, I want to filter both tables and get matched data.
Here is my query:
First query to filter first table
$query
->where([
'OR' => [
'Orders.id' => $freeText,
'Orders.postal' => $freeText,
'Orders.address LIKE' => '%' . $freeText . '%',
'Orders.city LIKE' => '%' . $freeText . '%',
'Users.first_name' => $freeText,
'Users.last_name' => $freeText,
'ProjectAddresses.cost_centre' => $freeText,
'CONCAT(first_name, last_name) LIKE' => '%' . str_replace(' ', '', $freeText) . '%',
'CONCAT(first_name, last_name) LIKE' => '%' . $freeText . '%',
'Users.first_name IN ' => $splittedKeywords,
'Users.last_name IN ' => $splittedKeywords,
]
]);
Second query - try to filter data from second Table (but still need matched data from first table)
$query->contain('Items', function ($q) use ($freeText) {
return $q->where(['vessel_id' => $freeText]);
});
So problem is if I use second query he automatically take only data from second table and my goal is to get all filtered data (from first and second table).
I have 20+ data-sets like:
(int) 0 => [
'id' => (int) 1,
'uuid' => '5f34ecda-6bc6-46ed-b5cc-b2227029aed8',
'user_id' => (int) 319,
'status' => (int) 30,
'order_price' => (float) 341.04,
'address_id' => (int) 379,
'address' => 'XYZ',
'building_number' => '171',
'postal' => '111',
'city' => 'XYZ',
'country' => 'AT',
'project_address' => [
'id' => (int) 379,
'type' => 'project',
'group_id' => (int) 3,
'default' => false,
'corresponding_invoice_address' => null,
'short_name' => 'XYT',
'comment' => '',
],
'user' => [
'id' => (int) 319,
'uuid' => '675216eb-7110-44d2-82a7-f7f020e934a6',
'title' => 'Herr',
'first_name' => 'Test',
'last_name' => 'Test',
],
'item_groups' => [],
'items' => [
(int) 0 => [
'id' => (int) 26,
'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065',
'item_group_id' => null,
'type' => (int) 2,
'status' => (int) 30,
'vessel_id' => (int) 40001,
'features' => [],
],
(int) 1 => [
'id' => (int) 28,
'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065',
'item_group_id' => null,
'type' => (int) 2,
'status' => (int) 30,
'vessel_id' => (int) 40003,
'features' => [],
],
(int) 1 => [
'id' => (int) 29,
'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065',
'item_group_id' => null,
'type' => (int) 2,
'status' => (int) 30,
'vessel_id' => (int) 40003,
'features' => [],
],
]
],
SQL
SELECT *
FROM orders Orders
INNER JOIN users Users ON Users.id = (Orders.user_id)
LEFT JOIN addresses ProjectAddresses ON ProjectAddresses.id = (Orders.address_id)
WHERE (Orders.id = :c0
OR Orders.postal = :c1
OR Orders.address LIKE :c2
OR Orders.city LIKE :c3
OR Users.first_name = :c4
OR Users.last_name = :c5
OR ProjectAddresses.cost_centre = :c6
OR CONCAT(first_name, last_name) LIKE :c7
OR Users.first_name IN (:c8)
OR Users.last_name IN (:c9))
Parameter is c1 = 4001 || %40001% || %40001
#ndm The goal is If somebody send in $freeText == 40003 I have to get as result this object where vessel_id = 40003 and thats works, but if somebody send in $freeText == Test then I need again same result because first_name == Test 8see first query) and when in second wuery I am using matching/contain this results are removed because he only "fetch" rows that are matching/contain Items...
Basically I want to check 10+ columns with given $freeText variable and if it is matching I want that results in my data-set (from both tables)
If I understood you correctly, then you're looking for a left join filter, ie left join Items (additionally to containing it for retrieval), group by Orders primary key (to avoid duplicates), and then simply add the Items.vessel_id condition to the main queries WHERE clause in order to make it a OR condition too.
$query
->contain('Items')
->leftJoinWith('Items')
->where([
'OR' => [
'Orders.id' => $freeText,
// ...
'Items.vessel_id' => $freeText
]
])
->groupBy('Orders.id');
See also
Cookbook > Database Access & ORM > Query Builder > Filtering by Associated Data
Cookbook > Database Access & ORM > Query Builder > Using leftJoinWith

how to save belongstomany checkboxes in cakephp

I have a set of checkboxes with a belongs to many relationship. I couldnt use a multiple checkbox option for various reasons. The table Tutors has a belongsToMany relationship with subjects table. I get the correct checkboxes checked when it loads and when I update the checboxes I get the correct checkbox data returned but how do i save this? The docs say i need to put the id's in an array for the correct Subjects to be checked . I tried a few things but I couldnt save the data. I dont get an error, just not saving.
//view
foreach ($subjects as $key => $item) {
$sub=$item->name;
$val=0;
foreach ($tutor->subjects as $key2 => $item2) {
$sub2=$item2->name;
if ($sub==$sub2){
$val=1;
break;
}
}
echo $this->Form->hidden('subjects.'.$key.'.id', ['value'=>$item->id]);
echo $this->Form->input('subjects.'.$key.'.checkedvalue', ['label'=>$sub,
'checked'=> $val,'type'=>'checkbox']);
}
//controller
public function edittest5($id = null)
{
$tutor = $this->Tutors->get($id, [
'contain' => ['AvailabilityForTutors','Subjects']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$tutor = $this->Tutors->patchEntity($tutor, $this->request->data(),['associated' => ['AvailabilityForTutors','Subjects']] );
// $tutor->dirty('availability_for_tutors', true);
debug($tutor);
$this->Tutors->save($tutor,[
// 'atomic'=>false,
'validate' => false,
'associated' => ['Subjects']
]);
return $this->redirect(['action' => 'edittest5',$id]);
}
$subjects = $this->Tutors->Subjects->find('all', ['limit' => 200]);
returned data
'first_name' => 'drew',
'subjects' => [
(int) 0 => [
'id' => '1',
'checkedvalue' => '1'
],
(int) 1 => [
'checkedvalue' => '0'
],
(int) 2 => [
'checkedvalue' => '0'
],
(int) 3 => [
'checkedvalue' => '0'
],
(int) 4 => [
'id' => '5',
'checkedvalue' => '1'

CakePHP: how to get total number of records retrieved with pagination

When you retrieve records using $this->paginate('Modelname') with some page limit, how do you get the total number of records retrieved?
I'd like to display this total count on the view, but count($recordsRetrieved) returns the number displayed only on the current page. So, if total number of records retrieved is 99 and limit is set to 10, it returns 10, not the 99.
You can debug($this->Paginator->params());
This will give you
/*
Array
(
[page] => 2
[current] => 2
[count] => 43
[prevPage] => 1
[nextPage] => 3
[pageCount] => 3
[order] =>
[limit] => 20
[options] => Array
(
[page] => 2
[conditions] => Array
(
)
)
[paramType] => named
)
*/
The final code for PHP >=5.4:
$this->Paginator->params()['count'];
For PHP versions less than 5.4:
$paginatorInformation = $this->Paginator->params();
$totalPageCount = $paginatorInformation['count'];
To get your answer go to he following link
http://book.cakephp.org/2.0/en/controllers/request-response.html
use pr($this->request->params) you will find all the stuff of pagination
For cake 3.x
you can use method getPagingParams
example:
debug($this->Paginator->getPagingParams());
output:
[
'users' => [
'finder' => 'all',
'page' => (int) 1,
'current' => (int) 5,
'count' => (int) 5,
'perPage' => (int) 1,
'prevPage' => false,
'nextPage' => true,
'pageCount' => (int) 5,
'sort' => null,
'direction' => false,
'limit' => null,
'sortDefault' => false,
'directionDefault' => false,
'scope' => null
]
]
Here's how I got the count from my controller*
This is for CakePHP 2.3. It uses a view helper, which is typically a no-no in the controller as it violates MVC, but in this case I think makes sense to keep the code DRY.
// Top of file
App::uses('PaginatorHelper', 'View/Helper');
// Later in controller method
$paginatorHelper = new PaginatorHelper(new View(null));
$records = $this->paginate();
$count = $paginatorHelper->params()['count'];
* I know the OP asked about from the view, but I figure if Arzon Barua's answer is helping people (though I think it only tells you the requested count, not the actual count as the OP wants), then this might help too.
This way is getting a pagination information over controller.
class YourController extends Controller{
$helpers = array('Paginator');
public fn(){
$view = new View(null);
var_dump($view->paginator->params());
}
}

Resources