- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
tl;dr getManagerForClass()
方法如何找出适合特定类的实体管理器?
我制作了一个通用 Controller ,它应该能够处理不同实体的基本操作。我还连接到两个不同的数据库,所以我使用了两个实体管理器。
在我的 Controller 中,我正在尝试使用 Doctrine 的 getManagerForClass() 方法来查找每个类要使用的管理器,如 on this blog 所解释的那样和 this SO answer .
但该方法似乎没有区分我的两个实体管理器,只是返回配置中的第一个。
我的 Controller Action 是这样开始的:
public function indexAction($namespace, $entityName)
{
$classFullName = "AppBundle:$namespace\\$entityName";
$em = $this->getDoctrine()->getManagerForClass($classFullName);
这是我的 Doctrine 配置:
dbal:
default_connection: postgres
connections:
postgres:
driver: pdo_pgsql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
oracle:
driver: oci8
host: "%oracle_host%"
port: "%oracle_port%"
dbname: "%oracle_name%"
user: "%oracle_user%"
password: "%oracle_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: true
entity_managers:
postgres:
connection: postgres
mappings:
AppBundle:
type: annotation
dir: Entity\Postgres
oracle:
connection: oracle
mappings:
AppBundle:
type: annotation
dir: Entity\Oracle
我的文件夹结构如下:
AppBundle
|___Controller
| |___EntityController.php
|
|___Entity
|___Postgres
| |___SomePostgresBasedEntity.php
|
|___Oracle
|___SomeOracleBasedEntity.php
现在我不知道这个方法是如何工作的,如果不通过配置它应该如何知道映射。但是如果我这样调用它,例如:
$em = $this->getDoctrine()->getManagerForClass("AppBundle:Oracle\\SomeOracleBasedEntity");
...我得到了 Postgres 的实体管理器。
但是,如果我简单地切换实体管理器配置,将 oracle 的配置放在第一位,则之前的调用有效,但以下调用无效:
$em = $this->getDoctrine()->getManagerForClass("AppBundle:Postgres\\SomePostgresBasedEntity");
getManagerForClass()
遍历每个管理器并针对每个管理器检查类是否“非 transient ”:
foreach ($this->managers as $id) {
$manager = $this->getService($id);
if (!$manager->getMetadataFactory()->isTransient($class)) {
return $manager;
}
}
这一直到 AnnotationDriver->isTransient()
。文档在这里说了以下内容:
A class is non-transient if it is annotated with an annotation from the AnnotationDriver::entityAnnotationClasses.
@Entity
似乎是使类非 transient 的注释之一。但是,我的任何实体怎么可能是短暂的呢?驱动程序如何仅根据其注释来区分属于特定管理器的实体?我一定是在高年级类(class)中漏掉了什么。
该方法在使用 yml
映射时有效。
我有点预料到这种行为。不同之处在于不同驱动程序中 isTransient() 方法的实现。如果元数据文件存在于映射配置的 dir:
目录中,则 isTransient 的 FileDriver 实现返回 true
。
我原以为 AnnotationDriver 只在指定的 dir:
目录中包含的实体中搜索注释,但它似乎忽略了该参数。还是我应该使用另一个?
最佳答案
终于,我解决了它。解决方案是使用 prefix
参数。
entity_managers:
postgres:
connection: postgres
mappings:
AppBundle:
type: annotation
dir: Entity\Postgres
prefix: AppBundle\Entity\Postgres
alias: Postgres
oracle:
connection: oracle
mappings:
AppBundle:
type: annotation
dir: Entity\Oracle
prefix: AppBundle\Entity\Oracle
alias: Oracle
prefix
参数被传递给相应的 Entity Manager 服务,并被添加到 entityNamespaces
属性中,否则默认为 AppBundle/Entity
。Annotation Driver 然后将检查特定命名空间 中的注释,而File Driver 检查通过dir
参数指定的目录中的现有映射文件。(alias
参数不是强制性的。)
至少,我是这么理解的。
关于php - 交响乐 2.8 : Doctrine getManagerForClass() not returning the right Entity Manager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40867556/
询问 unrelated question我有这样的代码: public boolean equals(Object obj) { if (this == obj) retur
在我之前的一个问题中 js: Multiple return in Ternary Operator我询问了有关使用三元运算符返回多个参数的问题。但是现在参数IsActveUser boolean(t
假设我有一个带有 return 的 if 语句。从效率的角度来看,我应该使用 if(A > B): return A+1 return A-1 或 if(A > B): return
例如考虑以下代码: int main(int argc,char *argv[]) { int *p,*q; p = (int *)malloc(sizeof(int)*10); q
PyCharm 对这段代码发出警告,说最后一个返回是不可访问的: def foo(): with open(...): return 1 return 0 如果 ope
我想实现这样的目标: 如果在返回 Json 的方法中抛出异常,则返回 new Json(new { success = false, error = "unknown"}); 但如果方法返回 View
它是多余的,但我正在学习 JS,我想知道它是如何工作的。 直接从模块返回函数 let func1 = function () { let test = function () {
我不明白我应该使用什么。我有两页 - intro.jsp(1) 和 booksList.jsp(2)。我为每一页创建了一个 Controller 类。第一页有打开第二页的按钮:
我最近在 Joomla 组件(Kunena,更准确地说是 Kunena)中看到这段代码,那么使用 $this->return VS 简单的 return 语句有什么区别. 我已经用谷歌搜索了代码,但没
我的类实现了 IEnumerable。并且可以编译这两种方式来编写 GetEnumerator 方法: public IEnumerator GetEnumerator() { yield r
我只是在编码,我想到了一个简单的想法(显然是问题),如果我有一个像这样的函数: int fun1(int p){ return(p); } 我有一个这样的函数: int fun1(int p){
这个问题在这里已经有了答案: What does the comma operator do in JavaScript? (5 个答案) 关闭 9 年前。 function makeArray
假设我写了一个 for 循环,它将输出所有数字 1 到 x: x=4 for number in xrange(1,x+1): print number, #Output: 1 2 3 4 现
我最近在这个 Apache Axis tutorial example. 中看到了下面的一段代码 int main() { int status = AXIS2_SUCCESS; ax
function a(){ return{ bb:"a" } } and function a(){ return { bb:"a" } } 这两个代码有什么区别吗,如果有请
function a() { return 1; } function b() { return(1); } 我在 Chrome 的控制台中测试了上面的代码,都返回了 1。 function c()
考虑这三个函数: def my_func1(): print "Hello World" return None def my_func2(): print "Hello World"
这可能是一个愚蠢的问题,但我正在努力,如果有一种简明的方法来测试函数的返回结果,如果它不满足条件,则返回该值(即,传递它)。。现在来回答一个可能的问题,是的,我正在寻找的类似于例外提供的东西。然而,作
我正在测试一个函数,并尝试使用 return 来做什么,并在 PowerShell 5.1 和 PwSh 7.1 中偶然发现了一个奇怪的问题,即 return cmdlet似乎不适合在团体中工作: P
这个问题已经有答案了: Return in generator together with yield (2 个回答) Why can't I use yield with return? (5 个回
我是一名优秀的程序员,十分优秀!