我正在尝试使用MongoDB。但是我遇到了一些问题。
# src/Controller/DefaultController.php
public function editCategoryAction(Category $category) {
echo $category->getName();
die();
}
使用“entity”可以,但不适用于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']
但同样的错误:/
答案 0 :(得分: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}