How to paginate associated records?

The paginator doesn’t support paginating associations, you’ll have to read the associated records manually in a separate query, and paginate that one, something along the lines of this: $product = $this->Products ->findBySlug($slug_prod) ->contain([‘Metas’, ‘Attachments’]) ->first(); $categoriesQuery = $this->Products->Categories ->find() ->innerJoinWith(‘Products’, function (\Cake\ORM\Query $query) use ($product) { return $query->where([ ‘Products.id’ => $product->id, ]); }) ->group(‘Categories.id’); $paginationOptions … Read more

sum() function in cakephp query

You should not be using PHP superglobals directly in CakePHP. You should instead use Model.field naming so that you do not get ambiguous field errors. Virtual fields is the way to go but that is not your problem, you need to read the book some more. $total = $this->RequestedItem->find(‘all’, array(array(‘fields’ => array(‘sum(Model.cost * Model.quantity) AS … Read more

How do I create a keyValue pair (display field) by combining/having two fields in CakePHP 3?

There are various ways to solve this, here’s three of them: Use a callback for the valueField Use a callback and stitch the value together by using values from the results. $query = $this->LocationsUser->Users ->find(‘list’, [ ‘valueField’ => function ($row) { return $row[‘first_name’] . ‘ ‘ . $row[‘last_name’]; } ]) ->where([‘id’ => $id]); See also … Read more

How to Install DebugKit on CakePHP

How to Install DebugKit for CakePHP (in just 4 easy steps!): STEP 1 (option A): The traditional / download method: Create a DebugKit folder within your app/Plugin directory, and put the contents of the download into it (not the top-level folder – the stuff within it). If you know how to clone from github, that … Read more

What is the equivalent to getLastInsertId() in Cakephp?

CakePHP has two methods for getting the last inserted id: Model::getLastInsertID() and Model::getInsertID(). Actually these methods are identical so it really doesn’t matter which method you use. echo $this->ModelName->getInsertID(); echo $this->ModelName->getLastInsertID(); This methods can be found in cake/libs/model/model.php on line 2768

How to use different datasources in a Query using cakephp3?

For now, CakePHP doesn’t take datasource configurations into account when creating joins, and I don’t think that this will be added in the near future, not least because cross database joins aren’t supported “out of the box” (as in, just prepend the database name and you’re set) in Postgres and SQLite. Assuming you are using … Read more

How to setup cronjobs in cake php?

Use a shell The ‘Cake Way’ of using a CakePHP application in cron jobs would be creating shell and then calling it as a cron job. i.e. Create a shell to do the task, and then add it to crontab (crontab -e on linux machine): 0 * * * * cd /path/to/app/ && Console/cake your_shell_name … Read more

How do I write a join query across multiple tables in CakePHP?

$markers = $this->Marker->find(‘all’, array(‘joins’ => array( array( ‘table’ => ‘markers_tags’, ‘alias’ => ‘MarkersTag’, ‘type’ => ‘inner’, ‘foreignKey’ => false, ‘conditions’=> array(‘MarkersTag.marker_id = Marker.id’) ), array( ‘table’ => ‘tags’, ‘alias’ => ‘Tag’, ‘type’ => ‘inner’, ‘foreignKey’ => false, ‘conditions’=> array( ‘Tag.id = MarkersTag.tag_id’, ‘Tag.tag’ => explode(‘ ‘, $this->params[‘url’][‘q’]) ) ) ))); as referred to in nate … Read more