简体   繁体   English

教义1.2 ORM继承的类不能加入其父级关系

[英]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 创建类B的对象并尝试访问$ B->时,具有从A继承的类A和类B,以及类A与类C的比率分别为1:M(A,C) C-> attributeOfC抛出Doctrine_Record_UnknownPropertyException'with message'未知记录属性/相关组件

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, 'autoincrement' => 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. 当您在子类中定义函数时,例如TableB中的“ setTableDefinition”,它不会调用父函数。 You need to explicitly call parent::setTableDefinition() in the tableB setTableDefinition function, as well as parent::setup() in the tableB setup function. 您需要在tableB setTableDefinition函数中显式调用parent :: setTableDefinition(),以及在tableB设置函数中显式调用parent :: setup()。

//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. 现在,看起来tableA和tableB实际上是具有不同列的不同表,并且上面的代码将中断,因为它将告诉tableB它具有称为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. 您可以将他的“ hasOne”调用复制到tableB setTabledefinition函数中。

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. 要记住的是,这仅仅是用PHP代码设置对象,并且这些类的继承与其他PHP类的工作方式相同。 So bring the stuff that is common into a parent class, and the stuff that is different into the children. 因此,将常见的东西带入父类,将不同的东西带入子级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM