If its an option, restructure the table and remove the special character. Its a little disturbing its there in the first place!
Otherwise, if that’s no option, you’re probably up for hand writing the controller, entity, table and templates - as I doubt anyone will be jumping to fix the bake command to handle a colon.
Only if you follow these CakePHP and the Bake Plugin can do 100% of their magic and help you as much as possible.
If you can’t change that you will have to manually adjust your baked files to what is present in the database since bake expects you to follow the convention.
An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.
Permitted characters in unquoted identifiers:
ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)
It could be that this is different for your used DBMS but it just (as you have seen) creates unforseen problems.
You can enable that for your Datasource via setting that flag in your config to true
If you overwrite that via your app_local.php then add that key=>value to that files datasource.
But also as the comment says its not recommended to do so if you can avoid it because escaping all the identifiers for every query decreases the performance
If you can and do restructure, before you do anything else (after the restructure) run this command: - bin/cake cache clear_all
and save yourself a world of hair-pulling!
On second thoughts, the bake doesn’t seem the problem, it outputs the right code as far as my little knowledge goes.
First problem was that the query builder couldn’t handle the ‘:’ well. With the ‘‘quoteIdentifiers’ => true’ the query builder seems to produce well defined output.
With the first problem solved, the second gets to the surface.Where is the real problem, a ‘parsing’ problem, skip the ‘:’ as a delimiter, escape it. Or can’t the variable in the array have a name with a ‘:’? Basic stuff for most of you, not for me.
So, when not baking but coding by hand I would come up with the same code (I think/hope), and having the same problem.
Only when it’s possible to treat the ‘data:3’ as one part by adding some tokens or whatever, in that case the bake has an issue. Well, along my line of thought with my little knowledge.
The database has imports/exports/other applications, it’s no option to rename the column-names, all have to be changed, some by third parties. This is not going to happen (costs, time, riscs etc.)
An other solution I used in the past was having a database-view with renamed columns. Always had a lot of discussion with DBA’s and a lot of paperwork to do to get them in place. And could take long time or short time, the view would give trouble (update’s etc.)
Next in line was keeping the renaming within application in some way or another. Apart from the extra work, long-term maintenance gets a problem. It’s not the way to go.
Any other options?
Well, apart from being my usecase, imho it’s a core cakephp question in to what extend it keeps up with database-features (and if not doing so for 100%, it’s ok, but you want to know).
The fact that it’s possible to use ‘‘quoteIdentifiers’ => false/true’ indicates that cakephp is aware of the feature, the query-builder seems to adjust it’s behaviour.
There’s seems to be one more step to take (and it’s ok if you have to change the baked code by hand).
Well the base problem here is the fact, that CakePHP maps PHP Object properties to column names.
And since $object->f18:0_g is not a valid property just by PHP syntax standard you would have to somehow map custom property names to your columns. But I am currently not aware of how to do that.
So I’m searching for a solution for an issue between MariaDB and PHP, will not be the first person to have this issue, will find something (the not-wanted databaseview will be plan b).
With that you don’t get entities and instead just the data as an array as it is present in the DB.
Now you just have to manually adjust your templates to access the data via array keys instead of properties.
Little known fun fact, PHP actually accepts all sorts of characters for properties, you just cannot use regular accessors, but would have to use curly braces (aka variable property access):
$obj = new stdClass();
$obj->{'foo:bar'} = 42;
var_dump(get_object_vars($obj));
var_dump($obj->{'foo:bar'});
This will work fine. With CakePHP entities you can use curly braces too, or, alternatively, you can use the get/set methods:
Yeah, I guess it would’ve been best for humanity if it would’ve been kept a secret. It’s ugly, error prone, hard to inspect, etc… but oh well, PHP gonna do what PHP does
It should absolutely be possible to work with such identifiers in CakePHP, but it will definitely involve some extra work, and one shouldn’t rely on Bake being able to support it. I guess it would be possible to even make bake work with it, but such column names are usually just legacy baggage, and seeing how PHP slowly starts discontinuing dynamic properties, this isn’t something that the framework should waste time on.