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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题