Custom Query Pagination Cakephp - mysql

I have a custom query in my controller and I would like to implement the custom query pagination I found on cakephp.org but their example is not similar to mine. Can someone please help me paginate this result in my view:
$cars = $this->Car->query(" select Car.id, Car.make, Car.model, Car.year, Car.description, CarImage.thumbnail
from cars Car
inner join car_images CarImage on Car.default_image_id = CarImage.id
where Car.make like '" . $category . "'
order by Car.created DESC
limit 10");
$this->set('cars', $cars);

Implement paginate and paginateCount in your model:
function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra)
{
return $this->query('SELECT ...');
}
function paginateCount($conditions, $recursive, $extra)
{
return $this->query('SELECT COUNT(.....');
}
Also check out the paginate function in: cake/libs/controller/controller.php

This looks like it should be able to be done without resorting to using custom pagination.
You should set in the following relationships in your models:
Car hasMany (or hasOne) CarImage CarImage belongsTo Car
Then you should be able to get this exact data using the following:
<?php
class CarsController extends AppController {
var $paginate = array('limit'=>'10',
'order'=>'Car.created',
'fields'=>array('Car.model','Car.year','Car.description',
'CarImage.thumbnail'));
function test() {
// not sure where you are getting the where clause, if its from some form
// you'll want to check look in $this->data
$category = "somevalue";
// set the LIKE condition
$conditions = array('Car.make LIKE' => '%'.$category.'%');
// apply the custom conditions to the pagination
$this->set('cars', $this->paginate($conditions);
}
}
?>
For more information on complex find conditions (which can be applied to paginate by passing in an array like I did in the above example): http://book.cakephp.org/view/74/Complex-Find-Conditions

In Cake 3 you can use de the deafult paginator with function find() .
Follow the example:
$query = $this->Car->find()
->select(['Car.id', 'Car.make','Car.model','Car.year','Car.description','CarImage.thumbnail'])
->join([
'table' => 'CarImage',
'alias' => 'CarImage',
'type' => 'INNER',
'conditions' => 'CarImage.id = Car.default_image_id,
])
->where(['Car.make LIKE' => '%$category%'])
->order(['Car.created' => 'DESC'])
->limit(50)
$cars = $this->paginate($query);
$this->set(compact('cars'));
$this->set('_serialize', ['cars']);

mysql limit clause support volume and offset, so 10 results per page....
select * from table limit 0, 10
page 1
select * from table limit 10, 10
page 2
select * from table limit 20, 10
page 3
select * from table limit ($page-1)*$perPage, $perPage
Generic

mysql limit clause support volume and offset, so 10 results per page....

Related

cakephp3 select count(*) from other models

I have 3 models Employees Customers and Partners
I want to display the count of all the tables employees customers and partners in dashboard view of Employees model.
EmployeesController.php
<?php
namespace App\Controller;
class EmployeesController extends AppController
{
public function dashboard()
{
}
}
?>
I'm new to cakephp and I have no idea how to query other model tables at all.
Please point me to good tutorial or documentation for this. It will be helpful
I fixed it using the following codes.
// Controller
$this->loadModel("Customer");
$query = $this->Customers->find('all', [
'conditions' => ['Customers.area_cd =' => $this->Auth->user('area_cd')]
]);
$entity_count[0] = $query->count();
$this->loadModel("Employee");
$query = $this->Employees->find('all', [
'conditions' => ['Employees.area_cd =' => $this->Auth->user('area_cd')]
]);
$entity_count[1] = $query->count();
$this->loadModel("Partner");
$query = $this->Partners->find('all', [
'conditions' => ['Partners.area_cd =' => $this->Auth->user('area_cd')]
]);
$entity_count[2] = $query->count();
$this->set(compact('entity_count'));
// View
<?= $entity_count[0] ?>
The query to use would be a nested query, assigned to an array. Then you can access the values from there.
In particular, you would want to write something like:
$result = List();
$result->query('SELECT COUNT(SELECT * FROM usertable), COUNT(SELECT * FROM partnertable), COUNT(SELECT * FROM customertable)');
To then access the values, you would refere to $result based on the position in the array, ie. $result[0] would be the first value, $result[1] the second and so on.
If you need to specify the items for the counts (such as active users, active customers etc) add where clauses into the select statements within the count expression
Eg. SELECT * FROM usertable WHERE active="yes"
Once you have the data you need, you can then edit the source file where you require them to be displayed and decide where to place them using a php echo to the html stream

how to use relation table when using sqldataprovider

please help I dont know how to get relation table when using sqldataprovider. Anyone understand how to use relation model?
$model = new Finalresult();
$searchModel = new FinalresultSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider = new SqlDataProvider([
'sql' => 'SELECT finalresult.bib,
finalresult.series_id,
finalresult.category_id,
GROUP_CONCAT(DISTINCT finalresult.point ORDER BY series.serie_seri_no DESC) AS seriPoint
FROM finalresult, series GROUP BY finalresult.bib',
'key' => 'bib',
]);
I'm trying to get relation table:
'attribute'=>'category_id',
'width'=>'300px',
'value'=>function ($model, $key, $index, $widget) {
return $model->category->category_name;
},
then getting trying to non-object
You can't use relations with SqlDataProvider, because each single result will be presented as array, for example:
[
'id' => 1,
'name' => 'Some name',
'category_id' => 1,
],
For example, you can access category_id as `$model['category_id'].
SqlDataProvider is for very very complex queries, your query can easily be written as ActiveQuery and you can use ActiveDataProvider and get all advantages of that (relations, etc.).
You can find category by id, but it will be lazily loaded that means amount of queries is multiplied by number of rows.
With ActiveDataProvider and relations you can use eager loading and reduce amount of queries. Read more in official docs.
Grid Columns example in documentation
try to change "value" to
'value'=> function($data) {
return $data['category']['category_name'];
}

CakePHP Model Query - Complex - Multi Table - Sum - Count - Group By

Hello and happy holidays everyone.
Recently I have been tasked with transforming a beta application from pure PHP/jQuery to CakePHP/ExtJS (Which I am new to).
My issue is with the most complex query that populates the main grid.
To keep things simple there are 3 tables with correct baked relationships and models: Projects, ToDo, ExtraToDo
Projects hasMany ToDo and ExtraToDo.
ToDo and ExtraToDo have columns Title and Complete.
My goal is to get a completion percent for each project based on these three tables.
The way I have gone about this is the SUM of the Complete column divided by the COUNT of the Complete column. I am trying in a CakePHP way for readability/performance/otherstuffIdontknowyet.
Originally, in raw SQL I had done it like this:
SELECT
`idProject`,
(SELECT
ROUND((SUM(`Complete`) / COUNT(`Complete`)) * 100),
FROM
(SELECT `Complete`, `ProjectID` FROM `ToDo`
UNION ALL
SELECT `Complete`, `ProjectID` FROM `ExtraToDo`) orders
WHERE
`ProjectID` = `idProject`
) AS 'Completion'
FROM
`Projects`
I also got this to work in the Kohana PHP MVC framework fairly easily which I tried before deciding on CakePHP. I LOOOVED how their queries were created...:
private function get_completion() {
$Query = DB::select('ProjectID', array(DB::expr('ROUND((SUM(`Complete`) / COUNT(`Complete`)) * 100)'), 'Completion'))
->from(array('ToDo', 'ExtraToDo'))
->group_by('ProjectID');
return $Query;
}
public function get_all() {
$Query = DB::select()
->from('Projects')
->join(array(self::get_completion(), 'Completion'))
->on('projects.id', '=', 'Completion.ProjectID')
->execute()
->as_array();
return $Query;
}
Unfortunately I have completely struggled to get this working in CakePHP while doing it the CakePHP way.
I'm pretty sure virtualFields are the key to my answer but after reading the documents and trying x, y, AND z. I have been unable to comprehend them and how they relate.
Thank you in advance
-T6
That is a lot of nested selects. IMO you would be better off building a better query.
This should get you going.
class Project extends AppModel {
public $findMethods = array(
'completion' => true
);
// other code
protected function _findCompletion($state, $query, $results = array()) {
if ($state == 'before') {
$this->virtualFields['total'] = 'ROUND((SUM(Todo.Complete + TodoExtra.Complete) / (COUNT(Todo.Complete) + COUNT(TodoExtra.Complete))) * 100)';
$query['fields'] = array(
$this->alias . '.' . $this->primaryKey,
'total'
);
$query['joins'] = array(
array(
'table' => 'todos',
'alias' => 'Todo',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array('Todo.project_id = ' , $this->alias . '.' . $this->primaryKey)
),
array(
'table' => 'todo_extras',
'alias' => 'TodoExtra',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array('TodoExtra.project_id = ' . $this->alias . '.' . $this->primaryKey)
),
);
$query['group'] = array(
$this->alias . '.' . $this->primaryKey
);
return $query;
}
return $results;
}
// other code
}
Now you have a custom find method that can be used like find('first') or find('all').
From the controller:
$this->Project->find('completion');
Or in the Project model
$this->find('completion');
It should return something like this:
$results = array(
0 => array(
'Project' => array(
'id' => 1,
'total' => 50
)
),
1 => array(
'Project' => array(
'id' => 2,
'total' => 75
)
)
);
I would suggest either creating an afterFind() function to the Project model class, or simply just adding a function that you would call when you need to perform this calculation.
The function to perform the calculation would look like:
getPercentageComplete($project){
{
$total_todos = count($project['ToDo']);
$completed_todos = 0;
foreach($project['ToDo'] as $todo){
if($todo['Complete']) //assuming this is a boolean field
$completed_todos++;
}
return $completed_todos / $total_todos;
}
Then, your afterFind would look something like this:
function afterFind(&$results)
{
foreach ($results as &$project)
{
$project['Project']['percentageComplete'] = $this->Project->getPercentageComplete($project);
}
return $results;
}
You can see more about afterFind() at the CakePHP Bakery - > Callback Methods

CakePHP: How can I use a "HAVING" operation when building queries with find method?

I'm trying to use the "HAVING" clause in a SQL query using the CakePHP paginate() method.
After some searching around it looks like this can't be achieved through Cake's paginate()/find() methods.
The code I have looks something like this:
$this->paginate = array(
'fields' => $fields,
'conditions' => $conditions,
'recursive' => 1,
'limit' => 10,
'order' => $order,
'group' => 'Venue.id');
One of the $fields is an alias "distance". I want to add a query for when distance < 25 (e.g. HAVING distance < 25).
I have seen two workarounds so far, unfortunately neither suit my needs. The two I've seen are:
1) Adding the HAVING clause in the "group" option. e.g. 'group' => 'Venue.id HAVING distance < 25'. This doesn't seem to work when used in conjunction with pagination as it messes up the initial count query that is performed. (ie tries to SELECT distinct(Venue.id HAVING distance < 25) which is obviously invalid syntax.
2) Adding the HAVING clause after the WHERE condition (e.g. WHERE 1 = 1 HAVING field > 25) This doesn't work as it seems the HAVING clause must come after the group statement which Cake is placing after the WHERE condition in the query it generates.
Does anyone know of a way to do this with CakePHP's find() method? I don't want to use query() as that would involve a lot of rework and also mean I'd need to implement my own pagination logic!
Thanks in advance
You have to put it with the group conditions. like this
$this->find('all', array(
'conditions' => array(
'Post.length >=' => 100
),
'fields' => array(
'Author.id', 'COUNT(*) as Total'
),
'group' => array(
'Total HAVING Total > 10'
)
));
Hope it helps you
I used the following trick to add my own HAVING clause at the end of my WHERE clause. The "dbo->expression()" method is mentioned in the cake sub-query documentation.
function addHaving(array $existingConditions, $havingClause) {
$model = 'User';
$db = $this->$model->getDataSource();
// Two fun things at play here,
// 1 - mysql doesn't allow you to use aliases in WHERE clause
// 2 - Cake doesn't allow a HAVING clause separate from a GROUP BY
// This expression should go last in the WHERE clause (following the last AND)
$taut = count($existingConditions) > 0 ? '1 = 1' : '';
$having = $db->expression("$taut HAVING $havingClause");
$existingConditions[] = $having;
return $existingConditions;
}
As per the manual, CakePHP/2 supports having at last. It was added as find array parameter on version 2.10.0, released on 22nd July 2017.
From the 2.10 Migration Guide:
Model::find() now supports having and lock options that enable you to
add HAVING and FOR UPDATE locking clauses to your find operations.
Just had the same problem. I know, one is not supposed to modify the internal code but if you open the PaginatorComponent and you modify line 188:
$count = $object->find('count', array_merge($parameters, $extra));
to this:
$count = $object->find(
'count',
array_merge(array("fields" => $fields),$parameters, $extra)
);
Everything will be fixed. You will be able to add your HAVING clause to the 'group' and the COUNT(*) won't be a problem.
Or, make line:
$count = $object->paginateCount($conditions, $recursive, $extra);
to include the $fields:
$count = $object->paginateCount($fields,$conditions, $recursive, $extra);
After that, you can "override" the method on the Model and make sure to include the $fields in the find() and that's it!, =P
Here is another idea that doesn't solve the pagination issue, but it is clean since it just overrides the find command in AppModel. Just add a group and having element to your query and this will convert to a HAVING clause.
public function find($type = 'first', $query = array()) {
if (!empty($query['having']) && is_array($query['having']) && !empty($query['group'])) {
if ($type == 'all') {
if (!is_array($query['group'])) {
$query['group'] = array($query['group']);
}
$ds = $this->getDataSource();
$having = $ds->conditions($query['having'], true, false);
$query['group'][count($query['group']) - 1] .= " HAVING $having";
CakeLog::write('debug', 'Model->find: out query=' . print_r($query, true));
} else {
unset($query['having']);
}
}
return parent::find($type, $query);
}
Found it here
https://groups.google.com/forum/?fromgroups=#!topic/tickets-cakephp/EYFxihwb55I
Using 'having' in find did not work for me. Instead I put into one string with the group
" group => product_id, color_id having sum(quantity) > 2000 " and works like a charm.
Using CakePHP 2.9

Filtering theme_table in Drupal

I just created a data table based on a query and displayed it successfully using theme_table().
Now, I'd like to add some filters to the table but have no idea how to proceed.
Is there a built-in feature that allow me to do this easily, or should I manually add a form and update the query/redisplay the results each time the user selects something?
Thanks for your help!
I think you want to use pager_query and tablesort_sql: it's especially made for creating tables of data with pagination and sorting capabilities (and themes usually theme such tables nicely out of the box).
Example:
<?php
// The regular query without sorting or pagination parameters
$sql = 'SELECT cid, first_name, last_name, company, city FROM {clients}';
// Number of rows per page
$limit = 20;
// List of table columns ("field" is the matching database column from the sql query)
$header = array(
array('data' => t('Name'), 'field' => 'last_name', 'sort' => 'asc'),
array('data' => t('Company'), 'field' => 'company'),
array('data' => t('City'), 'field' => 'city')
);
// Calculates how to modify the SQL query according to the current pagination and sorting settings
// Then performs the database query
$tablesort = tablesort_sql($header);
$result = pager_query($sql . $tablesort, $limit);
$rows = array();
while ($client = db_fetch_object($result)) {
$rows[] = array(l($client->last_name.', '.$client->first_name, 'client/'.$client->cid), $client->company, $client->city);
}
// A message in case no results were found
if (!$rows) {
$rows[] = array(array('data' => t('No client accounts created yet.'), 'colspan' => 3));
}
// Then you can pass the data to the theme functions
$output .= theme('table', $header, $rows);
$output .= theme('pager', NULL, $limit, 0);
// And return the HTML output
print $output;
?>
(I added comments, but the original version of the example comes from this page)
Alternatively, maybe you don't need to make a module at all if you're just trying to make a page that displays a list of data, you may prefer using the Views module.