【问题标题】:Symfony2.1 mapping error: class_parents()Symfony2.1 映射错误:class_parents()
【发布时间】:2012-07-26 01:55:10
【问题描述】:

我在尝试使用 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

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

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

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

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

【问题讨论】:

  • 您对config.yml 中的orm 部分的配置是什么?你能把它附在你的问题上吗?
  • 是的,我已经在邮件中附加了它,但它是默认设置。在另一个配置相同的 Symfony2 项目中,一切正常
  • 可能您还必须在配置中手动设置映射? symfony.com/doc/current/reference/configuration/doctrine.html

标签: php orm doctrine symfony-2.1


【解决方案1】:

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

编辑:默认 Symfony2 和 Doctrine 自动加载器符合PSR-0

【讨论】:

    【解决方案2】:

    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。您无需更改表名。

    【讨论】:

    • 谢谢!完美工作。奇怪的是,SYS_TCountry 类在 Symfony2.0 项目中运行良好。 2.1在这个问题上更加严格。 Un saludo :)
    猜你喜欢
    • 2012-11-04
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多