Próbuję zrobić dodawanie rekordu dla relacji wiele do wielu i dostaje błąd. Nie mam pojęcia jak to ugryźć. Ale po kolei:
Entity article:
<?php namespace Portal\CmsBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * Article * * @ORM\Table() * @ORM\Entity */ class Article { /** * @ORM\OneToMany(targetEntity="ArticleTag", mappedBy="article") */ protected $articleTags; public function __construct() { $this->articleTags = new ArrayCollection(); } /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Add articleTags * * @param \Portal\CmsBundle\Entity\ArticleTag $articleTags * @return Article */ public function addArticleTag(\Portal\CmsBundle\Entity\ArticleTag $articleTags) { $this->articleTags[] = $articleTags; return $this; } /** * Remove articleTags * * @param \Portal\CmsBundle\Entity\ArticleTag $articleTags */ public function removeArticleTag(\Portal\CmsBundle\Entity\ArticleTag $articleTags) { $this->articleTags->removeElement($articleTags); } /** * Get articleTags * * @return \Doctrine\Common\Collections\Collection */ public function getArticleTags() { return $this->articleTags; } }
Entity tag:
<?php namespace Portal\CmsBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * Tag * * @ORM\Table() * @ORM\Entity */ class Tag { /** * @ORM\OneToMany(targetEntity="ArticleTag", mappedBy="tag") */ protected $articleTags; public function __construct() { $this->articleTags = new ArrayCollection(); } /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Add articleTags * * @param \Portal\CmsBundle\Entity\ArticleTag $articleTags * @return Tag */ public function addArticleTag(\Portal\CmsBundle\Entity\ArticleTag $articleTags) { $this->articleTags[] = $articleTags; return $this; } /** * Remove articleTags * * @param \Portal\CmsBundle\Entity\ArticleTag $articleTags */ public function removeArticleTag(\Portal\CmsBundle\Entity\ArticleTag $articleTags) { $this->articleTags->removeElement($articleTags); } /** * Get articleTags * * @return \Doctrine\Common\Collections\Collection */ public function getArticleTags() { return $this->articleTags; } }
Entity ArticleTag:
<?php namespace Portal\CmsBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * ArticleTag * * @ORM\Table(name="article_tag") * @ORM\Entity */ class ArticleTag { /** * @ORM\ManyToOne(targetEntity="Article", inversedBy="articleTags") * @ORM\JoinColumn(name="article_id", referencedColumnName="id") * * @ORM\Column(name="article_id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ protected $article; /** * @ORM\ManyToOne(targetEntity="Tag", inversedBy="articleTags") * @ORM\JoinColumn(name="tag_id", referencedColumnName="id") * * @ORM\Column(name="tag_id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="NONE") */ protected $tag; public function __construct($article, $tag) { $this->article = $article; $this->tag = $tag; } /** * Set article * * @param \Portal\CmsBundle\Entity\Article $article * @return ArticleTag */ public function setArticle(\Portal\CmsBundle\Entity\Article $article = null) { $this->article = $article; return $this; } /** * Get article * * @return \Portal\CmsBundle\Entity\Article */ public function getArticle() { return $this->article; } /** * Set tag * * @param \Portal\CmsBundle\Entity\Tag $tag * @return ArticleTag */ public function setTag(\Portal\CmsBundle\Entity\Tag $tag = null) { $this->tag = $tag; return $this; } /** * Get tag * * @return \Portal\CmsBundle\Entity\Tag */ public function getTag() { return $this->tag; } }
Akcja w kontrolerze do dodania tagu:
<?php namespace Portal\CmsBundle\Controller; use Portal\CmsBundle\Entity\Tag; use Portal\CmsBundle\Entity\ArticleTag; use Symfony\Component\HttpFoundation\Request; use Doctrine\ORM\EntityRepository; class AdminTagsController extends AbstractController { public function addAction(Request $request) { if ($request->getMethod() == 'POST') { $tag = new Tag(); $em = $this->getDoctrine()->getManager(); $formArray = $request->request->get('formTag'); $repository = $em->getRepository('PortalCmsBundle:Article'); $validator = $this->get('validator'); $errors = $validator->validate($tag); foreach ($errors as $error) { $this->message .= $error->getPropertyPath().' <-- '.$error->getMessage().' '; } } else { $em->persist($tag); $em->flush(); foreach ($formArray['articles'] as $article_id) { $article = $repository->find((int)$article_id); $article_tag = new ArticleTag($article, $tag); $em->persist($article_tag); $em->flush(); } $this->message = 'Tag dodany'; } return $this->redirect($this->generateUrl( 'admin_tags', 'message' => $this->message, 'messageType' => $this->messageType ) )); } return $this->redirect($this->generateUrl( 'admin_tags' )); } }
Przy próbie dodania tagu wyrzuca mi taki ekran:
Kod
Whoops, looks like something went wrong.
1/1ContextErrorException: Catchable Fatal Error: Object of class Portal\CmsBundle\Entity\Article could not be converted to string in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1359
in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1359
at ErrorHandler->handle('4096', 'Object of class Portal\CmsBundle\Entity\Article could not be converted to string', 'C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php', '1359', array('entity' => object(ArticleTag), 'classMetadata' => object(ClassMetadata)))
at implode(' ', array('article' => object(Article), 'tag' => object(Tag))) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1359
at UnitOfWork->addToIdentityMap(object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1172
at UnitOfWork->scheduleForInsert(object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 864
at UnitOfWork->persistNew(object(ClassMetadata), object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1627
at UnitOfWork->doPersist(object(ArticleTag), array('000000002119541b000000007e966e3c' => object(ArticleTag))) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1583
at UnitOfWork->persist(object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php line 624
at EntityManager->persist(object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\src\Portal\CmsBundle\Controller\AdminTagsController.php line 128
at AdminTagsController->addAction(object(Request))
at call_user_func_array(array(object(AdminTagsController), 'addAction'), array(object(Request))) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\app\bootstrap.php.cache line 2891
at HttpKernel->handleRaw(object(Request), '1') in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\app\bootstrap.php.cache line 2865
at HttpKernel->handle(object(Request), '1', true) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\app\bootstrap.php.cache line 2994
at ContainerAwareHttpKernel->handle(object(Request), '1', true) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\app\bootstrap.php.cache line 2274
at Kernel->handle(object(Request)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\web\app_dev.php line 28
1/1ContextErrorException: Catchable Fatal Error: Object of class Portal\CmsBundle\Entity\Article could not be converted to string in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1359
in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1359
at ErrorHandler->handle('4096', 'Object of class Portal\CmsBundle\Entity\Article could not be converted to string', 'C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php', '1359', array('entity' => object(ArticleTag), 'classMetadata' => object(ClassMetadata)))
at implode(' ', array('article' => object(Article), 'tag' => object(Tag))) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1359
at UnitOfWork->addToIdentityMap(object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1172
at UnitOfWork->scheduleForInsert(object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 864
at UnitOfWork->persistNew(object(ClassMetadata), object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1627
at UnitOfWork->doPersist(object(ArticleTag), array('000000002119541b000000007e966e3c' => object(ArticleTag))) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1583
at UnitOfWork->persist(object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php line 624
at EntityManager->persist(object(ArticleTag)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\src\Portal\CmsBundle\Controller\AdminTagsController.php line 128
at AdminTagsController->addAction(object(Request))
at call_user_func_array(array(object(AdminTagsController), 'addAction'), array(object(Request))) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\app\bootstrap.php.cache line 2891
at HttpKernel->handleRaw(object(Request), '1') in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\app\bootstrap.php.cache line 2865
at HttpKernel->handle(object(Request), '1', true) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\app\bootstrap.php.cache line 2994
at ContainerAwareHttpKernel->handle(object(Request), '1', true) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\app\bootstrap.php.cache line 2274
at Kernel->handle(object(Request)) in C:\BitNami\wappstack-5.4.22-0\apache2\htdocs\cms\web\app_dev.php line 28
Wywala się na linii:
$em->persist($article_tag);
Dumpowałem obiekt $article_tag oraz obiekty $article i $tag, są w porządku.
Ktoś ma jakiś pomysł? Jeśli potrzebujecie więcej informacji z kodu to mówcie co.