我已经阅读this和this和其他的,但没有人解决了我的问题。Symfony 2嵌入式表单:可捕获的致命错误:传递给Entity :: addProperty的参数1必须是XX MyClass的实例,给出的数组为
我已经删除了所有可以缩小到一个字段的地址:地址。 当我尝试按照this tutorial时,如果我遵循它,从一个新的新项目开始,一切正常。但是,当我在我的其他项目中手动执行该操作时,出现此错误: “Catchable致命错误:传递给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(),
));
}
}
我的实体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
为什么?
如果你把'ManyToMany'改成'OneToMany'作为测试怎么办? – cheesemacfly
我试图改变另一个'ManyToMany'关系:课程(一位教师可能会给一个或多个课程,一个或多个教师可能会给一个课程)。有用。所以唯一的解释是有一些字段或一个声明丢失,但我真的不知道可能会丢失什么。 –