0

我正在尝试使用 MongoDB。但我有一些问题。

# src/Controller/DefaultController.php
public function editCategoryAction(Category $category) {
    echo $category->getName();
    die();
}

对于“实体”,这是可以的,但对于 MongoDB 则不行。

它显示此错误消息:

控制器 "Site\StoreBundle\Controller\DefaultController::editCategoryAction()" 要求您为 "$category" 参数提供一个值(因为没有默认值或者因为在此参数之后有一个非可选参数)。

我的分类文件:

namespace Site\StoreBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

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

    /**
     * @MongoDB\String
     */
    protected $name;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Product")
     */
    private $products = array();

    public function __contructor() {
        $this->products = new \Doctrine\Common\Collections\ArrayCollection();   
    }

    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return self
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    public function getProducts() {
        return $this->products;
    }
    public function __construct()
    {
        $this->products = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

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

我的路线

editCategory:
    path:  /editCategory/{id}
    defaults: { _controller: SiteStoreBundle:Default:editCategory }

我该如何解决这个问题?

编辑:我发现这篇文章: http: //php-and-symfony.matthiasnoback.nl/2012/10/symfony2-mongodb-odm-adding-the-missing-paramconverter/

我将src/Site/StoreBundle/Resources/config/services.yml更改为:参数:# site_store.example.class: Site\StoreBundle\Example

services:
    doctrine_mongo_db_param_converter:
        class: Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter
        arguments: ['@doctrine_mongodb']

但同样的错误:/

4

1 回答 1

1

我认为您错过了标记您的服务:

services:
    doctrine_mongo_db_param_converter:
        class: Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter
        arguments: ['@doctrine_mongodb']
        tags:
            - { name: request.param_converter, priority: -2, converter: doctrine_mongo_db_param_converter}
于 2013-10-14T20:35:55.687 回答