Symfony 创建新对象多对一错误调用未定义方法
Symfony create new object many to one error call undefined method
我已经建立了多对一关系,并想添加一个新的产品对象 linked 到它的类别中。与此相关,我有 2 个问题:
我无法将类别对象保存到新产品。尝试了不同的选项并在此处阅读相关问题。此刻我收到错误:
试图调用名为 "getCategory" of class "AppBundle\Controller\ProductController" 的未定义方法。
我的产品 class 中确实有方法 getCategory。
我在这里错过了什么?
我想知道的另一件事是我是否需要在 url 中传递类别 ID 以获得该类别的相关产品?
我有一个类别class:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $cat_id;
...
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
private $products; ...
public function __construct()
{
$this->products = new ArrayCollection();
}
和产品 class:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Category;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $prd_id;
/**
* @var Category
*
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="cat_id", referencedColumnName="cat_id", nullable=false)
*/
private $category;
....
/**
* Set category
*
* @param \AppBundle\Entity\Category $category
*
* @return Product
*/
public function setCategory(\AppBundle\Entity\Category $category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return \AppBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
从我的类别列表“/categories”我 link 类别到产品列表“/cat1/product”(<-- 我需要在这里传递类别 ID 吗?)。我想添加一个新产品并在我的 ProductController 中调用以下操作:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Product;
use AppBundle\Form\ProductType;
use Symfony\Component\HttpFoundation\Request;
class ProductController extends Controller
{
/**
* @Route("/cat{cat_id}/product/new", name="newproduct")
*/
public function newAction(Request $request, $cat_id)
{
$product = new Product();
$form = $this->createForm(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$category = $this->getCategory();
$product->setCategory($category);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return $this->redirectToRoute('productlist');
}
return $this->render('product/new.html.twig', array(
'form' => $form->createView(),
'cat_id' => $cat_id,
));
}
感谢建议!
当你这样做时:
$category = $this->getCategory();
$this代表你的productController,是未定义方法错误的原因。要获取类别对象,您必须执行以下操作:
$categoryRepository = $this->getDoctrine()->getRepository('AppBundle:Category');
$product->setCategory($categoryRepository->find($cat_id));
希望对您有所帮助。
我已经建立了多对一关系,并想添加一个新的产品对象 linked 到它的类别中。与此相关,我有 2 个问题:
我无法将类别对象保存到新产品。尝试了不同的选项并在此处阅读相关问题。此刻我收到错误: 试图调用名为 "getCategory" of class "AppBundle\Controller\ProductController" 的未定义方法。 我的产品 class 中确实有方法 getCategory。 我在这里错过了什么?
我想知道的另一件事是我是否需要在 url 中传递类别 ID 以获得该类别的相关产品?
我有一个类别class:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $cat_id;
...
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
private $products; ...
public function __construct()
{
$this->products = new ArrayCollection();
}
和产品 class:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Category;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $prd_id;
/**
* @var Category
*
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="cat_id", referencedColumnName="cat_id", nullable=false)
*/
private $category;
....
/**
* Set category
*
* @param \AppBundle\Entity\Category $category
*
* @return Product
*/
public function setCategory(\AppBundle\Entity\Category $category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return \AppBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
从我的类别列表“/categories”我 link 类别到产品列表“/cat1/product”(<-- 我需要在这里传递类别 ID 吗?)。我想添加一个新产品并在我的 ProductController 中调用以下操作:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Product;
use AppBundle\Form\ProductType;
use Symfony\Component\HttpFoundation\Request;
class ProductController extends Controller
{
/**
* @Route("/cat{cat_id}/product/new", name="newproduct")
*/
public function newAction(Request $request, $cat_id)
{
$product = new Product();
$form = $this->createForm(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$category = $this->getCategory();
$product->setCategory($category);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return $this->redirectToRoute('productlist');
}
return $this->render('product/new.html.twig', array(
'form' => $form->createView(),
'cat_id' => $cat_id,
));
}
感谢建议!
当你这样做时:
$category = $this->getCategory();
$this代表你的productController,是未定义方法错误的原因。要获取类别对象,您必须执行以下操作:
$categoryRepository = $this->getDoctrine()->getRepository('AppBundle:Category');
$product->setCategory($categoryRepository->find($cat_id));
希望对您有所帮助。