2016-04-16 5 views
0

多対1の関係を設定し、そのカテゴリにリンクされた新しい商品オブジェクトを追加したい。これに関連して、私は2つの質問があります:symfonyは新しいオブジェクトを多対1エラーで呼び出す定義されていないメソッドを呼び出す

カテゴリオブジェクトを新しい製品に保存することができません。さまざまなオプションを試し、ここで関連する質問を読んでください。この時点で私はエラーを受け取ります: "AppBundle \ Controller \ ProductController"クラスの "getCategory"という未定義のメソッドを呼び出そうとしました。 ProductクラスにgetCategoryメソッドがあります。 私はここで何が欠けていますか?

私が知りたいと思うもう一つのことは、そのカテゴリの関連商品を取得するためにURLに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; 
    } 

"/カテゴリは" 私はproductlistにカテゴリをリンクする "/ CAT1 /製品"

私はカテゴリのクラスを持っています(< - カテゴリ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

あなたは:

$category = $this->getCategory(); 

$これはあなたのproductControllerを表し、それは未定義のメソッドエラーの理由です。カテゴリオブジェクトを取得するには、あなたがする必要があります:

 $categoryRepository = $this->getDoctrine()->getRepository('AppBundle:Category'); 
    $product->setCategory($categoryRepository->find($cat_id)); 

あなたを助けるかもしれない希望。

+0

これは実際に役立ちます。私は多分1対多のリンクのために、製品エンティティのゲッター「カテゴリを取得する」が私のためにこれを行うと予想していました。しかし、私が今理解しているように、これはそうではありません。私が知りたいもう一つのことは、そのカテゴリの関連商品を取得するためにURLのcategory-idを渡す必要があることです。 – zef

+0

あなたは、すべてのカテゴリーで水和した選択を製品フォームに加えることができます。あなたはここで良い方法を見ることができます[リンク](http://symfony.com/doc/current/reference/forms/types/entity.html) –

+0

私はすでに新しい製品を追加する前にカテゴリを選択したので、私は選択したくありません再びカテゴリ。カテゴリコントローラからカテゴリコントローラにカテゴリIDを渡す(簡単な)方法を探していますが、URLを渡す必要はありません。 – zef

関連する問題