->fetch returns all fields as string - cakephp

Even in src/Database/StatementInterface.php the functions fetch and fetchAll have these comment:
$statement = $connection->prepare('SELECT id, title from articles');
$statement->execute();
print_r($statement->fetchAll('assoc')); // will show [0 => ['id' => 1, 'title' => 'a title']]
the functions return ALL fields as string, even the fields are defined as numeric:
array(4) {
["lat"]=>
string(11) "38.49580000"
["lng"]=>
string(11) "-6.95301000"
["taken_at"]=>
string(19) "2021-03-30 16:24:30"
["id"]=>
string(1) "1
The fields lat and lng are defined as DECIMAL(10,8)|(11,8) and id is INTEGER. CakePHP 4.3.1 [and also former versions] return the fields as string which breaks the code.
Am I doing something wrong or is this a bug?

You are using the lowest level of database operations that CakePHP provides, this is almost directly operating with PDO, no casting by CakePHP will apply here, and what exactly you'll receive will entirely depend on the DBMS and the PDO driver that you're using.
Most drivers will return everything as a string, others do cast some values, for example with Postgres you can get integers, while with MySQL you'll get everything as strings.

Related

CakePHP 3: order of translated data with missing translation strings

I use TranslationBehavior to handle translated data in my app. When retrieving data, CakePHP returns the translated string or if this string is not available, the value of the default locale. To request the data, the following query is used:
$items = $this->Model->find('all')
->all()
->extract('name');
To order the output, the query is:
$items = $this->Model->find('all')
->order([$this->Model->translationField('name') => 'ASC'])
->all()
->extract('name');
This works for all default locale items and all translated items. But when the translation for a record is missing, it will break the order. In this case the correct fallback value is returned, but the order is no longer correct. The output looks like this:
['A... (Translated)', 'B... (Translated)', 'A... (Default)', 'C... (Default)']
What I expect is the following order:
['A... (Default)', 'A... (Translated)', 'B... (Translated)', 'C... (Default)']
To achieve this, I changed the query to:
$items = $this->Model->find('all')
->order(['IF('.$this->Model->translationField('name').' != "", '.$this->Model->translationField('name').', Model.name)' => 'ASC'])
->all()
->extract('name');
Which gives the expected order:
['A... (Default)', 'A... (Translated)', 'B... (Translated)', 'C... (Default)']
The question is: is this the correct way to handle the order of mixed locales? Or did I miss something and CakePHP already provides a simpler solution?
You may ask, why should we mix the locales? In my case, it's not necessary to translate all the strings, because some of the items are identical in both languages.
Looking at how CakePHP queries the translated fields, and merges them later on at PHP level, this is the expected behavior, and you'd indeed have to use a conditional expression in the ORDER clause.
I'd suggest to use a CASE expression, as the IF() function is MySQL specific, something like:
$query = $this->Model->find();
$query
->orderAsc(
$query->newExpr()->addCase(
[
$query->newExpr()->isNotNull($this->Model->translationField('name'))
],
[
$query->identifier($this->Model->translationField('name')),
$query->identifier('Model.name')
]
)
)
// ...
Which would generate an expression similar to:
ORDER BY
CASE WHEN Model_name_translation.content IS NOT NULL
THEN Model_name_translation.content
ELSE Model.name END
ASC
And as mentioned in the comments, If you go the "do not translate identical strings" route, then you should avoid storing empty strings for such "missing" translations, instead do not store a record for them at all.
In order to avoid all this you could of course add translations for everything, even if they are equal in the different languages.

CakePHP 3 - access params of Query object

In CakePHP 3.x I can do this:
$Substances = TableRegistry::get('Substances');
$query = $Substances->find()->where($where_conditions)->select(['id']);
debug($query);
This will show me the Query object.
If I want to get the SQL string I can use debug($query->sql());. This will give me the SQL string with placeholders for any parameters, e.g.
SELECT ... WHERE ... in (:c0,:c1,:c2))
When using debug($query) I can see the values for :c0, :c1, etc:
'params' => [
':c0' => [
'value' => (int) 47,
'type' => 'smallinteger',
'placeholder' => 'c0'
],
':c1' => [
'value' => (int) 93,
'type' => 'smallinteger',
'placeholder' => 'c1'
],
':c2' => [
'value' => (int) 845,
'type' => 'smallinteger',
'placeholder' => 'c2'
],
':c3' => [
'value' => (int) 354,
'type' => 'smallinteger',
'placeholder' => 'c3'
]
]
However, I cannot access them outside the debug statement. For example $query->params() or $query['params'] doesn't give me the parameters - it will error. I want to be able to pass this array to a custom function, so how can I access it?
It's strange because I can use debug($query->sql()) to get the SQL string as above, and params is just another thing in that object, but doesn't seem to be accessible.
I've read How to get params from query object in CakePHP 3 but think that's a different question as it was to do with not seeing the values in the debug statement due to the default depth that debug would provide.
The reason I want to do this is because I want to be able to do a CREATE TABLE AS query that will write the values of the SELECT statement into a new table (Important: see this link for an example of how that works in vanilla MySQL). I can't figure out how to do that with the ORM in Cake, so was planning on writing a custom function. But I need to be able to access both the SQL as well as the parameters bound so that the query can be executed in my own function.
If you know of a solution where I can use the ORM to do the CREATE TABLE AS query, I'm still interested to know about this. However I would like to know if params are accessible outside debug() as well.
Premise: I did not actually understand why you need the params
anyway. The information you need is stored by the query ValueBinder object
so you could simply do
$params = $query->getValueBinder()->bindings();
debug($params);
but for some reason this will get you an empty array. My guess is that the query need some kind of initialization first.
in fact if you run
debug($query);
$params = $query->getValueBinder()->bindings();
debug($params);
you'll see your params. I think someone more expert than me will come and give a full explanation
edit: I noticed that debugging $query calls $query->sql() which in turns calls conection->compileQuery();
so you can do
$query->sql(); // you need this to compile the query
// and generate the bindings
$params = $query->getValueBinder()->bindings();
CakePHP does not provide specific methods for creating such CREATE TABLE AS statements, so you'll have to build that on your own.
Compiling a query as the one shown in your question is simple enough using the query objects sql() method, and as arilia already mentioned, you'll be able to access the parameters bound to that query after is was compiled.
Having the compiled SQL and the associated value binder, you can combine this with a custom raw query to build your CREATE TABLE AS statement. All you need to do is prepare a new statement with the compiled SQL, and attach the value binder via its own attachTo() method.
One thing you might also have to do, is to define custom aliases in your select(), as otherwise you'd end up with columns selected (and created) in the form of Substances_id.
$Substances = TableRegistry::get('Substances');
$selectQuery = $Substances
->find()
->where($where_conditions)
->select(['id' => 'id']); // < create aliases as required
// compile the ORM query, this will populate the value binder
$selectSql = $selectQuery->sql();
// combine table creation SQL and compiled ORM query in a single statement
$createStatement = $Substances
->getConnection()
->prepare('CREATE TABLE dynamic_table AS ' . $selectSql);
// attach the ORM querys value binder, binding all its values to the given statement
$selectQuery->getValueBinder()->attachTo($createStatement);
$success = $createStatement->execute();
This should create SQL similar to:
CREATE TABLE dynamic_table AS
SELECT
id AS id
FROM
substances Substances
WHERE
...
See also
Cookbook > Database Access & ORM > Database Basics > Interacting with Statements
API > \Cake\ORM\Association::attachTo()

In CakePHP 3.X using addCase how should I define a value as a fallback? [duplicate]

I'm trying to get a query working using a case statement, and can't figure out how to get the case to return a column value instead of a constant. I have the query working perfectly, except that the column names I'm providing for the results are being quoted or otherwise mishandled by Cake or maybe PDO somewhere down in a layer that I can't dig my way through. I got as far down as bindValue, but none of the documentation I encountered along the way tells me how to do this.
I have found this example comment:
$statement->bindValue(1, 'a title');
$statement->bindValue(2, 5, PDO::INT);
$statement->bindValue('active', true, 'boolean');
$statement->bindValue(5, new \DateTime(), 'date');
but in all these cases, the value provided is a constant. I need to pass in a string that is the name of the column that I want returned.
I tried both 'string' (resulted in quoted column name) and 'integer' (resulted in 0). I tried PDO::FETCH_COLUMN (seemed highly unlikely, but looked like the next best bet from http://php.net/manual/en/pdo.constants.php, and easy to try it...). I tried 'literal', inspired by the way you can put literal strings into expressions (resulted in Error: unknown type "literal"). That error message led me to src/Database/Type.php, but nothing in there helped me either.
So, I'm pretty much stumped. Here's a simple version of the code I have (leaving out a couple of conditions and unrelated columns):
$query = $this->Games->find();
$team_id = $query->newExpr()->addCase(
[$query->newExpr()->eq('Games.status', 'home_default')],
['home_team_id', 'away_team_id'],
['string', 'string']
);
$defaulting = $query
->select([
'id' => $team_id,
'count' => 'COUNT(Games.id)',
])
->where([
'Games.status IN' => ['home_default', 'away_default'],
])
->group('id')
->toArray();
This generates this SQL:
SELECT
(CASE WHEN Games.status = 'home_default'
THEN 'home_team_id' ELSE 'away_team_id' END) AS `id`,
COUNT(Games.id) AS `count`
FROM games Games
WHERE Games.status in ('home_default','away_default')
GROUP BY id
Note that THEN 'home_team_id' ELSE 'away_team_id' END should be simply THEN home_team_id ELSE away_team_id END. This will then allow me to read the list of ids of teams that have defaulted games along with the number of games they defaulted.
By default the values passed to the second argument of QueryExpression::addCase() are being treated as to be converted to literal values, not as identifiers. If you need the latter, then you should use an expression, an IdentifierExpression.
use Cake\Database\Expression\IdentifierExpression;
// ...
$team_id = $query->newExpr()->addCase(
[
$query->newExpr()->eq('Games.status', 'home_default')
],
[
new IdentifierExpression('Games.home_team_id'),
new IdentifierExpression('Games.away_team_id')
]
);
Also ditch the third argument in this case, you don't want the values to be string literals (for expressions the types would be ignored anyways).

different output for model name with capital letter in cakephp3

I dont understand how I get a different output for lesson_date field if in the select I use Lessons.lesson_date' or lessons.lesson_date. I thought I am supposed to use Lessons and not lessons and that the for a single name it doesnt really matter.
with Lessons.lesson_date I get on the debug :
'lesson_date' => object(Cake\I18n\FrozenDate) {
'time' => '2015-07-09T00:00:00+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
with lessons.lesson_date I get a better output:
'lessons' => [
'id' => '5399',
'lesson_date' => '2015-07-09'
//this is the code below I am talking about. The Lessons.lesson_date gives a different output than if I change this to lessons.lesson_date
$query3 = $this->Lessons->find()
->contain(['TutoringTypes'])
->select(['lessons.id','Lessons.lesson_date','Lessons.tutoring_type_id',
'TutoringTypes.value'])
->where(['Lessons.lesson_date >' => $a3,'Lessons.lesson_date <' => $a4, .....
That is the expected/correct behavior when following the conventions, the date gets casted according to its type.
The latter output might be better suited for what you are doing with the data, but generally the former is "better" since a date object gives you more freedom of manipulating dates, handling localization, output formatting, etc...
Why?
As to the why, the output is different because the ORM will not cast the value in case of a non-conventional column alias, as it's not present in the type map that holds the information about which column is of which type.
Using lessons.lesson_date will create an alias of
lessons__lesson_date
which is not following the conventions, where as using Lessons.lesson_date will create an alias of
Lessons__lesson_date
which does follow the conventions, and will match the field in the type map, causing the ORM to cast the data.
Changing the behavior
If you need YYYY-MM-DD output, then you could simply output it formatted it in your view like
echo $lesson->lesson_date->i18nFormat('yyyy-MM-dd')
or change the default output format (which is used when the date gets casted to a string)
\Cake\I18n\FrozenDate::setToStringFormat('yyyy-MM-dd');
or maybe even change the type in the type map in order to keep the date string as is
$query = $this->Lessons
->find()
// ...
$types = ['Lessons__lesson_date' => 'string'] + $query->selectTypeMap()->defaults();
$query->selectTypeMap()->defaults($types);
See also
Cookbook > Date & Time > Formatting
Cookbook > Date & Time > Setting the Default Locale and Format String
\Cake\ORM\Query::_addDefaultSelectTypes()
\Cake\Database\Query::selectTypeMap()
\Cake\Database\Query::typeMap()

CakePHP custom data object with grouping and nested arrays

I'm working on an app with extensive database relationships and I need to return a specific data object. I'm having trouble with it. Here's what I need it to look like:
AccommodationGroup =>
array(
['name']=>'Group1',
['AccommodationRoom']=> array(
[0]=> array(
['id']=>1,
['RoomIdentifier']=>'Cabin001',
),
[1]=> array(
['id']=>2,
['RoomIdentifier']=>'Cabin002'
)
)
)
AccommodationRoom is related to camp by camp_id. AccommodationGroup is related to AccommodationRoom by accommodation_group_id.
As you can see in the data object example, I need to retrieve all the groups for a particular camp with the rooms of each group as a nested array of the group, and all this restricted to a particular camp.
I've tried to get at it by selecting all the applicable groups using findAllByCampId() which, of course, doesn't work (knew it wouldn't but tried it anyway). Another table, AccommodationRoomsCamp is a transitional table between AccommodationRooms and Camps. One way to do it would be:
$this->AccommodationRoomsCamp->findAllByCampId($id, array('group'=>'AccommodationRoomsCamp.accommodation_group_id'))
but accommodation_group_id is not stored in AccommodationRoomsCamp because it is already being stored in the AccommodationRoom table. I think that I need to do more than one operation, but I'm baffled. Little bit of a newb. Ideas?
Have a little bit of a hard time following how your tables are related - but I think you can solve your problem using recursive finds. Something like this in your controller:
$this->AccomodationRoomsCamp->recursive = 2;
$this->AccomodationRoomsCamp->find('all, array('conditions' => array(bla bla bla bla
See http://book.cakephp.org/view/1063/recursive

Resources