我想编辑我的Clan,当我将Co Leader设置为null时,会出现错误,但其余部分会有效。
“类型为”UserBundle \ Entity \ User“的预期参数,”NULL“给出”
我希望有人可以帮助我。
Clan.php
/**
* @var User
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\OneToOne(targetEntity="UserBundle\Entity\User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="coLeader", referencedColumnName="id", onDelete="SET NULL")
* })
*
*
*/
private $coleader = null;
// @SamDufel的解决方案(在$ coleader追加“= null”之后)
/**
* Set coleader
*
* @param User $coleader
* @return Clan
*/
public function setColeader(User $coleader = null) // <--
{
$this->coleader = $coleader;
return $this;
}
ClanType.php
->add('coleader', null, array(
'label' => 'clan.coleader',
'required' => false,
'empty_data' => null,
'placeholder' => 'form.nobody',
));
ClanController.php
/**
* Displays a form to edit an existing Clan entity.
*
* @Route("/edit/{id}", name="clan_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Clan $clan)
{
$current_leader = $clan->getLeader();
$current_coleader = $clan->getColeader();
$editForm = $this->createForm('ClanBundle\Form\ClanType', $clan);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('UserBundle:User');
$current_leader = $user->find($current_leader);
$current_coleader = $current_coleader != '' ? $user->find($current_coleader) : '';
$leader = $editForm['leader']->getData();
$coleader = $editForm['coleader']->getData();
if($leader != $current_leader ){
$current_leader->removeRole('ROLE_LEADER');
$user_leader = $em->getRepository('UserBundle:User')->find($leader);
$user_leader->addRole('ROLE_LEADER');
$user_leader->setClanId($clan);
}
if(!empty($coleader) || $coleader != $current_coleader){
$current_coleader ? $current_coleader->removeRole('ROLE_COLEADER') : '';
$user_co = $em->getRepository('UserBundle:User')->find($coleader);
$user_co->addRole('ROLE_COLEADER');
$user_co->setClanId($clan);
}
if(empty($coleader)){
$current_coleader ? $current_coleader->removeRole('ROLE_COLEADER') : '';
}
$em->persist($clan);
$em->flush();
return $this->redirectToRoute('clan_edit', array('id' => $clan->getId()));
}
return $this->render(
'ClanBundle:clan:form.html.twig',
array(
'clan' => $clan,
'form' => $editForm->createView(),
'status' => 'edit'
)
);
}
User.php是FOSUserBundle的默认值,除了我的User.php中的3个实体
//...
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\OneToOne(targetEntity="Clan", mappedBy="leader")
* @ORM\OneToOne(targetEntity="Clan", mappedBy="coLeader")
*/
protected $id;
public function __construct()
{
parent::__construct();
$this->roles = array('ROLE_USER');
$this->createdAt = new \DateTime();
}
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
protected $createdAt;
/**
* @var integer
*
* @ORM\ManyToOne(targetEntity="ClanBundle\Entity\Clan")
* @ORM\JoinColumn(referencedColumnName="id", onDelete="SET NULL")
*/
protected $clanId;
//...
答案 0 :(得分:0)
有现代的方式: https://symfonycasts.com/screencast/php7/nullable-types
您可以使用'?'在参数类型之前:
public function setFunFact(?string $funFact): void
{
}