-->

Symfony2.1映射错误:class_parents()(Symfony2.1 mapping

2019-06-26 13:33发布

我有一个问题想在Symfony2.1项目中使用Doctrine2从一个表(通过实体)获取数据。 这里是我的错误控制器:

/**
 * Country list
 */
public function countrylistAction()
{
    $em = $this->getDoctrine()->getManager();
    $countryList = $em->getRepository('ProjectBaseBundle:SYS_TCountry')
        ->findAll();

    $serializer = new Serializer(array(new GetSetMethodNormalizer()),
        array('json' => new JsonEncoder()));

    return new Response($serializer->serialize($countryList, 'json'));
}

实体:

<?php

namespace Company\Project\BaseBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="SYS_TCountry")
 */
class SYS_TCountry
{
    /**
     * @ORM\Id
     * @ORM\Column(type="string", length=3, nullable=false)
     * @var string
     */
    protected $idcountry;

    /**
     * @ORM\Column(type="string", length=75, nullable=false)
     * @Assert\NotBlank()
     * @var string
     */
    protected $name;

    ....

    public function getIdcountry()              { return $this->idcountry; }
    public function getName()                   { return $this->name; }
    public function getA2()                     { return $this->a2; }
    public function getA3()                     { return $this->a3; }
    public function getIdstatus()               { return $this->idstatus; }
    public function setIdcountry($idcountry)    { $this->idcountry = $idcountry; }
    public function setName($name)              { $this->name = $name; }
    public function setA2($a2)                  { $this->a2 = $a2; }
    public function setA3($a3)                  { $this->a3 = $a3; }
    public function setIdstatus($idstatus)      { $this->idstatus = $idstatus; }

    public function __toString()                { return $this->idcountry; }

}

Config.yml:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

这就是错误:

Warning: class_parents(): 
Class Company\Project\BaseBundle\Entity\SYS_TCountry does not exist and could not be loaded in 
/var/www/project/src/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php line 40

这很奇怪,因为,作为学说在控制台说,映射做得好:我测试了执行PHP应用程序/控制台学说:映射:信息

[OK]   Company\Project\BaseBundle\Entity\SYS_TCountry

如果我在控制台执行查询一切顺利 - >应用程序/控制台学说:查询:SQL“SELECT * FROM SYS_TCountry”,即返回结果。

我不知道,如果使用Symfony2.1我必须配置一些东西到2.0版本不同,但似乎相同,因为映射是教义责任。

Answer 1:

Symfony的遵循文件名的PSR-0标准。 也就是说,除其他事项外,这意味着,如果你在你的类名称中使用下划线,它会以目录分隔符决定在哪里你的类应该住,这样当更换:

\namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php

所以,如果你有一个名为SYS_TCountry类它希望找到它

Company/Project/BaseBundle/Entity/SYS/TCountry.php

代替

Company/Project/BaseBundle/Entity/SYS_TCountry.php

我认为你最好的解决办法是将文件名和类的名称更改为SYSTCountry。 你鸵鸟政策需要更改表名。



Answer 2:

您的类实体名称不符合PSR-0这将导致加载错误。 如果重命名实体SYSTCountry一切都将正常工作!

编辑:默认的Symfony2和学说自动加载 PSR-0标准。



文章来源: Symfony2.1 mapping error: class_parents()