繁体   English   中英

原则实体从非实体类继承

[英]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.

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