本文介绍了ZF2 + Doctrine2:错误时Zend的形式验证的一个集合场一对多的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

别的之前,感谢阅读和对不起我的英语水平。

实体:

Morfologia

 命名内容\\实体;/ **
 * Morfologia
 *
 * @ORM \\实体
 * @ORM \\表(名称=morfologia)
 * @annotation \\名称(morfologia)
 * @annotation \\保湿露(Zend的\\ STDLIB \\保湿\\ OBJECTPROPERTY)
 * /类Morfologia
 {
    / **
     * @ORM \\标识
     * @ORM \\ GeneratedValue(策略=AUTO)
     * @ORM \\列(类型=整数)
     * @annotation \\型号(Zend的\\表格\\元素\\隐藏)
     * /
     私人的$ id;     / ** @ORM \\列(类型=字符串)
      * @annotation \\型号(Zend的\\表格\\元素\\文本)
      * @annotation \\过滤器({名:StripTags})
      * @annotation \\过滤器({名:StringTrim})
      * @annotation \\验证({名:Alnum,选项:{allowWhiteSpace:真正的}})
      * @annotation \\验证({名:StringLength,选项:{分:2,最大:250}})
      * @annotation \\选项({标签:农布雷:})
      * * /
     私人$农布雷;     / ** @ORM \\列(类型=文本)
      * @annotation \\型号(Zend的\\表格\\元素\\多行文本)
      * @annotation \\过滤器({名:StripTags})
      * @annotation \\过滤器({名:StringTrim})
      * @annotation \\验证({名:Alnum,选项:{allowWhiteSpace:真正的}})
      * @annotation \\选项({标签:Descripcion:})
      * * /
     私人$ descripcion; // texto descriptivo     //(...)     / **
      *
      * @ORM \\一对多(targetEntity =帕特里莫尼奥的mappedBy =tipo_morfo)
      * @var帕特里莫尼奥[]
      * /
     私人$ patrimonios;     公共职能__construct()
     {
        $这个 - > patrimonios =新ArrayCollection的();
     }

协会的类型:
Morfologia - >一对多 - > Patrimonios

帕特里莫尼奥

  / **
 *帕特里莫尼奥
 *
 * @ORM \\实体
 * @ORM \\表(名称=帕特里莫尼奥)
 *
 * / 类帕特里莫尼奥//实现InputFilterAwareInterface
 {
    / **
     * @var整数的$ id
     *
     * @ORM \\标识
     * @ORM \\ GeneratedValue(策略=SEQUENCE)
     * @ORM \\列(名称=ID,类型=​​整数,可为空= FALSE)
     * /
     私人的$ id;     / * Propios德尔帕特里莫尼奥* /     / ** @ORM \\列(类型=字符串)* /
     私人$农布雷;     / ** @ORM \\列(类型=文本)* /
     私人$ descripcion; // texto descriptivo     (......)     / ** @ORM \\多对一(targetEntity =Morfologia,inversedBy =patrimonios)
      * @ORM \\ JoinColumn(可为空=真)* /
     私人$ tipo_morfo; //小费。 morfológico:PIRAMIDE等。     //(...)

控制器

 公共职能的addAction()
{
    $建设者=新DoctrineAnnotationBuilder($这个 - > getObjectManager());
    $形式= $ builder->的CreateForm(内容\\实体\\ Morfologia');    $ btnReg =新元素(发送);
    $ btnReg->的setValue(注册服务商);
    $ btnReg-> setAttributes(阵列(
        '型'=> '提交'
    ));
    $形式 - >添加($ btnReg);    $形式 - > setHydrator(新DoctrineHydrator($这个 - > getObjectManager(),内容\\实体\\ Morfologia'));    $请求= $这个 - >调用getRequest();
    如果($请求 - > isPost()){
        $ morfologia =新内容\\实体\\ Morfologia();
        $形式 - >绑定($ morfologia);
        $形式 - >使用setData($请求 - >的getPost());        如果(形式 - $>的isValid()){
            $ morfologia-> exchangeArray($形式 - >的getData());            $这个 - > getObjectManager() - GT;坚持($ morfologia);
            $这个 - > getObjectManager() - GT;的flush();            返回$这个 - >重定向() - GT; toRoute('内容',阵列(
                控制器=> morfologia',
                '行动'=> '指数'
            ));
        }
    }
    返回阵列(形式= GT; $形式);
}

查看

 < PHP
$形式= $这个 - >的形式;$形式 - >的setAttribute('行动',$这个 - > URL('内容',阵列(控制器= GT;'morfologia','行动'=>'加')));
$形式 - > prepare();回声$这个 - >的形式() - GT; openTag($形式);
    回声$这个 - >的FormCollection($形式);
回声$这个 - >的形式() - GT; closeTag();
?>
< / DIV>

在控制器验证表单:

 如果($形式 - >的isValid()){
  // ...
}

我收到以下错误:
学说\\ DBAL \\ DBALException

Archivo:

<$p$p><$c$c>C:\\xampp\\htdocs\\culturart\\php\\vendor\\doctrine\\dbal\\lib\\Doctrine\\DBAL\\DBALException.php:91

Mensaje:

 在执行SELECT t0.id AS ID1,t0.nombre AS nombre2,t0.descripcion AS descripcion3,t0.historia_breve AS historia_breve4,t0.extension AS extension5,T0发生异常.lat AS lat6,t0.lon AS lon7,t0.alt AS alt8,t0.perimetro AS perimetro9,t0.ubica_geo AS ubica_geo10,t0.medios_acceso AS medios_acceso11,t0.tipo_crono_id AS tipo_crono_id12,t0.tipo_morfo_id AS tipo_morfo_id13,t0.epoca_id AS epoca_id14,t0.cultura_id AS cultura_id15,t0.ubica_politica_id AS ubica_politica_id16,t0.img_superpuesta_id AS img_superpuesta_id17,t0.modelo_3d_id AS modelo_3d_id18 FROM帕特里莫尼奥T0 WHERE t0.id =?使用参数():SQLSTATE [22P02]:无效的文本再presentation:7错误:整数无效的输入语法:«»

堆栈跟踪:

  0#应用程序\\供应商\\主义\\ DBAL \\ lib目录\\主义\\ DBAL \\ Connection.php(702):学说\\ DBAL \\ DBALException :: driverExceptionDuringQuery(对象(PDOException), SELECT t0.id AS ...',阵列)
#1应用程序\\供应商\\主义\\ ORM \\ lib目录\\学说\\ ORM \\,持久\\ BasicEntityPersister.php(748):学说\\ DBAL \\ Connection的&GT;的executeQuery(SELECT t0.id AS ...',阵列,阵列)
#2应用程序\\供应商\\主义\\ ORM \\ lib目录\\学说\\ ORM \\ EntityManager.php(460):学说\\ ORM \\,持久\\ BasicEntityPersister-&GT;负载(阵列)
#3应用程序\\供应商\\主义\\主义模块的\\ src \\ DoctrineModule \\ STDLIB \\保湿\\ DoctrineObject.php(433):学说\\ ORM \\ EntityManager-&GT;找到(内容\\实体\\ ...','')
#4应用程序\\供应商\\主义\\主义模块的\\ src \\ DoctrineModule \\ STDLIB \\保湿\\ DoctrineObject.php(366):DoctrineModule \\ STDLIB \\保湿\\ DoctrineObject-&GT;找到('','内容\\实体\\ ... ')
#5应用程序\\供应商\\主义\\主义模块的\\ src \\ DoctrineModule \\ STDLIB \\保湿\\ DoctrineObject.php(239):DoctrineModule \\ STDLIB \\保湿\\ DoctrineObject-&GT; toMany(对象(内容\\实体\\ Morfologia),patrimonios ','内容\\实体\\ ...','')
#6应用程序\\供应商\\主义\\主义模块的\\ src \\ DoctrineModule \\ STDLIB \\保湿\\ DoctrineObject.php(107):DoctrineModule \\ STDLIB \\保湿\\ DoctrineObject-&GT; hydrateByValue(数组,对象(内容\\实体\\ Morfologia))
#7应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\表格\\ Fieldset.php(546):DoctrineModule \\ STDLIB \\保湿\\ DoctrineObject-&GT;水合物(数组,对象(内容\\实体\\ Morfologia))
#8应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\表格\\ form.php的(364):Zend的\\表格\\ Fieldset-&GT; bindValues​​(阵列)
#9应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\表格\\ form.php的(513):Zend的\\表格\\形式 - &GT; bindValues​​()
#10的应用程序\\模块\\内容的\\ src \\内容\\控制器\\ MorfologiaController.php(71):Zend的\\表格\\形式 - &GT;的isValid()
#11的应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\的mvc \\控制器\\ AbstractActionController.php(83):内容\\控制器\\ MorfologiaController-&GT;的addAction()
#12 [内部功能]:Zend的\\的mvc \\控制器\\ AbstractActionController-&GT; onDispatch(对象(Zend的\\的mvc \\ MvcEvent))
#13的应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\ eventmanager进行\\ EventManager.php(468):call_user_func(数组,对象(Zend的\\的mvc \\ MvcEvent))
#14的应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\ eventmanager进行\\ EventManager.php(207):Zend的\\ eventmanager进行\\ EventManager-&GT; triggerListeners('调度',对象(Zend的\\的mvc \\ MvcEvent),对象(封闭) )
#15的应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\的mvc \\控制器\\ AbstractController.php(117):Zend的\\ eventmanager进行\\ EventManager-&GT;触发器('调度',对象(Zend的\\的mvc \\ MvcEvent),对象(关闭))
#16的应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\的mvc \\ DispatchListener.php(114):Zend的\\的mvc \\控制器\\ AbstractController-&GT;讯(对象(Zend的\\ HTTP \\ PhpEnvironment \\请求),对象(Zend的\\ HTTP \\ PhpEnvironment \\响应))
#17 [内部功能]:Zend的\\的mvc \\ DispatchListener-&GT; onDispatch(对象(Zend的\\的mvc \\ MvcEvent))
#18的应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\ eventmanager进行\\ EventManager.php(468):call_user_func(数组,对象(Zend的\\的mvc \\ MvcEvent))
#19的应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\ eventmanager进行\\ EventManager.php(207):Zend的\\ eventmanager进行\\ EventManager-&GT; triggerListeners('调度',对象(Zend的\\的mvc \\ MvcEvent),对象(封闭) )
#20的应用程序\\供应商\\ zendframework \\ zendframework \\库\\ Zend的\\的mvc \\ Application.php(309):Zend的\\ eventmanager进行\\ EventManager-&GT;触发器('调度',对象(Zend的\\的mvc \\ MvcEvent),对象(封闭) )
#21的应用程序\\ PUBLIC \\的index.php(26):Zend的\\的mvc \\应用程序 - &gt;运行()
#{22}主

我想SELECT * FROM帕特里莫尼奥T0 WHERE t0.id使用参数()

我如何才能避免这个错误?
我如何注册一个新的实体M​​orfologia没有任何关联的帕特里莫尼奥并没有得到这个错误?


解决方案

我已经找到了问题:

 公共职能的addAction()
{
    $建设者=新DoctrineAnnotationBuilder($这个 - &GT; getObjectManager());
    $形式= $ builder-&GT;的CreateForm(内容\\实体\\ Morfologia');    //一些code    $形式 - &GT;删除('patrimonios'); //我应该从表格中删除集合场    ///一些code
}

终于从形式得到morfologia对象实例

 如果($形式 - &GT;的isValid()){
    $ morfologia = $形式 - &GT;的getData();    $这个 - &GT; getObjectManager() - GT;坚持($ morfologia);
    $这个 - &GT; getObjectManager() - GT;的flush();    //一些code
}

而morfologia正确刷新!

Before anything else, thanks for read and sorry for my english.

Entities:

Morfologia

namespace Content\Entity;

/**
 * Morfologia
 *
 * @ORM\Entity
 * @ORM\Table(name="morfologia")
 * @Annotation\Name("morfologia")
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 */

class Morfologia
 {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @Annotation\Type("Zend\Form\Element\Hidden")
     */
     private $id;

     /** @ORM\Column(type="string")
      * @Annotation\Type("Zend\Form\Element\Text")
      * @Annotation\Filter({"name":"StripTags"})
      * @Annotation\Filter({"name":"StringTrim"})
      * @Annotation\Validator({"name":"Alnum", "options": {"allowWhiteSpace":"true"}})
      * @Annotation\Validator({"name":"StringLength", "options": {"min":"2", "max":"250"}})
      * @Annotation\Options({"label":"Nombre: "})
      * */
     private $nombre;

     /** @ORM\Column(type="text")
      * @Annotation\Type("Zend\Form\Element\Textarea")
      * @Annotation\Filter({"name":"StripTags"})
      * @Annotation\Filter({"name":"StringTrim"})
      * @Annotation\Validator({"name":"Alnum", "options": {"allowWhiteSpace":"true"}})
      * @Annotation\Options({"label":"Descripcion: "})
      * */
     private $descripcion; //texto descriptivo

     //(...)

     /**
      *
      * @ORM\OneToMany(targetEntity="Patrimonio", mappedBy="tipo_morfo")
      * @var Patrimonio[]
      */
     private $patrimonios;

     public function __construct()
     {
        $this->patrimonios = new ArrayCollection();
     }

Type of association:Morfologia -> One to Many -> Patrimonios

Patrimonio

/**
 * Patrimonio
 *
 * @ORM\Entity
 * @ORM\Table(name="patrimonio")
 *
 */

 class Patrimonio //implements InputFilterAwareInterface
 {
    /**
     * @var integer $id
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\Column(name="id", type="integer", nullable=false)
     */
     private $id;

     /*Propios del patrimonio*/

     /** @ORM\Column(type="string") */
     private $nombre;

     /** @ORM\Column(type="text") */
     private $descripcion; //texto descriptivo

     (...)

     /** @ORM\ManyToOne(targetEntity="Morfologia", inversedBy="patrimonios")
      *  @ORM\JoinColumn(nullable=true) */
     private $tipo_morfo; //tip. morfológico: pirámide, etc.

     //(...)

Controller

public function addAction()
{
    $builder = new DoctrineAnnotationBuilder($this->getObjectManager());
    $form    = $builder->createForm('Content\Entity\Morfologia');

    $btnReg = new Element('send');
    $btnReg->setValue('Registrar');
    $btnReg->setAttributes(array(
        'type'  => 'submit'
    ));
    $form->add($btnReg);

    $form->setHydrator(new DoctrineHydrator($this->getObjectManager(),'Content\Entity\Morfologia'));

    $request = $this->getRequest();
    if ($request->isPost()){
        $morfologia = new Content\Entity\Morfologia();
        $form->bind($morfologia);
        $form->setData($request->getPost());

        if ($form->isValid()){
            $morfologia->exchangeArray($form->getData());

            $this->getObjectManager()->persist($morfologia);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('content', array(
                'controller' => 'morfologia',
                'action' => 'index'
            ));
        }
    }
    return array('form' => $form);
}

View

<?php
$form = $this->form;

$form->setAttribute('action', $this->url('content', array('controller' => 'morfologia', 'action' => 'add')));
$form->prepare();

echo $this->form()->openTag($form);
    echo $this->formCollection($form);
echo $this->form()->closeTag();
?>
</div>

When the controller validate the form:

if ($form->isValid()){
  //...
}

I get the following error:Doctrine\DBAL\DBALException

Archivo:

C:\xampp\htdocs\culturart\php\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php:91

Mensaje:

An exception occurred while executing 'SELECT t0.id AS id1, t0.nombre AS nombre2, t0.descripcion AS descripcion3, t0.historia_breve AS historia_breve4, t0.extension AS extension5, t0.lat AS lat6, t0.lon AS lon7, t0.alt AS alt8, t0.perimetro AS perimetro9, t0.ubica_geo AS ubica_geo10, t0.medios_acceso AS medios_acceso11, t0.tipo_crono_id AS tipo_crono_id12, t0.tipo_morfo_id AS tipo_morfo_id13, t0.epoca_id AS epoca_id14, t0.cultura_id AS cultura_id15, t0.ubica_politica_id AS ubica_politica_id16, t0.img_superpuesta_id AS img_superpuesta_id17, t0.modelo_3d_id AS modelo_3d_id18 FROM patrimonio t0 WHERE t0.id = ?' with params [""]:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: «»

Stack trace:

#0 app\vendor\doctrine\dbal\lib\Doctrine\DBAL\Connection.php(702): Doctrine\DBAL\DBALException::driverExceptionDuringQuery(Object(PDOException), 'SELECT t0.id AS...', Array)
#1 app\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php(748): Doctrine\DBAL\Connection->executeQuery('SELECT t0.id AS...', Array, Array)
#2 app\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php(460): Doctrine\ORM\Persisters\BasicEntityPersister->load(Array)
#3 app\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php(433): Doctrine\ORM\EntityManager->find('Content\Entity\...', '')
#4 app\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php(366): DoctrineModule\Stdlib\Hydrator\DoctrineObject->find('', 'Content\Entity\...')
#5 app\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php(239): DoctrineModule\Stdlib\Hydrator\DoctrineObject->toMany(Object(Content\Entity\Morfologia), 'patrimonios', 'Content\Entity\...', '')
#6 app\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php(107): DoctrineModule\Stdlib\Hydrator\DoctrineObject->hydrateByValue(Array, Object(Content\Entity\Morfologia))
#7 app\vendor\zendframework\zendframework\library\Zend\Form\Fieldset.php(546): DoctrineModule\Stdlib\Hydrator\DoctrineObject->hydrate(Array, Object(Content\Entity\Morfologia))
#8 app\vendor\zendframework\zendframework\library\Zend\Form\Form.php(364): Zend\Form\Fieldset->bindValues(Array)
#9 app\vendor\zendframework\zendframework\library\Zend\Form\Form.php(513): Zend\Form\Form->bindValues()
#10 app\module\Content\src\Content\Controller\MorfologiaController.php(71): Zend\Form\Form->isValid()
#11 app\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): Content\Controller\MorfologiaController->addAction()
#12 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#13 app\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#14 app\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#15 app\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 app\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#17 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#18 app\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#19 app\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#20 app\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#21 app\public\index.php(26): Zend\Mvc\Application->run()
#22 {main}

I tried to Select * FROM patrimonio t0 WHERE t0.id with params [""]

How can I avoid this error?How can I register a new entity Morfologia without any associated Patrimonio and don't get this error?

解决方案

I have found the problem:

public function addAction()
{
    $builder = new DoctrineAnnotationBuilder($this->getObjectManager());
    $form    = $builder->createForm('Content\Entity\Morfologia');

    //some code

    $form->remove('patrimonios'); //I should have removed the collection field from the form

    ///some code
}

Finally get the morfologia object instance from form

if ($form->isValid()){
    $morfologia = $form->getData();

    $this->getObjectManager()->persist($morfologia);
    $this->getObjectManager()->flush();

    //some code
}

And the morfologia was flushed correctly!

这篇关于ZF2 + Doctrine2:错误时Zend的形式验证的一个集合场一对多的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:01