检测Symfony2中的实体配置错误 [英] Detecting an entity mis-configuration in Symfony2

查看:88
本文介绍了检测Symfony2中的实体配置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试处理一个嵌套表单,具有多个对象 - 与属性关系。
错误表示它无法创建关联,因为其中一方需要一个ID。



好的。所以我试图创造只是失踪的一面。
但仍然是同样的错误。



最后,我意识到我仍然遇到与这个简单代码相同的问题:

  public function onSuccess(页$页)
{
$ this-> em-> flush();
}

我会感谢有一个错误信息,如嘿,没有什么冲!
但不是,仍然是同样的错误:我必须在关联它之前创建对象(和它的id)。



我看看堆栈跟踪。是的,似乎在flush之后调用了UnitOfWork - > computeAssociationChanges,并且需要相关对象的Ids。



如果在flush中创建数据库中的对象,命令产生一个错误?

解决方案

问题是由于实体类中的一个错误的拼写错误。
我通过检查模式结构,使用Symfony的附加组件找到它:

 <?php 
命名空间Lp\LibBundle\Command;

使用Symfony\Component\Console\Input\InputOption;
使用Symfony\Component\Console\Input\InputInterface;
使用Symfony\Component\Console\Output\OutputInterface;
使用Symfony\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
使用Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;

class MyValidateSchemaCommand扩展ValidateSchemaCommand
{

/ **
*(非PHPdoc)
* @see工具/控制台/命令/Doctrine\ORM\Tools\Console\Command.ValidateSchemaCommand::configure()
*
*修改命令在Doctrine命名空间中的名称
*添加帮助器em对于在Doctrine类中没有定义的实体管理器
* @author ulrich,09/09/11
* /
protected function configure()
{
parent ::配置();
$ this-> setName('doctrine:orm:validate-schema');
$ this-> addOption('em',null,InputOption :: VALUE_OPTIONAL,'用于此命令的实体管理器');
}

/ **
*(非PHPdoc)
* @see工具/控制台/命令/ Doctrine\ORM\Tools\Console\\ \\Command.ValidateSchemaCommand :: execute()
*
* Ajoute le Helper em pour l'entity manager qui n'est pasdéfinisdans la class Doctrine
* @author ulrich,09/09 $ 11
* /
protected function execute(InputInterface $ input,OutputInterface $ output)
{
DoctrineCommandHelper :: setApplicationEntityManager($ this-> getApplication(),$ input- > getOption( '时间'));

return parent :: execute($ input,$ output);
}



这是一个非常重要的事情。允许使用此命令指令来检测实体拼写错误。

 > php app / console doctrine:orm:validate-schema 


Well, I don't know what to do to fix a bug in my project.

I'm trying to handle a nested form with a many-to-many-with-attribute relationship. The errors says it cannot create the association because an ID is required for one of the side.

Alright. So I tried to create just the missing side. But still the same error.

Finally, I realized that I had still the same problem with this simple code :

public function onSuccess(Page $page)
{   
    $this->em->flush();
}

I would appreciate to have an error message such as "hey, there's nothing to flush!" But no, still the same error : I have to create the object (and its id) prior to associate it.

I had a look to the stack trace. And yes, it seems that UnitOfWork ->computeAssociationChanges is called after the flush, and requires Ids for associated objects.

How can I create the objects in the database if the flush command generates a bug ?

解决方案

Problem was due to a maj misspell in an entity class. I found it by checking the schema structure, using an add-on to Symfony:

<?php
namespace Lp\LibBundle\Command;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;

class MyValidateSchemaCommand extends ValidateSchemaCommand
{

/**
 * (non-PHPdoc)
 * @see Tools/Console/Command/Doctrine\ORM\Tools\Console\Command.ValidateSchemaCommand::configure()
 * 
 * Modifies name of the command to be in Doctrine namespace
 * Adds the Helper em for the entity manager which is not defined in Doctrine class 
 * @author ulrich, 09/09/11
 */
protected function configure()
{
    parent::configure();
    $this->setName('doctrine:orm:validate-schema');
    $this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
 * (non-PHPdoc)
 * @see Tools/Console/Command/Doctrine\ORM\Tools\Console\Command.ValidateSchemaCommand::execute()
 * 
 * Ajoute le Helper em pour l'entity manager qui n'est pas définis dans la class Doctrine
 * @author ulrich, 09/09/11
 */
protected function execute(InputInterface $input, OutputInterface $output)
{
    DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));

            return parent::execute($input, $output);
}

Source: Ulrich

This allowed to use this command instruction to detect the entity misspell.

> php app/console doctrine:orm:validate-schema

这篇关于检测Symfony2中的实体配置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆