问题描述
我正在尝试使用主义设置symfony,配置主义对我来说是新的。
I'm trying to setup symfony with doctrine, configuring doctrine is new for me.
我尝试通过使用以下代码从数据库中检索实体我的控制器:
I try to retrieve a entity from the database by using the following code in my controller:
$ feedImport = $ this-> getDoctrine()-> getRepository(MyClass :: class);
但是我一直收到 MappingException
错误:
在链配置的名称空间中找不到类'AppBundle\Entity\MyClass'
我还尝试手动指定完全限定的类名。但这无济于事。
I also tried to manually specify the fully qualified class name. But this didn't help.
但是我的错误信息似乎表明我需要做一些额外的配置:我需要添加实体/ repositories到配置的名称空间链。但是我在找到该链以及如何配置它方面遇到一些麻烦(如果我的假设是正确的话)
However the way I looked to it the error message seems to suggest I need to do some additional configuring: that I need to add my entities/repositories to "The chain of configured namespaces". But I have some trouble finding this chain and how to configure it (if my assumptions are correct ofcourse)
问题#1:是否存在某种命名空间链?如果在哪里可以看到如何配置它的示例?
Question #1: Is there some kind of namespace chain? If there is where can I see examples of how to configure it?
所有实体信息都在我的 AppBundle
中束。请参阅下面的包结构:
All the entity information is in my AppBundle
bundle. see the bundle structure below:
...
└── AppBundle
├── AppBundle.php
├── Controller
│ └── DefaultController.php
├── Entity
│ └── MyClass.php
├── Repository
│ └── MyClassRepository.php
└── Resources
└── config
└── doctrine
└── MyClass.orm.yml
...
我使用symfony bin\控制台工具。
I generated the entity class / repository / orm.yml files using the symfony bin\console
tool.
如果我使用相同的控制台
工具使用以下命令检查学说映射信息:
doctrine:mapping:info
命令我告诉我这些类已正确/成功映射:
If i use the same console
tool to check the doctrine mapping info with:doctrine:mapping:info
command I show me that the classes are correctly/sucessfuly mapped:
Found 1 mapped entity:
[OK] AppBundle\Entity\MyClass
关于模型/实体配置文件的生成过程。
我使用symfony 控制台
工具生成了元数据yaml文件。通过运行以下命令: doctrine:mapping:import
。在创建了yml文件之后,我使用了'doctrine:generate:entities'来生成enity类。
About the process of generation of the models/entity configuration files.I generated the meta data yaml files using the symfony console
tool. By running the command: doctrine:mapping:import
. After the yml files where created I used 'doctrine:generate:entities' to generate the enity classes.
给我的印象是,如果生成文件并将它们存储在 Entity
文件夹中的工作包中, auto_mapping:true
会自动处理此映射问题。
I was under the impression that if the files where generated and store in a working bundle in the Entity
folder, the auto_mapping: true
would automatically take care of this mapping thingy.
此假设正确吗?如果不是,请告诉我出什么问题了。
Is this assumption correct? If not please tell me what is wrong.
-
实体文件和配置存储在工作包 AppBundle
AppBundle
已注册并正在运行(控制器为
The AppBundle
is registered and working (the controller is doing his thing)
对于实体模型的完整代码:
For the full code of the Entity Model:
<?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;
}
}
任何建议都值得赞赏!。
Any words of advise is appreciated!.
推荐答案
经过长时间的努力,我发现了导致问题的原因。
我的symfony环境在 prod
模式下运行。我切换到 dev
。
After a long struggle I found out what caused the problem. My symfony environment was running in prod
mode. I switched to dev
. And it all worked like a charm.
但是我仍然想知道为什么?!我检查了 AppKernel.php
,它看起来像:
But i'm still wondering why?! I checked The AppKernel.php
and it looks like:
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
];
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
if ('dev' === $this->getEnvironment()) {
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
$bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
}
}
return $bundles;
在我看来,无论我在哪种环境下运行,都必须遵守学说。有人可以向我解释这种行为的原因。
It seems to me no matter what environment I run, doctrine is loaded. Can somebody explain to me the reason behind this behaviour.
编辑:
我发现了更多东西,我换了再次进入 prod
环境,但是现在我开始打开/关闭调试功能。
I discovered something more, I switched to prod
environment again but now I started flipping debugging on/off.
现在,我看到它在生产模式下可以很好地工作,但只有在启用调试后才可以。为什么?
Now I see that is works nicely in production mode but only when debugging is enabled. Why?
这篇关于原则无法在链中映射实体/存储库名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!