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 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

Cakephp-3.x: How to change the data type of a selected alias?

As of CakePHP 3.2 you can use Query::selectTypeMap() to add further types, which are only going to be used for casting the selected fields when data is being retrieved. $query = $table ->find() ->select([‘alias’ => ‘actual_field’, /* … */]); $query ->selectTypeMap() ->addDefaults([ ‘alias’ => ‘integer’ ]); You can use any of the built-in data types, … Read more

SQLSTATE[HY000] [1045] Access denied for user ‘username’@’localhost’ using CakePHP

That error message usually means that either the password we are using doesn’t match what MySQL thinks the password should be for the user we’re connecting as, or a matching MySQL user doesn’t exist (hasn’t been created). In MySQL, a user is identified by both a username (“test2”) and a host (“localhost”). The error message … Read more

Why are date/time values interpreted incorrectly when patching/saving?

Date/time values are being casted/parsed in a locale aware fashion Update: this is the default behavior with the CakePHP application template versions prior to 3.2.5. As of 3.2.5 locale parsing is not enabled by default anymore, which will make the date/time marshalling logic expect a default format of Y-m-d H:i:s instead. In the marshalling process, … Read more

CakePHP 3.0 -> Between find condition

Expressions Between expression are supported out of the box, however they only support the first case without additional fiddling: $Query = $Table ->find() ->where(function($exp) { return $exp->between(‘start_date’, ‘2014-01-01’, ‘2014-12-32’, ‘date’); }); If you’d wanted to handle the second case via the between method, then you’d have to pass all values as expressions, which can easily … Read more

How to limit contained associations per record/group?

What you are looking for, is a solution to the greatest-n-per-group problem. You didn’t mention any specific RDBMS, but nonetheless see also http://dev.mysql.com/doc/refman/5.6/en/example-maximum-column-group-row.html A library solution For those who are a little bit adventurous, I’ve developed some custom associations that transparently integrate into the ORM layer, and allow for basic limit per group for hasMany … Read more