我已经删除了所有可能的内容并缩小到一个字段:地址.当我尝试按照本教程学习时,如果我遵循它,从一个全新的项目开始,一切正常.但是当我在我的其他项目中手动完成时,我收到此错误:"可捕获的致命错误:传递给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行中给出的数组.
继承人堆栈跟踪:

这是我的代码,我删除了有效的属性:
我的控制器:
<?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(),
));
}
}
Run Code Online (Sandbox Code Playgroud)
我的实体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();
}
}
Run Code Online (Sandbox Code Playgroud)
提示
注意:我已经检查过表单是否正确发送,信息格式是否正确,我是否已经使用xdebug逐步调试,但是对于我(还)看到我缺少的内容来说太复杂了.
这是一个'collection'类型的问题:如果我删除'by_reference' => false,那么它的工作....只有我不坚持它.
如果我尝试使用此代码保留它:
$form->handleRequest($request);
if ($form->isValid()) {
$person=$form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($person);
$em->flush();
}
Run Code Online (Sandbox Code Playgroud)
然后我收到这个错误:
警告:spl_object_hash()期望参数1是对象,在(projectpath)\ vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php第1572行中给出的数组
为什么?
我找到了.
问题出现在我的FormType AddressType.php文件中:函数setDefaultOptions()丢失了.一切都还可以,但是如果你希望教义能够将传入的字段转换为特定的类,那么这个函数是强制性的.
public function setDefaultOptions(
\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
) {
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Adresse',
));
}
Run Code Online (Sandbox Code Playgroud)
我对此失去的时间感到非常生气.你必须全心全意地了解整个symfony 2过程,以便快速解决这类问题,或者它需要花费6个小时才能完成.Symfony 2本身就是一个完全漏洞的抽象:你觉得自己有时间,但是你没有.Symfony应该简化你的生活,但最后引用JoëlSpolsky:
这是一个漏洞的抽象:它意味着抽象并没有像他们想要的那样真正简化我们的生活.代码生成工具假装抽象出一些东西,比如所有抽象,泄漏,以及处理泄漏的唯一方法是了解抽象如何工作以及抽象的内容.