src / AppBundle / Entity / ShoppingCart / Order.php 解析了 OrderInterface ,它可以与Doctrine一起使用。
如果尝试在0
格式中使用它,那么我收到错误:
'class' => OrderInterface::class
当然可以直接定义Class "ShoppingCartBundle\Entity\OrderInterface" seems not to be a managed Doctrine entity. Did you forget to map it?"
,但在这种情况下会失去解决目标实体配置的优势。
的src / ComplaintsBundle /窗体/类型/ ComplaintType.php
'class' => Order::class
应用程序/配置/ config.yml
...
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('order', EntityType::class,
array(
'required' => true,
'class' => OrderInterface::class,
'property' => 'id',
)
);
}
...
答案 0 :(得分:0)
在您的表单中将doctrine.dbal.connection.event_manager
注明为evm
,将EntityManager
注入em
。
之后你可以尝试这样的事情:
private function resolveClass(string $className): string
{
$eventArgs = new OnClassMetadataNotFoundEventArgs($className, $this->em);
$this->evm->dispatchEvent(Events::onClassMetadataNotFound, $eventArgs);
$metadata = $eventArgs->getFoundMetadata();
if ($metadata === null) {
throw new InvalidArgumentException("Failed to resolve class $className");
}
return $metadata->getName();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('order', EntityType::class,
array(
'required' => true,
'class' => $this->resolveClass(OrderInterface::class),
'property' => 'id',
)
);
}
我没有真正测试过,只看了how Doctrine does this。
答案 1 :(得分:0)
在元数据加载期间解析实际类名。 要通过接口元数据加载来获取真正的类名,应该触发。 这可以通过下一个代码来实现
$entityManager = $managerRegistry->getManager();
$metadata = $entityManager->getClassMetadata(OrderInterface::class);
$realClassName = $metadata->getName();