简体   繁体   English

Symfony 2嵌入式表单:可捕获的致命错误:传递给Entity的参数1 :: addProperty必须是XX \\ MyClass的一个实例,给出的数组

[英]Symfony 2 Embedded Forms: Catchable Fatal Error: Argument 1 passed to Entity::addProperty must be an instance of XX\MyClass, array given

I've already read this and this and other ones but no one solved my problem. 我已经读过和其他的人,但没有一个解决我的问题。

I've removed all that I could and narrowed down to one field: adresses. 我已经删除了所有可能的内容并缩小到一个字段:地址。 When I try to follow this tutorial , everything works fine if I follow it, starting from a fresh new project. 当我尝试按照本教程学习时 ,如果我遵循它,从一个全新的项目开始,一切正常。 But when I do it by hand in my other project, I get this error: "Catchable Fatal Error: Argument 1 passed to BN\\Bundle\\MyBundle\\Entity\\PersonTeacher::addAdresse() must be an instance of BN\\Bundle\\MyBundle\\Entity\\Adresse, array given in C:\\Users\\Olivier\\PhpstormProjects\\My\\My\\src\\BN\\Bundle\\MyBundle\\Entity\\PersonTeacher.php line 111". 但是当我在我的其他项目中手动完成时,我收到此错误:“可捕获的致命错误:传递给BN \\ Bundle \\ MyBundle \\ Entity \\ PersonTeacher :: addAdresse()的参数1必须是BN \\ Bundle \\ MyBundle的实例\\ Entity \\ Adresse,在C:\\ Users \\ Olivier \\ PhpstormProjects \\ My \\ My \\ src \\ BN \\ Bundle \\ MyBundle \\ Entity \\ PersonTeacher.php第111行中给出的数组。

Heres the stacktrace: 继承人堆栈跟踪: stacktracke

Here's my code, where I've remove properties that work: 这是我的代码,我删除了有效的属性:


My controller: 我的控制器:

<?php

namespace BN\Bundle\MyBundle\Controller;

use BN\Bundle\MyBundle\Entity\PersonTeacher;
use BN\Bundle\MyBundle\Entity\Adresse;
use BN\Bundle\MyBundle\Form\Type\AdresseType;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class RegistrationController extends Controller
{
    public function registerTeacherAction(Request $request)
    {
        $person = new PersonTeacher();
        $form = $this->createFormBuilder($person)
            ->add('adresses', 'collection',
                array(
                    'type' => new AdresseType(),
                    'allow_add' => true,
                    'by_reference' => false,
                )
            )
        ->getForm();
        $form->handleRequest($request);
        if ($form->isValid()) {
            /**/
        }
        return $this->render('BNMyBundle:Registration:person_teacher.form.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

My Entity PersonTeacher: 我的实体PersonTeacher:

<?php

namespace BN\Bundle\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="person_teacher")
 * @ORM\Entity(repositoryClass="BN\Bundle\MyBundle\Repository\PersonTeacherRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class PersonTeacher extends Person
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /*--------- snap ---------*/

    /**
     * @ORM\ManyToMany(
     *     targetEntity="Adresse",
     *     inversedBy="personsTeacher",
     *     cascade={"persist"}
     * )
     * @ORM\JoinTable(name="person_teacher_adresse")
     **/
    private $adresses;

    /*--------- snap ---------*/

    public function addAdresse(\BN\Bundle\MyBundle\Entity\Adresse $adresse) {
        $this->adresses[] = $adresse;
        return $this;
    }
    public function removeAdresse(\BN\Bundle\MyBundle\Entity\Adresse $adresse) {
        $this->adresses->removeElement($adresse);
    }
    public function getAdresses() {
        return $this->adresses;
    }
    /*--------- snap ---------*/
    public function __construct() {
        parent::__construct();
        $this->adresses = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

Hints 提示

NB: I've checked that the form is properly sent, that the information are properly formatted, and I've done step by step debugging with xdebug, but it's too complex for me (yet) to see what I am missing. 注意:我已经检查过表单是否正确发送,信息格式是否正确,我是否已经使用xdebug逐步调试,但是对于我(还)看到我缺少的内容来说太复杂了。

It's a problem with the 'collection' type: if I remove 'by_reference' => false , then it works.... only if I do not persist it. 这是'collection'类型的一个问题:如果我删除'by_reference' => false ,那么只有当我不坚持它时它才有效。

If I try to persist it with this code: 如果我尝试使用此代码保留它:

    $form->handleRequest($request);
    if ($form->isValid()) {
        $person=$form->getData();
        $em = $this->getDoctrine()->getManager();
        $em->persist($person);
        $em->flush();
    }

Then I get this error: 然后我收到这个错误:

Warning: spl_object_hash() expects parameter 1 to be object, array given in (projectpath)\\vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\UnitOfWork.php line 1572 警告:spl_object_hash()期望参数1是对象,在(projectpath)\\ vendor \\ doctrine \\ orm \\ lib \\ Doctrine \\ ORM \\ UnitOfWork.php第1572行中给出的数组

Why? 为什么?

I've found it. 我找到了。

The problem was in my FormType AddressType.php file: the function setDefaultOptions() was missing. 问题出在我的FormType AddressType.php文件中:缺少函数setDefaultOptions() Everything was okay, but this function ir mandatory if you want doctrine to be able to convert the incoming fields into a specific class. 一切都还可以,但是如果你希望教义能够将传入的字段转换为特定的类,那么这个函数是强制性的

public function setDefaultOptions(
    \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
) {
    $resolver->setDefaults(array(
        'data_class' => 'MyBundle\Entity\Adresse',
    ));
}

I'm quite angry about the time I've lost with that. 我对此失去的时间感到非常生气。 You have to know by heart the whole symfony 2 process to solve quickly those kind of problems, or it will take 6 hours like it did to me. 你必须全心全意地了解整个symfony 2过程,以便快速解决这类问题,或者它需要花费6个小时才能完成。 Symfony 2 itself is a whole leaky abstraction : you feel like gaining time but you dont. Symfony 2本身就是一个完全漏洞的抽象 :你觉得自己有时间,但是你没有。 Symfony is supposed to simplify your life, but in the end, to quote Joël Spolsky: Symfony应该简化你的生活,但最后引用JoëlSpolsky:

It's a leaky abstraction: it means that abstractions do not really simplify our lives as much as they were meant to. 这是一个漏洞的抽象:它意味着抽象并没有像他们想要的那样真正简化我们的生活。 Code generation tools which pretend to abstract out something, like all abstractions, leak, and the only way to deal with the leaks competently is to learn about how the abstractions work and what they are abstracting. 代码生成工具假装抽象出一些东西,比如所有抽象,泄漏,以及处理泄漏的唯一方法是学习抽象如何工作以及抽象的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可捕获的致命错误:传递给Symfony :: render()的参数3必须是Symfony \\ .. \\ Response的实例,给定数组 - Catchable Fatal Error: Argument 3 passed to Symfony::render() must be an instance of Symfony\..\Response, array given 可捕获的致命错误:传递给...的参数1必须是...,给定数组的实例 - Catchable Fatal Error: Argument 1 passed to … must be an instance of …, array given 主义(Symfony3)可捕获的致命错误:传递给(bundle)的参数1必须是(bundle)的实例,给定数组 - Doctrine (Symfony3) Catchable Fatal Error: Argument 1 passed to (bundle) must be an instance of (bundle), array given 可捕获的致命错误:传递给\\ Entity \\ Image :: setFile()的参数1必须是Symfony \\ Component \\ HttpFoundation \\ File \\ UploadedFile的实例, - Catchable Fatal Error: Argument 1 passed to \Entity\Image::setFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, Symfony2可捕获的致命错误:参数1传递给实体可捕获的致命错误:参数1传递给实体 - Symfony2 Catchable Fatal Error: Argument 1 passed to entity Catchable Fatal Error: Argument 1 passed to entity 可捕获的致命错误:传递给Doctrine \\ DBAL \\ Connection :: update()的参数3必须为数组类型,在symfony2 update中没有给出 - Catchable Fatal Error: Argument 3 passed to Doctrine\DBAL\Connection::update() must be of the type array, none given in symfony2 update Action PHP - 可捕获的致命错误:传递给Too的参数1必须是Foo的实例,Boo的实例给出了吗? - PHP - Catchable fatal error: Argument 1 passed to Too must be an instance of Foo, instance of Boo given? Symfony2:ContextErrorException:可捕获的致命错误:传递给[…] :: __ construct()的参数1必须实现接口[…]没有给出 - Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given Symfony 2可捕获的致命错误:传递给Sg \\ DatatablesBundle \\ Datatable \\ :: __ construct()的参数1必须是的实例 - Symfony 2 Catchable Fatal Error: Argument 1 passed to Sg\DatatablesBundle\Datatable\::__construct() must be an instance of 可捕获的致命错误:传递给AppBundle \\ Form \\ TagType :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityRepository的实例,未给出任何实例, - Catchable Fatal Error: Argument 1 passed to AppBundle\Form\TagType::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM