我已經從 symfony 3.4 遷移到 4.4,從使用包移動到 /src 我遇到了這個錯誤的物體問題
Uncaught PHP Exception Doctrine\ORM\Mapping\MappingException: "Class
"App\Entity\Regions" is not a valid entity or mapped super class.
我的物體看起來像這樣。我想知道這是不是因為找不到orm.xml?或者是其他東西?它在包/資源中。此外,作為 ORM 的 Doctrine\ORM\Mapping(比如未使用)是 orm.xml 正確的,這里的鏈接似乎顯示了非常不同的模式標簽https://symfony.com/doc/4.4/reference/configuration/doctrine.html
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Regions")
*/
/**
* Regions
*/
class Regions
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $region;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
public function getRegion()
{
return $this->region;
}
}
查詢物體
$regions = $this->getDoctrine()->getRepository(Regions::class);
$regionInfo = $regions->findOneBy(array('region' => strtolower($regionSearch)));
/src/Resources/config/doctrine 中的 ORM
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity repository-class="App\Repository\RegionsRepository" name="App\Entity\Regions">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="region" type="string" column="region" length="40" nullable="true"/>
</entity>
</doctrine-mapping>
uj5u.com熱心網友回復:
您的注釋應該設定在類宣告的正上方:
/**
* Regions
* @ORM\Entity
* @ORM\Table(name="Regions")
*/
class Regions
對于您當前的檔案,僅讀取第一個 docblock,并且由于缺少@ORM\Entity注釋,Doctrine 認為它不是有效的物體類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/401023.html
