开发者

Doctrine 1.2 ORM inherited class cant acces to its parent relations

having the class A and the class B that inherits from A, and class C to which A has a ratio of 1: M respectively (A, C), when creating an object of the class B and trying to access form $B->C->attributeOfC throws Doctrine_Record_UnknownPropertyException 'with message' Unknown record property / related component

A simple example in code:

//A
class tableA extends Doctrine_Record{    
    public function setTableDefinition() {
        $this->hasColumn('tableA_id', 'integer', null, array(
            'primary' => true, 'autoincrement' => true));          
            $this->hasColumn('tableC_id','integer');
             $this->setSubclasses(array(
                'tableB'  => array('type' => 1)
            ));  }
    function setup() {
        $this->setTableName("tableA");
        $this->hasOne('tableC', array(
            'local' => 'tableC_id',
            'foreign' => 'tableC_id'
                ));    }
}
//B
class tableB extends tableA{
   public function setTableDefinition() {
        $this->hasColumn('tableB_id', 'integer', null, array(
            'primary' => true, 'autoincrement' => true));
            $this->hasColumn('tableA_id','integer');        
    }
    function setup() {
        $this->setTableName("tableB");
    }
}

//C
class tableC extends Doctrine_Record{   
    public function setTableDefinition() {
        $this->hasColumn('tableC_id', 'integer', null, array(
            'primary' => true, '开发者_如何学Pythonautoincrement' => true));
            $this->hasColumn('attributeOfC','string');
    }
    function setup() {
        $this->setTableName("tableC");
         $this->hasMany('tableA as Alias', array(
            'local' => 'tableC_id',
            'foreign' => 'tableC_id'
                )); }
}
 //some code where we create $objectOfCClass as an instance of tableCe
 $objectA = new $tableA(); 
 $objectA->tableC=$objectOfCClass;
 $objectA->save();


When you define a function in a subclass, such as "setTableDefinition" in TableB, it does not call the parent function. You need to explicitly call parent::setTableDefinition() in the tableB setTableDefinition function, as well as parent::setup() in the tableB setup function.

//B
class tableB extends tableA{
   public function setTableDefinition() {
        parent::setTableDefinition();
        $this->hasColumn('tableB_id', 'integer', null, array(
            'primary' => true, 'autoincrement' => true));
            $this->hasColumn('tableA_id','integer');        
    }
    function setup() {
        parent::setup();
        $this->setTableName("tableB");
    }
}

Now, it looks like tableA and tableB are actually different tables with different columns, and the above code will break because it will tell tableB that it has a column called tableA_id. So you'll have to move the code around based on which tables have which columns. You could just copy he "hasOne" call into the tableB setTabledefinition function.

The thing to remember is that this is all just PHP code setting up the objects, and the inheritance of these classes works the same way as other PHP classes. So bring the stuff that is common into a parent class, and the stuff that is different into the children.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜