gpt4 book ai didi

symfony - Doctrine 连接错误 "Undefined index: product in ObjectHydrator.php"(Symfony2)

转载 作者:行者123 更新时间:2023-12-04 21:53:27 24 4
gpt4 key购买 nike

我正在尝试学习 Symfony2,目前正在“The Book”的“8:实体关系/关联(加入相关记录)”中。刚刚与示例一起编码(三重检查了我的代码)但后来我收到了这个错误:

Notice: Undefined index: product in C:\My\Path\vendor\doctrine\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php line 95



这是我的代码:
//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)
);
}

如果我使用此代码,则一切正常:
//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)
);
}

有任何想法吗?

编辑:
产品实体:
<?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;
}
}

类别实体:
<?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;
}
}

最佳答案

好的。您的问题是您在 Product 实体中说类别关系与“product”相反,它应该是“products”。只需将其更改为

/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;

它应该工作

关于symfony - Doctrine 连接错误 "Undefined index: product in ObjectHydrator.php"(Symfony2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11865004/

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