The problem You created a new CakePHP application and you try to visit its homepage. You get a database error. Missing Database Connection Cake\Database\Exception\MissingConnectionException Connection to Sqlite could not be established: SQLSTATE[HY000] [14] unable to open database file The (potential) reason Check your composer.json and see if your application uses cakephp/debug_kit: “require-dev”: { … “cakephp/debug_kit”:… Continue reading Connection to Sqlite not established, unable to open database file
Category: Quick Bites
Quick searches for everyday tasks while working with CakePHP
“Authentication is required to continue” when loading Authentication component
If you use cakedc/users or cakephp/authentication and want to programmatically log in a newly created user, you will have to use setIdentity() on Authentication component, and for that you will have to load it into your controller. But if you do it wrong, you will get this error: Authentication is required to continue Authentication\Authenticator\UnauthenticatedException To… Continue reading “Authentication is required to continue” when loading Authentication component
Optional parameter in named routes
Let’s say you want to make a named CakePHP route which may or may not have an argument. For example, you want to return WHOIS information for an IP address: /whois/1.1.1.1 returns WHOIS information for 1.1.1.1 which is specifically passed in the URL; /whois will return WHOIS information for the visitor’s IP address. When doing… Continue reading Optional parameter in named routes
Changing request IP address for debugging in CakePHP
If you’re working on a feature that acts differently based on the request IP address, you would be wanting to test it continuously. For that, you may have to have your application think you’re accessing it from a specific/different IP address. The easiest way would be to just store the IP in a variable and… Continue reading Changing request IP address for debugging in CakePHP
CRUD View: _getIndexTitleField() must be of type string, array returned
Setup You’re using CakePHP with CRUD plugin installed. You baked some models from the database. Problem You visit the index page for a given model and you get the following error: CrudView\Listener\ViewListener::_getIndexTitleField(): Return value must be of type string, array returned Solution Make sure the model has a display field. You can always try using… Continue reading CRUD View: _getIndexTitleField() must be of type string, array returned
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
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