Complex CASE-THEN-WHEN expressions as columns in select queries

(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

Getting started with Paginator: a minimal example

The built-in pagination changed in more recent CakePHP versions (4 and 5), it is now a view helper and no longer a controller component. MVP In your Controller where you want to use pagination, load the helper: public function initialize(): void { parent::initialize(); $this->viewBuilder()->addHelper(‘Paginator’); } Paginate your query results before passing them to the view… Continue reading Getting started with Paginator: a minimal example

Published
Categorised as CakePHP

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

Datasource class could not be found

Let’s say you defined a new connection example-connection in your config/app_local.php: ‘example-connection’ => [ ‘host’ => ‘localhost’, ‘username’ => ‘my_username’, ‘password’ => ‘my_password’, ‘database’ => ‘my_database_name’, ‘url’ => env(‘DATABASE_URL’, null), ], If you don’t specify className and driver, you may be getting a MissingDatasourceException error. [Cake\Datasource\Exception\MissingDatasourceException] Datasource class example-connection could not be found. To fix,… Continue reading Datasource class could not be found

Published
Categorised as CakePHP

Lycamobile DK as home internet / mobilt bredbånd

I got their All in One 119 plan with 200 GB. The connection is unstable. Disconnects often. Can’t maintain VPN for prolonged time. Using with a good expensive 4G/5G router in Copenhagen, so it’s on the carrier.

Connecting to Github from Debian Jessie in 2024

Upgrade everything that still upgrades. Add these to your /etc/apt/sources.list: deb [trusted=yes] http://archive.debian.org/debian jessie main deb [trusted=yes] http://archive.debian.org/debian-security jessie/updates main Optionally, you can comment out the old entries you may already have there. Run apt-get update && apt-get upgrade Chances are, if you try to connect with what you got, Github will complain: ERROR: You’re… Continue reading Connecting to Github from Debian Jessie in 2024

Android Studio Virtual Device: Installing and Gotchas, pre-Android 7

Android 7+ introduces certificate pinning that makes it more difficult to debug the requests the device makes. This covers the pre-certificate-pinning Android versions. Install Download and install. Here’s the archive with all available versions: https://developer.android.com/studio/archive. I will be installing 2023.2.1.25. First Launch; Choosing SDK If Android Studio crashes every time you try to open it,… Continue reading Android Studio Virtual Device: Installing and Gotchas, pre-Android 7

Passing a Qualys scan with Cloudflare free plan

Start by creating a Cloudflare account. Add your website. Use their nameservers on your domain. Wait until they validate your website. Below assumes you have a brand new account with all the default settings, i.e. you won’t have Bot Fight Mode turned on. On your account level (so not having entered a specific website), go… Continue reading Passing a Qualys scan with Cloudflare free plan

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