Search code examples
phpzend-frameworkdoctrine-ormdoctrineclass-table-inheritance

Zend Framework 1.11 + Doctrine 2 + Class Inheritance mapping (YAML) problems with generate entities


I get number of problems trying to implement CTI

First of all I use a custom loader for my entity classes

class My_AutoLoader implements Zend_Loader_Autoloader_Interface
{

    public function autoload($class)
    {
        $class = trim(str_replace('\\', '/', $class), '/');
        if (@include(APPLICATION_PATH . '/Entities/' . $class . '.php')) {
            return $class;
        } else {
            throw new Zend_Loader_Exception('Cannot load ' . $class . '.');
        }
    }

}

The idea is to use application\Entities for classes that have no namespace like $user = new Users();

Then I have defined class inheritance

Profiles:
  type: entity
  table: profiles
  repositoryClass: Repositories\Base
  inheritanceType: JOINED
  discriminatorColumn:
    name: profiletype
    type: integer
    length: 11
  discriminatorMap:
    1: Personal
    2: Work
    3: Business
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    firstname:
      type: string
      length: 255
      fixed: false
      nullable: true
    ...


Work:
  type: entity
  table: work
  repositoryClass: Repositories\Base
  fields:
    position:
      type: string
      length: 255
      fixed: false
      nullable: true

then I have manually created the class Work to extend Profiles

class Work extends Profiles
{
}

The first problem begun with 2.0.0 (2.0.1), when I use console tool's generate-entities I get the error that I don't have id's for the Work class, it is odd because IMHO it contradicts with the idea that Work extends Profiles and id is already defined.

However I tried to add a column id for the Work class, but then I get a message that I already have a column id. DOH!

I tried to add some other column name for PK but I actually get an extra column that is unnecessary because the proper inherited column id is also created. In CTI i should have a single FK column and there are no other PK's with auto-generated values.

So I did the bad thing to hack the doctrine classes and remove the checks for missing id's. Ugly but it worked. The entities start to generate properly and the db structure is just fine.

I later found that all that strange behavior is due to a bug in doctrine 2 and it is fixed in 2.0.5.

Well, I tried 2.0.5 and had exactly the same problem, so I thought the mistake is in my code.

I filed a bug in doctrine's jira and I got answered that my definitions are wrong and I need id's for subclasses (and got referred to the documentation that all we know is quite poor, especially for YAML mapping). I gave up and sticked with my hack.

Later I tried with 2.0.6 and 2.1 but with those versions my entities are no longer updated but each time i use generate-entities the new class definitions are being append to the end so there are duplicates.

My question is:

Is this a problem with doctrine or I am doing it wrong?

If it is in me what is the proper way of mapping CI


Solution

  • Taken from your question:

    Update: I found that the problem is actually a bug in Doctrine which always prefix the namespace with "\" when updating entities and my custom autoloader loads only classes without namespace. In addition there is a bug with inheriting properties (ids)

    Both will be fixed in 2.1.1