Symfony创建新对象多对一错误调用未定义方法

时间:2016-04-16 12:46:14

标签: doctrine-orm symfony many-to-one

我已设置多对一关系,并希望添加与其类别相关联的新产品对象。与此相关我有两个问题:

我无法将类别对象保存到新产品中。尝试了不同的选项,并在此处阅读相关问题。此刻我收到错误: 试图调用名为" getCategory"的未定义方法。 class" AppBundle \ Controller \ ProductController"。 我的Product类中有getCategory方法。 我在这里缺少什么?

我想知道的另一件事是,我是否需要在网址中传递category-id以获取该类别的相关产品?

我有一个类别类:

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(); 
}

和产品类:

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"我将类别链接到产品列表" / cat1 / product" (< - 我需要在这里传递category-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,
            ));
    }

建议赞赏!

1 个答案:

答案 0 :(得分:1)

当你这样做时:

    $category = $this->getCategory();

$ this代表你的productController,这是未定义方法错误的原因。获取必须执行的类别对象:

     $categoryRepository = $this->getDoctrine()->getRepository('AppBundle:Category');
     $product->setCategory($categoryRepository->find($cat_id));
希望这对你有帮助。