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

CakePHP switch database (using same datasource) on the fly?

Made it work using this (create a new connection on the fly) : $newDbConfig = $this->dbConnect($serverConfig); $this->Model->useDbConfig = $newDbConfig[‘name’]; $this->Model->cacheQueries = false; With : /** * Connects to specified database * * @param array $config Server config to use {datasource:?, database:?} * @return array db->config on success, false on failure * @access public */ function … 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

UNION syntax in Cakephp

Too many coders try to limit themselves to the functionality of a framework. DON’T. Use what the framework provides. If it does not have the functionality you seek, then either: Code the functionality you need into a class extension or Custom spin the code within the framework to suit your needs. Often, developers try to … Read more

CakePHP: best way to call an action of another controller with array as parameter?

I would not advice to use the method requestAction but rather import, and instantiate the needed controller. CakePHP doc says about requestAction that: “It is rarely appropriate to use in a controller or model” http://book.cakephp.org/view/434/requestAction Once you imported and loaded the controller you can call any method of this controller with its parameters. <?php //Import … Read more

Best way to document Array options in PHPDoc?

This is how I do it instead: /** * Class constructor. * * @param array $params Array containing the necessary params. * $params = [ * ‘hostname’ => (string) DB hostname. Required. * ‘databaseName’ => (string) DB name. Required. * ‘username’ => (string) DB username. Required. * ‘password’ => (string) DB password. Required. * ‘port’ … Read more