2015-03-09 3 views
1

У нас есть простой абстрактный Node класса следующим образом:Доктрина объект наследует от не класса сущностей

<?php 

namespace DataSource\CoreBundle\Model; 

/** 
* Class Node 
* @package DataSource\CoreBundle\Model 
*/ 
abstract class Node 
{ 
    protected $id; 

    /** 
    * @var Node Parent of current node. 
    */ 
    protected $parent = null; 

    /** 
    * @var Node[] Children of the current node. 
    */ 
    protected $children = array(); 

    /** 
    * @var string Url of the node. 
    */ 
    protected $url; 

    /** 
    * @var string Label of the node. 
    */ 
    protected $label; 

    /** 
    * @return mixed 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @return Node[] 
    */ 
    public function getChildren() 
    { 
     return $this->children; 
    } 

    /** 
    * @param Node[] $children 
    */ 
    public function setChildren(array $children) 
    { 
     $this->children = $children; 
    } 

    /** 
    * @return bool 
    */ 
    public function hasChildren() 
    { 
     return count($this->children) > 0; 
    } 

    /** 
    * Add child to the children list. 
    * 
    * @param Node $child 
    */ 
    public function addChild(Node $child) 
    { 
     $this->children[] = $child; 
    } 

    /** 
    * Remove child from children list. 
    * 
    * @param Node $child 
    */ 
    public function removeChild(Node $child) 
    { 
     foreach ($this->children as $key => $_child) { 
      if ($child->isEqual($_child)) { 
       unset($this->children[$key]); 
      } 
     } 
    } 

    /** 
    * Checks if current node is same as the given node. 
    * 
    * @param Node $node 
    * @return bool 
    */ 
    public function isEqual(Node $node) 
    { 
     return $node->url == $this->url; 
    } 

    /** 
    * @return string 
    */ 
    public function getLabel() 
    { 
     return $this->label; 
    } 

    /** 
    * @param string $label 
    */ 
    public function setLabel($label) 
    { 
     $this->label = $label; 
    } 

    /** 
    * @return Node 
    */ 
    public function getParent() 
    { 
     return $this->parent; 
    } 

    /** 
    * @param Node $parent 
    */ 
    public function setParent($parent) 
    { 
     $this->parent = $parent; 
    } 

    /** 
    * @return string 
    */ 
    public function getUrl() 
    { 
     return $this->url; 
    } 

    /** 
    * @param string $url 
    */ 
    public function setUrl($url) 
    { 
     $this->url = $url; 
    } 

} 

и есть учение сущность, как показано ниже:

<?php 

namespace DataSource\CoreBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Advertise 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="DataSource\CoreBundle\Repository\AdvertiseRepository") 
*/ 
class Advertise extends \DataSource\CoreBundle\Model\Node 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 
} 

При обновлении схемы базы данных он генерирует только идентификатор поле. Как можно наследовать свойства модели Node и иметь их в схеме базы данных?

ответ

0

Если вы хотите наследовать свойства абстрактного класса, я бы рассмотрел либо создание MappedSuperClass - в противном случае вы могли бы сделать класс сущностью и использовать однонамерное наследование или что-то подобное.

Примера и т.д., можно найти здесь: http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html

Надеется, что это помогает!

Смежные вопросы