duanduanxi9441 2015-11-09 13:57
浏览 56
已采纳

Symfony2 Catchable Fatal Error:set方法

I try to write on db but i receive this error when i write this code:

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

This is the error: Catchable Fatal Error: Argument 1 passed to Dt\PyramidBundle\Entity\UserDigitalPr::setIdUser() must be an instance of Dt\EcBundle\Entity\User, integer given, called in /var/www/it.virtuego.com/src/Dt/PyramidBundle/Controller/UserDigitalPrController.php on line 70 and defined

IN my entity i have this:

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

In User Entity i have this :

/* ######################################################################## */
/* #############          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;

And getter / setter method like this:

  /**
 * 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;
}
  • 写回答

2条回答 默认 最新

  • doulan1866 2015-11-09 14:04
    关注

    You need to set an entity object instance, so you can find it and set as follow:

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

    Hope this help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程