Details
-
Type:
New Feature
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 2.2
-
Component/s: None
-
Security Level: All
-
Labels:None
Description
I have created a custom type called point but need a way to have a function called on columns of this type when fetching / updating / inserting entities.
For example:
Consider I have an entity which has a column called the_geom
This is associated with my custom type point
A basic select statement for this entity is as follows:
SELECT id, name, the_geom FROM points where id = 1
The problem is the_geom is encoded and on the PHP side I want it in human readable form. I would like a way to specify custom functions on SELECT / INSERT / UPDATE for columns of this custom type.
When selecting my entity I would like to execute the function ST_AsText(the_geom)
SELECT id, ST_AsText(the_geom) FROM points where id = 1
The same goes for updates and inserts. I would like to surround the column SQL with other custom functions, to encode my human readable text back to the encoded value for the DB.
Possibly this could be integrated into Doctrine\DBAL\Types\Type ?
// Modify the column SQL for insert public function getSqlInsertColumn(....)
// Modify the column SQL for update public function getSqlUpdateColumn(....)
// Modify the column SQL for select public function getSqlSelectColumn(....)
// A possible example of getSqlSelectColumn(...) public function getSqlSelectColumn($doctrineSql) { // $doctrineSql == "p0_.the_geom" return 'ST_AsText(' . $doctrineSql . ')'; }
Issue Links
- is duplicated by
-
DBAL-53
Missing convertToDatabase-calls when loading and persisting entities
-
You can only fetch entities using the normalized value of any field, that means you have to find a common transformation that is saved in your entities and then converted on the fly.
There is a default converting method from database to PHP and back you could use, however this is only in one direction. You could define a class for the Point and convert from and to this Call in your Type. This way you could encapsualte all the converting logic in an object and your entities only use the object.
You can extend DQL to have additional queries, however when you select additional fields in DQL and apply a function to them, they are retrieved as scalar and not inside your entity. How to do this is detailed in the manual and the cookbook.