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



我已经建立了一个多对一关系,并希望添加一个链接到其类别的新产品对象。与此相关,我有两个问题:

我陷入了将类别对象保存到新产品的困境。尝试了不同的选项并阅读了此处的相关问题。此时此刻,我得到了一个错误:尝试调用类"AppBundle\Controller\ProductController"的名为"getCategory"的未定义方法。我的Product类中确实有getCategory方法。我在这里错过了什么?

我想知道的另一件事是,我需要在url中传递类别id才能获得该类别的相关产品吗?

我有一个类别类:

namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
 * @ORMEntity
 * @ORMTable(name="category")
 */
class Category
{
    /**
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     * @ORMColumn(type="integer")
     */
    private $cat_id;
 ... 
/**
* @ORMOneToMany(targetEntity="Product", mappedBy="category")
*/
private $products; ...
public function __construct()
{
    $this->products = new ArrayCollection(); 
}

产品类别:

namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use AppBundleEntityCategory;
use DoctrineCommonCollectionsArrayCollection;
/**
 * @ORMEntity
 * @ORMTable(name="product")
 */
class Product
{
    /**
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     * @ORMColumn(type="integer")
     */
    private $prd_id;
    /**
     * @var Category
     * 
     * @ORMManyToOne(targetEntity="Category", inversedBy="products")
     * @ORMJoinColumn(name="cat_id", referencedColumnName="cat_id", nullable=false)
     */
    private $category;
    ....
    /**
     * Set category
     *
     * @param AppBundleEntityCategory $category
     *
     * @return Product
     */
    public function setCategory(AppBundleEntityCategory $category)
    {
        $this->category = $category;
        return $this;
    }
    /**
     * Get category
     *
     * @return AppBundleEntityCategory
     */
    public function getCategory()
    {
        return $this->category;
    }

从类别列表"/categies"中,我将类别链接到产品列表"/cat1/product"(<--我需要在此处传递类别id吗?)。在那里,我想添加一个新产品,并在我的ProductController中调用以下操作:

namespace AppBundleController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationResponse;
use AppBundleEntityProduct;
use AppBundleFormProductType;
use SymfonyComponentHttpFoundationRequest;
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));

希望这能对你有所帮助。

相关内容

  • 没有找到相关文章

最新更新