Symfony2-服务/控制器-将控制器用作服务以显示来自实体的数据
在我的Symfony应用程序中,我尝试将控制器用作服务,以便在表中显示我需要的数据 这两个实体是Symfony2-服务/控制器-将控制器用作服务以显示来自实体的数据,symfony,service,doctrine-orm,controller,routes,Symfony,Service,Doctrine Orm,Controller,Routes,在我的Symfony应用程序中,我尝试将控制器用作服务,以便在表中显示我需要的数据 这两个实体是Categories.php和SubCategories.php。 1个类别可以有多个子类别,1个子类别只能属于一个类别。我有一个关系ManyToOne,FK在我的子类别实体中。因此,在我的Symfony/Doctrine项目中,我在SubCategories.php实体中有一个变量$category 这是我的小树枝视图,她将用于在表中搜索我的类别的所有数据。php实体: <tab
Categories.php
和SubCategories.php
。
1个类别可以有多个子类别,1个子类别只能属于一个类别。我有一个关系ManyToOne
,FK在我的子类别实体中。因此,在我的Symfony/Doctrine项目中,我在SubCategories.php
实体中有一个变量$category
这是我的小树枝视图,她将用于在表中搜索我的类别的所有数据。php
实体:
<table id="dataTablesCategories">
<thead>
<tr>
<th>reference</th>
<th>id</th>
<th>name</th>
<th>description</th>
<th>Sub Categories</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>reference</th>
<th>id</th>
<th>name</th>
<th>description</th>
<th>Sub Categories</th>
<th>Action</th>
</tr>
</tfoot>
<tbody>
{% for category in category %}
<tr>
<td>{{ category.reference }}</td>
<td>{{ category.id }}</td>
<td>{{ category.name}}</td>
<td>{{ category.description}}</td>
<td>
<a href="{{ path('indexSubCategories ', {'category': subCategories.category}) }}"><button class="btn btn-default btn-xs">Détails</button></a>
</td>
<td>
<a href="{{ path('editCategory ', {'name': category.name}) }}"><button class="btn btn-warning btn-xs">Modifier</button></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
因此,当我单击详细信息按钮时,它会显示在indexSubcategories中,即我单击详细信息按钮的类别行的所有子类别中
这是我的类别控制器:
public function indexCategoriesAction()
{
$em=$this->getDoctrine()->getManager();
$category = $em->getRepository('MySpaceMyBundle:Categories')->findAll();
return $this->render('MySpaceMyBundle:MyFolder:indexCategories.html.twig', array('category ' => $category ));
}
public function indexSubCategoriesAction ($category)
{
$em=$this->getDoctrine()->getManager();
$subcategory = $em->getRepository('MySpaceMyBundle:SubCategories')->findOneByCategory($category);
return $this->render('MySpaceMyBundle:MyFolder:indexSubCategories.html.twig', array('subcategory' => $subcategory));
}
这是我的子类别控制器:
public function indexCategoriesAction()
{
$em=$this->getDoctrine()->getManager();
$category = $em->getRepository('MySpaceMyBundle:Categories')->findAll();
return $this->render('MySpaceMyBundle:MyFolder:indexCategories.html.twig', array('category ' => $category ));
}
public function indexSubCategoriesAction ($category)
{
$em=$this->getDoctrine()->getManager();
$subcategory = $em->getRepository('MySpaceMyBundle:SubCategories')->findOneByCategory($category);
return $this->render('MySpaceMyBundle:MyFolder:indexSubCategories.html.twig', array('subcategory' => $subcategory));
}
在indexSubCategories.html.twig
中,我在一个表中做同样的事情,就像我为indexCategories.html.twig
做的一样简单
我想我需要使用SubCategories controller作为一项服务来实现它
这是我的路由文件:
# categories index #
indexCategories:
path: /managecategories
defaults: { _controller: MySpaceMyBundle:MyController:indexCategories }
requirements:
methods: GET
# Subcategories index #
indexCategories:
path: /managecategories/subcategories/{category}
defaults: { _controller: MySpaceMyBundle:MyController:indexSubCategories }
requirements:
methods: GET
我怎样才能真正开始呢
我已经看过了,但我如何才能真正提供我的服务呢
谢谢你的帮助
更新 我尝试将我的
子类别controller.php
用作服务。在my service.yml(同一文件夹包)中,以下是我的代码:
services:
subCategoriesDetail:
class: MyCompany\MyBundle\Controller\SubcategoriesController
我在CategoriesController.php中称之为:
public function indexCategoriesAction()
{
$em=$this->getDoctrine()->getManager();
$category = $em->getRepository('MySpaceMyBundle:Categories')->findAll();
$SubCategoriesController = $this->get('subCategoriesDetail');
return $this->render('MySpaceMyBundle:MyFolder:indexCategories.html.twig', array('categories' => $category ));
}
但我有一个错误,这是我第一次尝试将控制器用作服务:
试图从命名空间加载类“子类别”
中的“MyCompany\MyBundle\Controller”
C:\wamp\www\my\path\to\my\project\cache\dev\appDevDebugProjectContainer.php
第618行。是否需要从另一个命名空间“使用”它
你有打字错误吗
您正在从控制器数组('subcategory'=>$subcategory)
传递,但试图呈现{'category':subCategories.category}
此外,我建议将名称更改为细枝循环:
{% for category in category %}
因为在循环之后,category
将是一个元素,而不是原始集合
更新
您应该将控制器从
public function indexSubCategoriesAction ($category)
{
$em=$this->getDoctrine()->getManager();
$subcategory = $em->getRepository('MySpaceMyBundle:SubCategories')
->findOneByCategory($category);
return $this->render('MySpaceMyBundle:MyFolder:indexSubCategories.html.twig', array(
'subcategory' => $subcategory));
}
到
或者您应该从更改您的细枝模板
<td>
<a href="{{ path('indexSubCategories ', {'category': subCategories.category}) }}"><button class="btn btn-default btn-xs">Détails</button></a>
</td>
到
更新
通过与OP聊天,我了解到他只需要从a类子类别中检索。我告诉他,这可以通过调用视图中的直接方法(点表示法)轻松实现,因为对象彼此相关(ORM)只是为了我的个人信息,为什么要这样处理?我的意思是,为什么要将控制器定义为服务?@DonCallisto,事实上,我认为将控制器用作服务可以让我访问其中的所有方法。这样,我就可以从控制器中的另一个控制器调用方法。现在我有一个错误:变量子类别在细枝线中不存在…
发生错误的那一行是我有详细信息按钮的那一行,带有
是的,你是对的,但这是执行imho的唯一原因。只是检查一下:你建议我把名字改成细枝环。如果我更改为:{%for categories in categories%}
,它似乎更好?(当然,如果有必要,我会在所有文件中更改此选项)。打字错误?事实上,我对变量和细枝变量使用了相同的名称似乎是个问题,对吗?如何使用您的建议来解决我的问题:{'category':subCategories.category}
?谢谢您的帮助。@dopebeat:这个问题不是由引起的,当然,这只是一个额外的建议。问题似乎是由控制器或细枝模板中的输入错误引起的。请查看我的更新。@dopebeat,我想你应该按照建议去做,两个变量的名称相同,也许这对理解和理解代码更好。我按照你的建议去做,我将在我的代码中进行更改。谢谢你的帮助。当我的问题得到解决时,我会回到这里。我让你知道了这个问题。事实上,它不起作用。我遵循所有建议(更改变量名称…等),但我始终存在相同的错误:变量
子类别(或者如果我更改,子类别)在细枝线中不存在…
。如果我在Categories.php
中添加子类别的FK,并且关系为OneToMany
可能是一个解决方案,您认为如何?
<td>
<a href="{{ path('indexSubCategories ', {'category': subcategory.category}) }}"><button class="btn btn-default btn-xs">Détails</button></a>
</td>