Symfony2.1 映射错误:class_parents()

2022-01-16 00:00:00 orm php doctrine symfony-2.1

我在尝试使用 Symfony2.1 项目中的 Doctrine2 从表(通过实体)获取数据时遇到问题.这是我得到错误的控制器:

I have a problem trying to get data from a table (via entity) using Doctrine2 in a Symfony2.1 project. Here is the controller where I get the error:

/**
 * 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 CompanyProjectBaseBundleEntity;

use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;

/**
 * @ORMEntity
 * @ORMTable(name="SYS_TCountry")
 */
class SYS_TCountry
{
    /**
     * @ORMId
     * @ORMColumn(type="string", length=3, nullable=false)
     * @var string
     */
    protected $idcountry;

    /**
     * @ORMColumn(type="string", length=75, nullable=false)
     * @AssertNotBlank()
     * @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:

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 CompanyProjectBaseBundleEntitySYS_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

这很奇怪,因为正如 Doctrine 在控制台中所说,映射已正确完成:我测试它执行 php app/console dictionary:mapping:info:

It is strange because, as Doctrine says in console, the mapping is properly done: I test it executing php app/console doctrine:mapping:info:

[OK]   CompanyProjectBaseBundleEntitySYS_TCountry

如果我在控制台中执行查询一切正常 -> 应用程序/控制台原则:查询:sql 'SELECT * FROM SYS_TCountry',返回结果.

and if I execute a query in console everything goes fine -> app/console doctrine:query:sql 'SELECT * FROM SYS_TCountry', that return results.

我不知道是否使用 Symfony2.1 我必须配置一些与 2.0 版本不同的东西,但似乎相同,因为映射是 Doctrine 责任.

I do not know if using Symfony2.1 I have to configure something different to the 2.0 version, but seems the same because the mapping is Doctrine responsibility.

推荐答案

Symfony 遵循 PSR-0 文件名标准.这意味着如果你在类名中使用下划线,在决定你的类应该放在哪里时,它将用目录分隔符替换它,如下所示:

Symfony follows the PSR-0 standard for filenames. That, amongst other things, means that if you use an underscore in your class name, it will replace it with a directory separator when deciding where your class should live, like this:


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

所以,如果你有一个名为 SYS_TCountry 的类,它会期望在

So, if you have a class named SYS_TCountry it would expect to find it in

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

而不是

Company/Project/BaseBundle/Entity/SYS_TCountry.php

我认为最好的解决方案是将文件名和类名更改为 SYSTCountry.您无需更改表名.

I think that your best solution would be to change the filename and class name to SYSTCountry. You don´t need to change the table name.

相关文章