4

I'm trying to implement a model/mapper type of interaction using Doctrine DBAL but ran into a few questions. Some of my column names have a '#' at the end. Changing the name is not an option. The ${'COL1#'} syntax works fine for regular variables, but PHP seems to have a hard time when it is used as an object property.

Parse error: syntax error, unexpected '$', expecting variable (T_VARIABLE in...

How can I have a model for a table with a hashtag in the field name?

d.lanza38
  • 2,525
  • 7
  • 30
  • 52
  • Question 1 : do you have Doctrine entities configured ? Question 2 : did you try to use the `Zend\Stdlib\Hydrator\ClassMethods` hydrator to hydrate an entity ? – ceadreak May 23 '16 at 18:51
  • @ceadreak I started out using Doctrine entities due to this issue, but these database tables are nearly 40 years old and relations are nearly non-existent. In addition we have developers who do not work in PHP (other languages but same tables) and I can't expect them to remember to change an entity in the event they make a change to the database. So I believe entities are not a possibility. As far as `ClassMethods` I used that to hydrate, but I still had to loop through each row. – d.lanza38 May 23 '16 at 18:59
  • Can you not create views in MySQL and rename your columns to something more friendly (without `#`)...? Doctrine supports the use of views for your model. – Wilt May 24 '16 at 07:38

2 Answers2

3

You could create views in MySQL and rename your columns to something more friendly in those views (something without #)...? Like this you won't have to alter your original tables, but you can still work around those naming issues.

Doctrine also supports the use of views for your model.

Many databases support all CRUD operations on views that semantically map to certain tables. You can create views for all your problematic tables and column names to...

They refer to a different scenario, but same solution using views could help out.

As I understood you are only using Doctrine DBAL, but anyway here some more information on using MySQL views with doctrine ORM, it might be helpful (to you or others).

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Thanks, this isn't the solution I ended up going with but it is still a possible solution for the problem. That's why I marked this as the answer. – d.lanza38 May 24 '16 at 17:47
1

I don't see the point of using Doctrine without entities.

If your database is really old (40 years old, omg !), you should use a DB abstract layer / framework such as Zend DB (sorry you're using ZF2) or Aura (http://auraphp.com/framework/1.x/en/sql/).

But if you really want to use Doctrine, you should create entities manually, and use magic setters - getters to handle special fields and access / hydrate your entities.

EDIT

Imagine your DB with a table Clients and 2 fields : id and name#1

class Client
{
   protected $id;
   protected $name1;

   public function __set()
   {
       // here you can set unknown properties
       // remove '#' e.g ...
   }

   public function setName1($name1)
   {
       $this->name1 = $name1;

       return $this;
   }

   public function getName1()
   {
       return $this->name1;
   }

   // ... other accessors

}

Usage :

$results = clients_query_result ...
$hydrator = new ClassMethods();
$clients = [];

foreach ($results as $result)
{
    $client = new Client();
    $clients[] = $hydrator->hydrate($result, $client);
}

// that's it, now you have a collection of Client objects
ceadreak
  • 1,662
  • 2
  • 18
  • 27
  • Thank you, I'm using Doctrine DBAL but not Doctrine ORM. I supposed I could use `Zend\DB` along with `Zend\Db\ResultSet\HydratingResultSet` instead, but that would still be looping through the result set twice from what I can tell. I'm not really asking about my particular situation. I'm just curious if everyone loops through their query results twice when using hydrators or if there is some other method/trick I'm unaware of. It just seems silly to loop through twice, but maybe the general conception is that having a model is worth the additional overhead. – d.lanza38 May 23 '16 at 21:06
  • It depends of your results set... If it's an object or an array, you can easily use the ClassMethods hydrator. But you don't need to use Doctrine for this case. It's impossible to create schema from entities, and impossible to create great entities from schema ... I updated my answer to show you an exemple of entity and ClassMethods hydration. hope it'll help – ceadreak May 23 '16 at 21:26
  • ClassMethods can't handle the hashtag. I have to use `Zend\Stdlib\Hydrator\ArraySerializable` and configure my `exchangeArray` and `getArrayCopy()` methods so they map the association from my table names to my object properties. – d.lanza38 May 24 '16 at 17:46