我尝试在db上写,但是当我编写这段代码时收到此错误:
public function createAction(Request $request)
{
$id_user = $request->get('id_user');
var_dump($id_user);
$prova = intval($id_user);
$entity = new UserDigitalPr();
$em = $this->getDoctrine()->getManager();
$entity->setIdUser($prova);
$em->persist($entity);
$em->flush();
return $this->render('DtPyramidBundle:UserDigitalPr:new.html.twig', array(
'entity' => $entity,
));
}
这是错误: 捕获致命错误:传递给Dt \ PyramidBundle \ Entity \ UserDigitalPr :: setIdUser()的参数1必须是Dt \ EcBundle \ Entity \ User的实例,给定整数,在/var/www/it.virtuego中调用。第70行的com / src / Dt / PyramidBundle / Controller / UserDigitalPrController.php并定义了
在我的实体中我有这个:
class UserDigitalPr
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @var integer
* @ORM\OneToOne(targetEntity="Dt\EcBundle\Entity\User", inversedBy="digitalpr")
* @ORM\JoinColumn(name="id_user", referencedColumnName="id")
*/
private $id_user;
/**
*
* @var integer
* @ORM\OneToOne(targetEntity="Dt\EcBundle\Entity\User", inversedBy="imtheboss")
* @ORM\JoinColumn(name="become_from", referencedColumnName="id")
*/
private $become_from;
/**
* Set id_user
*
* @param \Dt\EcBundle\Entity\User $idUser
* @return UserDigitalPr
*/
public function setIdUser(\Dt\EcBundle\Entity\User $idUser = null)
{
$this->id_user = $idUser;
return $this;
}
/**
* Get id_user
*
* @return \Dt\EcBundle\Entity\User
*/
public function getIdUser()
{
return $this->id_user;
}
/**
* Set become_from
*
* @param \Dt\EcBundle\Entity\User $becomeFrom
* @return UserDigitalPr
*/
public function setBecomeFrom(\Dt\EcBundle\Entity\User $becomeFrom = null)
{
$this->become_from = $becomeFrom;
return $this;
}
/**
* Get become_from
*
* @return \Dt\EcBundle\Entity\User
*/
public function getBecomeFrom()
{
return $this->become_from;
}
}
在用户实体中我有:
/* ######################################################################## */
/* ############# Tabella user_digital_pr ############ */
/* ######################################################################## */
/**
* @ORM\OneToOne(targetEntity="Dt\PyramidBundle\Entity\UserDigitalPr", mappedBy="id_user")
* */
private $digitalpr;
/**
* L'utente che ha promosso un'altro utente a digital pr
* @ORM\OneToOne(targetEntity="Dt\PyramidBundle\Entity\UserDigitalPr", mappedBy="become_from")
*/
private $imtheboss;
这样的getter / setter方法:
/**
* Set digitalpr
*
* @param \Dt\PyramidBundle\Entity\UserDigitalPr $digitalpr
* @return User
*/
public function setDigitalpr(\Dt\PyramidBundle\Entity\UserDigitalPr $digitalpr = null) {
$this->digitalpr = $digitalpr;
return $this;
}
/**
* Get digitalpr
*
* @return \Dt\PyramidBundle\Entity\UserDigitalPr
*/
public function getDigitalpr() {
return $this->digitalpr;
}
/**
* Set imtheboss
*
* @param \Dt\PyramidBundle\Entity\UserDigitalPr $imtheboss
* @return User
*/
public function setImtheboss(\Dt\PyramidBundle\Entity\UserDigitalPr $imtheboss = null) {
$this->imtheboss = $imtheboss;
return $this;
}
/**
* Get imtheboss
*
* @return \Dt\PyramidBundle\Entity\UserDigitalPr
*/
public function getImtheboss() {
return $this->imtheboss;
}
答案 0 :(得分:0)
您需要设置实体对象实例,以便找到它并设置如下:
// your entity namespace
$userObj = $em->getRepository("DtPyramidBundle:User")->find($id_user);
$entity = new UserDigitalPr();
$em = $this->getDoctrine()->getManager();
$entity->setIdUser($userObj);
$em->persist($entity);
$em->flush();
希望这个帮助
答案 1 :(得分:0)
您无法将数值传递给setIdUser
,您必须传递实体引用。这可以通过首先检索实体然后将其设置为参数来完成。
但是如果您只需要实体来设置该值,您也可以将引用传递给实体:
$em = $this->getDoctrine()->getManager();
$entity->setIdUser($em->getReference($user_id));
$em->persist($entity);
$em->flush();
顺便说一下,相应的实体字段不应该命名为$id_user
,因为它不包含用户的ID(从代码的角度来看),而是实际的用户对象。这就是使用ORM的重点。