gpt4 book ai didi

php - Symfony2.1映射错误: class_parents()

转载 作者:可可西里 更新时间:2023-10-31 22:45:51 27 4
gpt4 key购买 nike

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

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

}

配置.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 doctrine:mapping:info:

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

如果我在控制台中执行查询,一切正常 -> app/console doctrine:query:sql 'SELECT * FROM SYS_TCountry',返回结果。

我不知道如果使用 Symfony2.1 我必须配置一些与 2.0 版本不同的东西,但看起来是一样的,因为映射是 Doctrine 的职责。

最佳答案

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

关于php - Symfony2.1映射错误: class_parents(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11686922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com