La Doctrine rejoindre erreur “Undefined index: produit en ObjectHydrator.php” (Symfony2)
Je suis en train d'apprendre Symfony2 et actuellement au "8: les Relations d'entités/Associations (Adhésion à des documents Connexes)" du "Livre". Juste codé avec les exemples (triple vérifié mon code), mais ensuite j'ai eu cette erreur:
Notice: Undefined index: produit en C:\My\Path\vendor\doctrine\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php la ligne 95
C'est la mon code:
//src\Acme\StoreBundle\Repository\ProductRepository.php
public function findOneByIdJoinedToCategory($id)
{
$query = $this->getEntityManager()
->createQuery('SELECT p, c
FROM AcmeStoreBundle:Product p
JOIN p.category c
WHERE p.id = :id')
->setParameter('id', $id);
try {
return $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
//src\Acme\StoreBundle\Controller\DefaultController.php
public function showAction($id)
{
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product')
->findOneByIdJoinedToCategory($id);
if (!$product) {
throw $this->createNotFoundException('No product found for id: '.$id);
}
$category = $product->getCategory();
return $this->render('AcmeStoreBundle:Default:product.html.twig', array(
'product' => $product,
'category' => $category)
);
}
Si j'utilise ce code, tout fonctionne comme il se doit:
//src\Acme\StoreBundle\Controller\DefaultController.php
public function showAction($id)
{
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product')
->find($id);
if (!$product) {
throw $this->createNotFoundException('No product found for id: '.$id);
}
$category = $product->getCategory();
return $this->render('AcmeStoreBundle:Default:product.html.twig', array(
'product' => $product,
'category' => $category)
);
}
Des idées?
Edit:
L'Entité De Produit:
<?php
namespace Acme\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\Column(type="decimal", scale=2)
*/
protected $price;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\Column(type="text", nullable="true")
*/
protected $extras;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="product")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set price
*
* @param decimal $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* Get price
*
* @return decimal
*/
public function getPrice()
{
return $this->price;
}
/**
* Set description
*
* @param text $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Get description
*
* @return text
*/
public function getDescription()
{
return $this->description;
}
/**
* Set extras
*
* @param text $extras
*/
public function setExtras($extras)
{
$this->extras = $extras;
}
/**
* Get extras
*
* @return text
*/
public function getExtras()
{
return $this->extras;
}
/**
* Set category
*
* @param Acme\StoreBundle\Entity\Category $category
*/
public function setCategory(\Acme\StoreBundle\Entity\Category $category)
{
$this->category = $category;
}
/**
* Get category
*
* @return Acme\StoreBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
Entité Category:
<?php
namespace Acme\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Acme\StoreBundle\Entity\Category
*
* @ORM\Table()
* @ORM\Entity
*/
class Category
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
protected $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add products
*
* @param Acme\StoreBundle\Entity\Product $products
*/
public function addProduct(\Acme\StoreBundle\Entity\Product $products)
{
$this->products[] = $products;
}
/**
* Get products
*
* @return Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
Vous pouvez poster votre produit Entité?
Granados ajouté.
Vous pouvez aussi afficher la catégorie de l'entité?
Bien sûr, donnez-moi une minute
Granados ajouté.
Vous pouvez aussi afficher la catégorie de l'entité?
Bien sûr, donnez-moi une minute
OriginalL'auteur Mats Rietdijk | 2012-08-08
Vous devez vous connecter pour publier un commentaire.
OK. Votre problème est que vous dites dans l'entité de Produit que la catégorie de la relation est inversée par "produit" et doit être "produits". Il suffit de changer à
et cela devrait fonctionner
OriginalL'auteur Carlos Granados