1

我尝试在 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 中调用。 dt/PyramidBundle/Controller/UserDigitalPrController.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;
}
4

2 回答 2

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

希望这有帮助

于 2015-11-09T14:04:14.530 回答
0

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

但是,如果您只需要实体来设置该值,您也可以传递对实体的引用

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

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

于 2015-11-09T14:55:06.080 回答