gpt4 book ai didi

php - Symfony 2 Embedded Forms: Catchable Fatal Error: Argument 1 passed to Entity::addProperty must be an instance of XX\MyClass, 数组给定

转载 作者:可可西里 更新时间:2023-11-01 00:09:24 25 4
gpt4 key购买 nike

我已经读过 thisthis和其他人,但没有人解决我的问题。

我已经删除了所有可能的内容并将范围缩小到一个字段:地址。当我尝试关注 this tutorial 时,如果我遵循它,一切都会正常,从一个全新的新项目开始。但是当我在我的其他项目中手动执行时,我得到了这个错误:“可捕获的 fatal error :传递给 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 行。

这是堆栈跟踪: stacktracke

这是我的代码,我在其中删除了有效的属性:


我的 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(),
));
}
}

我的实体 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();
}
}

提示

注意:我已经检查过表单是否正确发送,信息的格式是否正确,并且我已经使用 xdebug 进行了逐步调试,但它对我来说太复杂了(目前)看不出我遗漏了什么.

这是 'collection' 类型的问题:如果我删除 'by_reference' => false,那么它会起作用....只有当我不坚持时

如果我尝试用这段代码坚持下去:

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

然后我得到这个错误:

警告:spl_object_hash() 期望参数 1 为对象,在 (projectpath)\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1572 中给出的数组

为什么?

最佳答案

我找到了。

问题出在我的 FormType AddressType.php 文件中:缺少函数 setDefaultOptions()。一切都很好,但是如果您希望 doctrine 能够将传入字段转换为特定类,则此函数 必须

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

我对自己因此而失去的时间感到非常生气。您必须牢记整个 symfony 2 过程才能快速解决这类问题,否则就像我一样需要 6 个小时。 Symfony 2 本身就是一个整体leaky abstraction : 你想争取时间,但你没有。 Symfony 旨在简化您的生活,但最后,引用 Joël Spolsky 的话:

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.

关于php - Symfony 2 Embedded Forms: Catchable Fatal Error: Argument 1 passed to Entity::addProperty must be an instance of XX\MyClass, 数组给定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19690188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com