简体   繁体   English

Symfony2可捕获的致命错误:设置方法

[英]Symfony2 Catchable Fatal Error: set method

I try to write on db but i receive this error when i write this code: 我尝试在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,
    ));
}

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 这是错误: 可捕获的致命错误:传递给Dt \\ PyramidBundle \\ Entity \\ UserDigitalPr :: setIdUser()的参数1必须是Dt \\ EcBundle \\ Entity \\ User的实例,给定整数,在/ var / www / it中调用。在第70行上并已定义的usernamego.com/src/Dt/PyramidBundle/Controller/UserDigitalPrController.php

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

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 希望这个帮助

You cannot pass a numeric value to setIdUser , you must pass an entity reference. 您不能将数字值传递给setIdUser ,而必须传递实体引用。 This can be done by first retrieving the entity and then setting it as parameter. 这可以通过首先检索实体,然后将其设置为参数来完成。

But if you just need the entity for setting that value, you can as well pass a reference to the entity: 但是,如果只需要用于设置该值的实体,则还可以将引用传递给该实体:

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

By the way, the corresponding entity field should not be named $id_user , because it does not contain the ID of the user (from the perspective of your code), but the actual user object. 顺便说一句,不应将相应的实体字段命名为$id_user ,因为它不包含用户的ID(从代码的角度来看),而是实际的用户对象。 That's the whole point of using an ORM. 这就是使用ORM的全部重点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可捕获的致命错误:symfony2 - Catchable Fatal Error: symfony2 Symfony2可捕获的致命错误:参数1传递给实体可捕获的致命错误:参数1传递给实体 - Symfony2 Catchable Fatal Error: Argument 1 passed to entity Catchable Fatal Error: Argument 1 passed to entity 在Symfony2中上载文件:可捕获的致命错误:参数1传递给 - Upload file in Symfony2: Catchable Fatal Error: Argument 1 passed to symfony2 可捕获的致命错误:类的对象无法转换为字符串 - symfony2 Catchable Fatal Error: Object of class could not be converted to string Symfony2原则:ContextErrorException:可捕获的致命错误:DateTime类的对象无法转换为字符串 - Symfony2 Doctrine: ContextErrorException: Catchable Fatal Error: Object of class DateTime could not be converted to string Symfony2可捕获的致命错误:DateTime类的对象无法转换为字符串 - Symfony2 Catchable Fatal Error: Object of class DateTime could not be converted to string Symfony2:ContextErrorException:可捕获的致命错误:传递给[…] :: __ construct()的参数1必须实现接口[…]没有给出 - Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given symfony2 ContextErrorException:Catchable Fatal Error:类Proxies \\ __ CG __ \\ ... \\ Entity \\ ...的对象无法转换为字符串 - symfony2 ContextErrorException: Catchable Fatal Error: Object of class Proxies\__CG__\…\Entity\… could not be converted to string Symfony 2:如何捕获可捕获的致命错误? - Symfony 2: How to catch Catchable Fatal Error? Symfony2:细枝中的form_widget调用引发异常“可捕获的致命错误……必须是Symfony \\ Component \\ Form \\ FormView的实例” - Symfony2: form_widget call in twig throws exception “Catchable fatal error … must be an instance of Symfony\Component\Form\FormView”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM