gpt4 book ai didi

php - [Symfony2]Catchable Fatal Error : Object of class [. ..] 无法转换为字符串

转载 作者:行者123 更新时间:2023-12-02 07:29:33 24 4
gpt4 key购买 nike

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

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

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

ContextErrorException: Catchable Fatal Error: Object of class Sam\VitrineBundle\Entity\Categorie could not be converted to string in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Entity\Produit.php line 124

我的 Controller ,我有错误的代码。

...
$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

最佳答案

在你的实体中

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

代替

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

关于php - [Symfony2]Catchable Fatal Error : Object of class [. ..] 无法转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com