【问题标题】:Symfony2 - Service/Controller - using a controller as a service in order to display datas from entitiesSymfony2 - 服务/控制器 - 使用控制器作为服务来显示来自实体的数据
【发布时间】:2015-02-12 15:16:25
【问题描述】:

在我的 Symfony 应用程序中,我尝试将控制器用作服务,以便在表格中显示我需要的数据。

这两个实体是Categories.phpSubCategories.php1 个类别 可以有许多子类别,而1 个子类别 可以属于一个类别。我有一个关系ManyToOneFK 在我的SubCategories 实体 中。因此,在我的 Symfony/Doctrine 项目中,我的 SubCategories.php 实体中有一个变量 $category

这是我的树枝视图,她将用于在表格中搜索我的Categories.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>

如您所见,在我的子类别列中,我有一个 , 链接到子类别的索引。事实上,我正在尝试重定向到子类别索引来管理它们。但是如果我点击这个按钮 Details,我希望 indexSubCategories 显示与我点击的类别相关的所有子类别。

像这样:

    table for indexCategory
+-------------+-------------------------+
| name        | Sub Categories          |
+-------------+-------------------------+
| Category 1  |  Details button         |
|             |  href=indexSubcategory  |
|             |   for Category 1        |
+-------------+-------------------------+
| Category 2  |  Details button         |
|             |  href=indexSubcategory  |
|             |   for Category 2        |
+-------------+-------------------------+

因此,当我单击详细信息按钮时,它会显示在 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));
}

indexSubCategories.html.twig 中,我在表格中做同样的事情,就像我为indexCategories.html.twig 做的一样简单。

我认为我需要使用 SubCategories 控制器作为服务来实现它?

这是我的路线文件:

# 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

我该如何真正进行?

我已经查看了here,但我该如何真正提供服务?

感谢您的帮助。


更新

我尝试将我的SubcategoriesController.php 用作服务。在我的 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 ));
}

但是我有这个错误,这是我第一次尝试将控制器用作服务:

尝试从命名空间加载类“SubCategories” “我的公司\我的捆绑\控制器”在 C:\wamp\www\my\path\to\my\project\cache\dev\appDevDebugProjectContainer.php 第 618 行。您是否需要从另一个命名空间“使用”它?

【问题讨论】:

  • 只是为了我的个人信息,你为什么要那样做?我的意思是,你为什么将控制器定义为服务?
  • @DonCallisto,事实上,我认为使用控制器作为服务可以让我访问里面的所有方法。这样我就可以从我的控制器中的另一个控制器调用方法。现在我有这个错误:variable subcategory does not exist in twig line... 并且发生错误的行是我有 detail button&lt;a href="{{ path('indexSubCategories ', {'category': subCategories.category}) }}"&gt; 的行
  • 是的,你是对的,但这是做恕我直言的唯一理由。只是检查:P

标签: symfony service doctrine-orm controller routes


【解决方案1】:

你有错别字吗?

您从控制器 array('subcategory' =&gt; $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));
}

public function indexSubCategoriesAction ($category)
{
    $em=$this->getDoctrine()->getManager();
    $subcategory = $em->getRepository('MySpaceMyBundle:SubCategories')
        ->findOneByCategory($category);


    return $this->render('MySpaceMyBundle:MyFolder:indexSubCategories.html.twig', array(
        'subCategories' => $subcategory));
}

或者你应该改变你的树枝模板

<td>
  <a href="{{ path('indexSubCategories ', {'category': subCategories.category}) }}"><button class="btn btn-default btn-xs">Détails</button></a>
</td>

<td>
  <a href="{{ path('indexSubCategories ', {'category': subcategory.category}) }}"><button class="btn btn-default btn-xs">Détails</button></a>
</td>

更新

通过与 OP 聊天,我了解到他只需要从一个类别中检索一个子类别。我告诉他,这可以通过从视图中调用直接方法(点表示法)轻松获得,因为对象相互关联(ORM)

【讨论】:

  • 您建议我将名称更改为树枝循环。如果我为此更改:{% for category in categories%},似乎更好? (当然,如果必须,我会在所有文件中更改它)。错字?事实是我为变量和树枝变量使用了相同的名称似乎是一个问题,对吧?尝试使用您的建议时,我该如何解决我的问题:{'category': subCategories.category}?感谢您的帮助。
  • @dopebeat:问题不是由 for 引起的,当然,这只是一个额外的建议。问题似乎是由控制器或树枝模板中的拼写错误产生的。请查看我的更新。
  • @dopebeat,我认为你应该听从建议,你有两个变量的相同名称,也许它更好地理解和理解代码。
  • 我听从您的建议,我将更改我的代码。感谢您的帮助。当我的问题得到解决时,我会回到这里。我让你知道这个问题。
  • 其实是不行的。我遵循所有建议(更改变量名称......等),但我一直有同样的错误:variable subcategory(或者如果我改变,subcategories)does not exist in twig line...。也许如果我在Categories.php 中添加 subcategoriesFKOneToMany 的关系可以是一个解决方案,你怎么看?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 2013-04-01
相关资源
最近更新 更多