简体   繁体   中英

Doctrine entity inherits from non entity class

We have a simple abstract Node class as follow:

<?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;
    }

}

and have a doctrine entity like below:

<?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;
    }
}

When updating database schema it generates only id field. How can inherit Node model properties and have them in database schema?

If you want to inherit the properties of the abstract class I would look into either making it a MappedSuperClass - Failing that you could make the class an entity and use Single table inheritance or something similar.

Example etc. can be found here: http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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