【发布时间】:2017-10-04 18:20:10
【问题描述】:
我正在尝试使用教义设置 symfony,配置教义对我来说是新的。
我尝试在控制器中使用以下代码从数据库中检索实体:
$feedImport = $this->getDoctrine()->getRepository(MyClass::class);
但我不断收到MappingException 错误:
The class 'AppBundle\Entity\MyClass' was not found in the chain configured namespaces
我还尝试手动指定完全限定的类名。但这没有帮助。
但是,我查看错误消息的方式似乎表明我需要进行一些额外的配置:我需要将我的实体/存储库添加到“配置的命名空间链”。但是我很难找到这个链以及如何配置它(如果我的假设是正确的)
问题 #1:是否存在某种命名空间链?如果有在哪里可以看到如何配置的示例?
所有实体信息都在我的AppBundle 包中。请参阅下面的包结构:
...
└── AppBundle
├── AppBundle.php
├── Controller
│ └── DefaultController.php
├── Entity
│ └── MyClass.php
├── Repository
│ └── MyClassRepository.php
└── Resources
└── config
└── doctrine
└── MyClass.orm.yml
...
我使用 symfony bin\console 工具生成了实体类/repository/orm.yml 文件。
如果我使用相同的 console 工具来检查学说映射信息:
doctrine:mapping:info 命令我告诉我类是正确/成功映射的:
Found 1 mapped entity:
[OK] AppBundle\Entity\MyClass
关于模型/实体配置文件的生成过程。
我使用 symfony console 工具生成了元数据 yaml 文件。通过运行命令:doctrine:mapping:import。在创建了 yml 文件之后,我使用了 'doctrine:generate:entities' 来生成实体类。
我的印象是,如果文件生成并存储在 Entity 文件夹中的工作包中,auto_mapping: true 会自动处理此映射。
这个假设正确吗?如果不是,请告诉我出了什么问题。
实体文件和配置存储在工作包
AppBundleAppBundle已注册并正在工作(控制器正在做他的事情)- 命令:
doctrine:mapping:info似乎会立即映射实体。
实体模型的完整代码:
<?php
namespace AppBundle\Entity;
/**
* MyClass
*/
class MyClass
{
/**
* @var integer
*/
private $id;
/**
* @var integer
*/
private $customerId;
/**
* @var integer
*/
private $feedId;
/**
* @var string
*/
private $contentHash;
/**
* @var string
*/
private $headerHash;
/**
* @var string
*/
private $feedName;
/**
* @var integer
*/
private $fileSize;
/**
* @var integer
*/
private $totalHeaderFields;
/**
* @var integer
*/
private $totalRecords;
/**
* @var string
*/
private $importStatus;
/**
* @var \DateTime
*/
private $datetimeCreated = 'CURRENT_TIMESTAMP';
/**
* @var \DateTime
*/
private $datetimeProcessed;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set customerId
*
* @param integer $customerId
*
* @return MyClass
*/
public function setCustomerId($customerId)
{
$this->customerId = $customerId;
return $this;
}
/**
* Get customerId
*
* @return integer
*/
public function getCustomerId()
{
return $this->customerId;
}
/**
* Set feedId
*
* @param integer $feedId
*
* @return MyClass
*/
public function setFeedId($feedId)
{
$this->feedId = $feedId;
return $this;
}
/**
* Get feedId
*
* @return integer
*/
public function getFeedId()
{
return $this->feedId;
}
/**
* Set contentHash
*
* @param string $contentHash
*
* @return MyClass
*/
public function setContentHash($contentHash)
{
$this->contentHash = $contentHash;
return $this;
}
/**
* Get contentHash
*
* @return string
*/
public function getContentHash()
{
return $this->contentHash;
}
/**
* Set headerHash
*
* @param string $headerHash
*
* @return MyClass
*/
public function setHeaderHash($headerHash)
{
$this->headerHash = $headerHash;
return $this;
}
/**
* Get headerHash
*
* @return string
*/
public function getHeaderHash()
{
return $this->headerHash;
}
/**
* Set feedName
*
* @param string $feedName
*
* @return MyClass
*/
public function setFeedName($feedName)
{
$this->feedName = $feedName;
return $this;
}
/**
* Get feedName
*
* @return string
*/
public function getFeedName()
{
return $this->feedName;
}
/**
* Set fileSize
*
* @param integer $fileSize
*
* @return MyClass
*/
public function setFileSize($fileSize)
{
$this->fileSize = $fileSize;
return $this;
}
/**
* Get fileSize
*
* @return integer
*/
public function getFileSize()
{
return $this->fileSize;
}
/**
* Set totalHeaderFields
*
* @param integer $totalHeaderFields
*
* @return MyClass
*/
public function setTotalHeaderFields($totalHeaderFields)
{
$this->totalHeaderFields = $totalHeaderFields;
return $this;
}
/**
* Get totalHeaderFields
*
* @return integer
*/
public function getTotalHeaderFields()
{
return $this->totalHeaderFields;
}
/**
* Set totalRecords
*
* @param integer $totalRecords
*
* @return MyClass
*/
public function setTotalRecords($totalRecords)
{
$this->totalRecords = $totalRecords;
return $this;
}
/**
* Get totalRecords
*
* @return integer
*/
public function getTotalRecords()
{
return $this->totalRecords;
}
/**
* Set importStatus
*
* @param string $importStatus
*
* @return MyClass
*/
public function setImportStatus($importStatus)
{
$this->importStatus = $importStatus;
return $this;
}
/**
* Get importStatus
*
* @return string
*/
public function getImportStatus()
{
return $this->importStatus;
}
/**
* Set datetimeCreated
*
* @param \DateTime $datetimeCreated
*
* @return MyClass
*/
public function setDatetimeCreated($datetimeCreated)
{
$this->datetimeCreated = $datetimeCreated;
return $this;
}
/**
* Get datetimeCreated
*
* @return \DateTime
*/
public function getDatetimeCreated()
{
return $this->datetimeCreated;
}
/**
* Set datetimeProcessed
*
* @param \DateTime $datetimeProcessed
*
* @return MyClass
*/
public function setDatetimeProcessed($datetimeProcessed)
{
$this->datetimeProcessed = $datetimeProcessed;
return $this;
}
/**
* Get datetimeProcessed
*
* @return \DateTime
*/
public function getDatetimeProcessed()
{
return $this->datetimeProcessed;
}
}
感谢任何建议!
【问题讨论】:
-
你的 PHP 版本是多少?
-
我的服务器正在运行 PHP 7.1
-
你能展示你的
Entity类和namespace吗? -
这是我的实体类:文件路径:
MyProject/src/AppBundle/Entity/MyClass.php` / class MyClass { /* * @var integer */ private $id; .....还有更多}` -
我想看看你的
Entity类的所有代码
标签: php entity-framework symfony doctrine-orm