[英]Doctrine entity inherits from non entity class
我們有一個簡單的抽象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;
}
}
更新數據庫架構時,它僅生成id字段。 如何繼承Node
模型屬性並將其置於數據庫模式中?
如果您想繼承抽象類的屬性,我會考慮將其設為MappedSuperClass-如果失敗,則可以使該類成為實體並使用Single table繼承或類似方法。
示例等可以在這里找到: http : //doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.