"班级不是有效的实体......" EntityManager的错误#getClassMetadata(...)

时间:2017-10-25 15:45:21

标签: doctrine-orm doctrine phpunit entity metadata

我试图编写一个测试用例,将其发布在Doctrine的GitHub页面上,并且遇到架构生成问题。电话

$this->_em->getClassMetadata(Server::class)

导致异常

  

Class" Doctrine \ Tests \ Models \ DDC6786 \ Server"不是有效的实体或映射的超类。

我做错了什么? /如何让它运作?

CODE

DDC6786Test

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\Models\DDC6786\Endpoint;
use Doctrine\Tests\Models\DDC6786\EndpointServerConfig;
use Doctrine\Tests\Models\DDC6786\Server;
use Doctrine\Tests\OrmFunctionalTestCase;

class DDC6786Test extends OrmFunctionalTestCase
{

    public function testIssue()
    {
        $this->assertTrue(true);
    }

    public function setUp()
    {
        parent::setUp();
        try {
            $this->_schemaTool->createSchema(
                [
                    $this->_em->getClassMetadata(Server::class),
                    $this->_em->getClassMetadata(EndpointServerConfig::class),
                    $this->_em->getClassMetadata(Endpoint::class),
                ]
            );
        } catch(\Exception $e) {
            $breakpoint = null;
        }
    }

}

Endpoint

namespace Doctrine\Tests\Models\DDC6786;

use Doctrine\ORM\Mapping as ORM;
use ReflectionClass;

/**
 * Endpoint
 *
 * @ORM\Table(name="endpoint")
 * @ORM\Entity
 */
class Endpoint
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="role", type="string", nullable=true)
     */
    protected $role;

    /**
     * @var string
     *
     * @ORM\Column(name="server_place", type="string", nullable=true)
     *
     * @Groups({"export"})
     */
    protected $serverPlace;

    /**
     * @var string
     *
     * @ORM\Column(name="contact_person", type="string", length=500, nullable=true)
     *
     * @Groups({"export"})
     */
    protected $contactPerson;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    protected $created;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated", type="datetime", nullable=true)
     */
    protected $updated;

    /**
     * @var EndpointServerConfig
     *
     * @ORM\OneToOne(targetEntity="EndpointServerConfig", inversedBy="endpoint", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="endpoint_server_config_id", referencedColumnName="id")
     * })
     *
     * @Groups({"export"})
     */
    protected $endpointServerConfig;

    /**
     * @param integer $id
     *
     * @return Endpoint
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

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

    /**
     * @param string $role
     *
     * @return Endpoint
     */
    public function setRole($role)
    {
        $this->role = $role;

        return $this;
    }

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

    /**
     * @param string $type
     *
     * @return Endpoint
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * @return string
     *
     * @Groups({"export"})
     */
    public function getType()
    {
        return str_replace(
            'Endpoint',
            '',
            (new ReflectionClass($this))->getShortName()
        );
    }

    /**
     * @param string $serverPlace
     *
     * @return Endpoint
     */
    public function setServerPlace($serverPlace)
    {
        $this->serverPlace = $serverPlace;

        return $this;
    }

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

    /**
     * @param string $contactPerson
     *
     * @return Endpoint
     */
    public function setContactPerson($contactPerson)
    {
        $this->contactPerson = $contactPerson;

        return $this;
    }

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

    /**
     * @param \DateTime $created
     *
     * @return Endpoint
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * @param \DateTime $updated
     *
     * @return Endpoint
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * @param EndpointServerConfig $endpointServerConfig
     *
     * @return Endpoint
     */
    public function setEndpointServerConfig(EndpointServerConfig $endpointServerConfig = null)
    {
        $this->endpointServerConfig = $endpointServerConfig;

        return $this;
    }

    /**
     * @return EndpointServerConfig
     */
    public function getEndpointServerConfig()
    {
        return $this->endpointServerConfig;
    }

}

EndpointServerConfig

namespace Doctrine\Tests\Models\DDC6786;

use Doctrine\ORM\Mapping as ORM;

/**
 * EndpointServerConfig
 *
 * @ORM\Table(name="endpoint_server_config", indexes={
 *     @ORM\Index(name="fk_endpoint_server_config_server_idx", columns={"server_name"})}
 * )
 * @ORM\Entity
 */
class EndpointServerConfig
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="dns_address", type="string", length=253, nullable=true)
     */
    protected $dnsAddress;

    /**
     * @var Server
     *
     * @ORM\ManyToOne(targetEntity="Server")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="server_name", referencedColumnName="name")
     * })
     */
    protected $server;

    /**
     * @var Endpoint
     *
     * @ORM\OneToOne(targetEntity="Endpoint", mappedBy="endpointServerConfig")
     */
    protected $endpoint;

    /**
     * @param integer $id
     *
     * @return EndpointServerConfig
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

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

    /**
     * @param string $dnsAddress
     *
     * @return EndpointServerConfig
     */
    public function setDnsAddress($dnsAddress)
    {
        $this->dnsAddress = $dnsAddress;

        return $this;
    }

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

    /**
     * @param Server $server
     *
     * @return EndpointServerConfig
     */
    public function setServer(Server $server = null)
    {

        if(! $server || ! $server->getName()) {
            $server = null;
        }
        $this->server = $server;

        return $this;
    }

    /**
     * @return Server
     */
    public function getServer()
    {
        return $this->server;
    }

    /**
     * @param Endpoint $endpoint
     *
     * @return EndpointServerConfig
     */
    public function setEndpoint($endpoint)
    {
        $this->endpoint = $endpoint;

        return $this;
    }

    /**
     * @return Endpoint
     */
    public function getEndpoint()
    {
        return $this->endpoint;
    }

}

Server

namespace Doctrine\Tests\Models\DDC6786;

use Doctrine\ORM\Mapping as ORM;

/**
 * Server
 *
 * @ORM\Table(name="server")
 * @ORM\Entity
 *
 * @author automatix
 */
class Server
{

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=32, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    protected $name;

    /**
     * @var boolean
     *
     * @ORM\Column(name="active", type="boolean", nullable=true)
     */
    protected $active;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated", type="datetime", nullable=true)
     */
    protected $updated;

    /**
     * @var string
     *
     * @ORM\Column(name="node_name", type="string", length=50, nullable=true)
     */
    protected $nodeName;

    /**
     * @var string
     *
     * @ORM\Column(name="virtual_node_name", type="string", length=50, nullable=true)
     */
    protected $virtualNodeName;

    /**
     * Not relevant for ORM. Should become obsolete after and be removed after the migration to Doctrine.
     *
     * @var EndpointServerConfig[]
     */
    protected $endpointServerConfigs;

    /**
     * @param string $name
     *
     * @return Server
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

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

    /**
     * @param boolean $active
     *
     * @return Server
     */
    public function setActive($active)
    {
        $this->active = $active;
        return $this;
    }

    /**
     * @return boolean
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * @param \DateTime $updated
     *
     * @return Server
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;
        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * @param string $nodeName
     *
     * @return Server
     */
    public function setNodeName($nodeName)
    {
        $this->nodeName = $nodeName;

        return $this;
    }

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

    /**
     * @param string $virtualNodeName
     *
     * @return Server
     */
    public function setVirtualNodeName($virtualNodeName)
    {
        $this->virtualNodeName = $virtualNodeName;

        return $this;
    }

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

    /**
     * @param EndpointServerConfig[] $endpointServerConfigs
     *
     * @return Server
     */
    public function setEndpointServerConfigs($endpointServerConfigs)
    {
        $this->endpointServerConfigs = $endpointServerConfigs;
        return $this;
    }

    /**
     * @return EndpointServerConfig[] $endpointServerConfigs
     */
    public function getEndpointServerConfigs()
    {
        return $this->endpointServerConfigs;
    }

}

1 个答案:

答案 0 :(得分:0)

问题是由注释引起的。 use Doctrine\ORM\Mapping as ORM;& @ORM\Entity无效,但只有@ORM\Entity(和@ORM\Table等)。