Symfony2 Catchable Fatal Error: set method

Symfony2 Catchable Fatal Error: set method

我尝试在 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.[=27 中调用=].php 在第 70 行并定义了

在我的实体中我有这个:

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;
}

你需要设置一个实体对象实例,所以你可以找到它并设置如下:

    // 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();

希望对您有所帮助

您不能将数值传递给 setIdUser,您必须传递实体引用。这可以通过首先检索实体然后将其设置为参数来完成。

但如果您只需要设置该值的实体,您也可以将 reference 传递给该实体:

$em = $this->getDoctrine()->getManager();
$entity->setIdUser($em->getReference($user_id));
$em->persist($entity);
$em->flush();

顺便说一下,对应的实体字段不要命名为$id_user,因为它不包含用户的ID(从你的代码来看),而是实际的用户对象。这就是使用 ORM 的全部意义。