(Below has been tested on CakePHP 5, but should work on 3+.) Say you’re working on a query that may have to do some currency conversion on the go if user’s currency is different from the currency in your database. To make it worse, your site may use a third currency as a base currency… Continue reading Complex CASE-THEN-WHEN expressions as columns in select queries
Tag: cakephp-orm
Containing threaded association
CakePHP has find('threaded') which lets you fetch records recursively. A good example is threaded comments. Say, you have Articles hasMany Comments, and each comment can have a parent, linked via parent_comment_id. You define your association as usual. Then in the Comments table add the following: public function beforeFind($event, $query, $options, $primary) { $query->find('threaded', [ 'parentField'… Continue reading Containing threaded association
Containing associated tables automatically
Say you have OrdersTable associated to CustomersTable, and you want every order you select to automatically contain the corresponding customer information. (So that you don’t have to do ->contain('Customers') on each find() call.) There are two ways to accomplish this. In your OrdersTable: public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) { $query->contain(‘Customers’); return… Continue reading Containing associated tables automatically