在非对象上调用成员函数getRepository()

时间:2015-12-29 11:45:56

标签: mongodb symfony

我是新手在使用mongoDB的symfony2中,我正在使用ODM创建一个与文档关系的关系。我在创建表单类型时面临上述问题,我的文件代码如下

文件路径:/src/AppBundle/Form/Type/ProductFormType.php

<?php
namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ProductFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text', array('label' => 'Product Name'))
                ->add('description', 'textarea',array('label' => 'Description'))
                ->add('Category', 'document', array(
                  'label'=>'Product Category',
                 'class' => 'AppBundle:Category',
                  'property' => 'name',
               ))

            ->add('save', 'submit');
    }
     public function getName()
    {
        return 'product';
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Document\Product'
        ));
    }

}

&GT;

文件:src / AppBundle / Document / product.php

<?php

namespace AppBundle\Document;


use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Validator\Constraints as Assert;

/** @ODM\Document */
class Product
{
   /** @ODM\Id */
   private $id;

   /** @ODM\String */

    protected $name;

   /** @ODM\String */

    protected $description;

    /** @ODM\ReferenceOne(targetDocument="Category", cascade="all") */
    private $Category;

    public function getId() { return $this->id; }

    public function getName() { return $this->name; }
    public function setName($name) { $this->name = $name; }


    /**
     * Set category
     *
     * @param AppBundle\Document\Category $category
     * @return self
     */
    public function setCategory(\AppBundle\Document\Category $category)
    {
        $this->Category = $category;
        return $this;
    }

    /**
     * Get category
     *
     * @return AppBundle\Document\Category $category
     */
    public function getCategory()
    {
        return $this->Category;
    }

    public function getDescription() { return $this->description; }
    public function setDescription($description) { $this->description = $description; }


}

文件src / Appbundle / Docment / Category.php

<?php
namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

//** @ODM\Document */
class Category
{
    /** @ODM\Id */
    protected $id;

     /** @ODM\String */

    protected $cat_id;

   /** @ODM\String */

   protected $categoryname;

   /** @ReferenceMany(targetDocument="Product" mappedBy="category") */
    private $products;


    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    /**
     * Add product
     *
     * @param \AppBundle\Document\Product $product
     *
     * @return Category
     */
    public function addProduct(\AppBundle\Document\Product $product)
    {
        $this->products[] = $product;

        return $this;
    }

    /**
     * Remove product
     *
     * @param \AppBundle\Document\Product $product
     */
    public function removeProduct(\AppBundle\Document\Product $product)
    {
        $this->products->removeElement($product);
    }

    /**
     * Get products
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getProducts()
    {
        return $this->products;
    }


    public function getId()
    {
        return $this->id;
    }

    public function getCat_id() { return $this->cat_id; }
    public function setCat_id($cat_id) { $this->cat_id = $cat_id; }

    public function getDescription() { return $this->categoryname; }
    public function setDescription($categoryname) { $this->categoryname = $categoryname; }


}

我也试过以下过程:

MongoDB的getter-setter php app / console doctrine:mongodb:generate:documents AppBundle

然后清除缓存并设置开发人员环境

任何人都可以有解决方案,请帮助我。

先谢谢

0 个答案:

没有答案