【问题标题】:[Symfony2]Catchable Fatal Error: Object of class [...] could not be converted to string[Symfony2] 可捕获的致命错误:类 [...] 的对象无法转换为字符串
【发布时间】:2014-04-29 15:31:20
【问题描述】:

我目前正在为我的在职培训开展一个项目。 但我遇到了一个无法解决的错误。

所以,我使用关系 ManyToOne 来连接 2 个事物,即类别中的产品。

但是当我想添加一个产品时,我遇到了这个错误:

ContextErrorException:可捕获的致命错误:Sam\VitrineBundle\Entity\Categorie 类的对象无法转换为 D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Entity\Produit 中的字符串.php 第 124 行

我的控制器,出现错误的代码。

...
$catRepo = $this->getDoctrine()->getManager()->getRepository('SamVitrineBundle:Categorie');
$cat = $catRepo->findOneById($form["categorie"]->getData());
$product -> setCategorie($cat);
...

我的实体,产品

...
    /**
     * @ORM\ManyToOne(targetEntity="Categorie", inversedBy="produits", cascade={"remove"})
     * @ORM\JoinColumn(name="categorie_id", referencedColumnName="id")
     */
    protected $categorie;

    /**
     * Set categorie
     *
     * @param Sam\VitrineBundle\Entity\Categorie $categorie
     */
    public function setCategorie(\Sam\VitrineBundle\Entity\Categorie $categorie)
    {
      $this->$categorie = $categorie;
    }

    /**
     * Get categorie
     *
     * @return Sam\VitrineBundle\Entity\Categorie 
     */
    public function getCategorie()
    {
      return $this->categorie;
    }
...

对不起,有些变量是法语的。 类别 -> 类别 产品 -> 产品

如果有人可以帮助我。 问我是否需要更多信息,但我会告诉你我认为错误来自的主要信息。

[1] Symfony\Component\Debug\Exception\DummyException: 
    at n/a
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\vendor\symfony\symfony\src\Symfony\Component\Debug\ErrorHandler.php line 174

    at Symfony\Component\Debug\ErrorHandler->handle('4096', 'Object of class Sam\VitrineBundle\Entity\Categorie could not be converted to string', 'D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Entity\Produit.php', '124', array('categorie' => object(Categorie)))
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Entity\Produit.php line 124

    at Sam\VitrineBundle\Entity\Produit->setCategorie(object(Categorie))
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Controller\VitrineController.php line 113

    at Sam\VitrineBundle\Controller\VitrineController->addProductAction()
        in  line 

    at call_user_func_array(array(object(VitrineController), 'addProductAction'), array())
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\app\bootstrap.php.cache line 2925

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\app\bootstrap.php.cache line 2897

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\app\bootstrap.php.cache line 3036

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\app\bootstrap.php.cache line 2304

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\web\app_dev.php line 28

【问题讨论】:

    标签: php symfony doctrine entity many-to-one


    【解决方案1】:

    在您的实体中

    public function setCategorie(\Sam\VitrineBundle\Entity\Categorie $categorie)
    {
        $this->categorie = $categorie;
    }
    

    而不是

    public function setCategorie(\Sam\VitrineBundle\Entity\Categorie $categorie)
    {
        $this->$categorie = $categorie;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-08
    • 2015-03-08
    • 2013-08-07
    • 2014-06-04
    • 2015-08-28
    • 2018-03-12
    • 2014-02-21
    • 1970-01-01
    相关资源
    最近更新 更多