1

In my Symfony app, I'm trying to use a controller as a service, in order to display datas I need in a table.

The two entities are Categories.php and SubCategories.php. 1 Category could have many SubCategories and 1 subCategory could belong to only one Category. I have a relation ManyToOne, and the FK is in my SubCategories entity. So, in my Symfony/Doctrine project, I have a variable $category in my SubCategories.php entity.

here's my twig view, she'll be used to make search in a table on all datas for my Categories.php entity:

       <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>

Like you can see, in my column Sub categories, I have a , linked to the index of SubCategories. In fact I'm trying to redirect to the sub categories index for manage them. But If I click on this button Details, I would like the indexSubCategories display all sub categories in relation with the category I click on.

Like this:

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

So when I click on a detail button, it displays in indexSubcategories, all the subcategories for the category line I click on details button.

Here's my controller for Categories:

public function indexCategoriesAction()
    {
        $em=$this->getDoctrine()->getManager();
        $category = $em->getRepository('MySpaceMyBundle:Categories')->findAll();


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

And this my controller for SubCategories:

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));
}

In the indexSubCategories.html.twig, I'm doing the same thing in a table, as simple as I make for indexCategories.html.twig.

I think I need to use the SubCategories controller as a service to make it?

This is my route files:

# 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

How can I really proceed?

I already look at here, but How do I really make my service?

Thank you for the help.


UPDATE

I try to use my SubcategoriesController.php as a service. In my service.yml (same folder bundle), this my code:

services:
    subCategoriesDetail:
        class: MyCompany\MyBundle\Controller\SubcategoriesController

I call this in the 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 ));
}

But I have this error, It's the first time I try to use a controller as a service:

Attempted to load class "SubCategories" from namespace "MyCompany\MyBundle\Controller" in C:\wamp\www\my\path\to\my\project\cache\dev\appDevDebugProjectContainer.php line 618. Do you need to "use" it from another namespace?

Community
  • 1
  • 1
  • Just for my personal information, why are you procede that way? I mean, why are you defining controller as a service? – DonCallisto Feb 12 '15 at 17:12
  • 1
    @DonCallisto, In fact I thought that used a controller as a service could afford me to access all of the methods inside. In that way I could call method from another controller in my controller. For now I have this error: `variable subcategory does not exist in twig line...` and the line where the error occured is the line where I have the **detail button** with `` –  Feb 13 '15 at 06:05
  • Yes, you're right, but this is the only reason to do imho. Just checking :P – DonCallisto Feb 13 '15 at 07:21

1 Answers1

1

Do you have a typo?

You are passing from controller array('subcategory' => $subcategory) but trying to render {'category': subCategories.category}

Moreover I suggest to change names into twig loop:

{% for category in category %}

because after the loop, category will be an element and not the original collection


UPDATE

You should change your controller from

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));
}

to

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));
}

or you should change your twig template from

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

to

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

UPDATE

From chatting with OP I've understood that he needs only to retrieve from a category a subcategory. I told him that this could easily obtain by calling direct methods (dot notation) from views as objects are related each other (ORM)

Community
  • 1
  • 1
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • 1
    you suggest me to changenames into twig loop. If I change for this: `{% for category in categories%}`, it seems to be better? (of course I change this in all my files if I have to). A typo? The fact is that I used the same name for variable and twig variable seems to be a problem right? How can I fix my problem trying to use your suggestion: `{'category': subCategories.category}` ? Thank you for the help. –  Feb 13 '15 at 07:57
  • 3
    @dopebeat: issue isn't caused by for, of course, that was just an additional advice. Issues seems to be generated from a typo in your controller or twig template. Please check out my update. – DonCallisto Feb 13 '15 at 08:01
  • 2
    @dopebeat, I think you should follow the advice, you have the same name for two variable, maybe it's better for comprehension and in apprehension code. – french_dev Feb 13 '15 at 08:21
  • I follow your suggestions, I'm going to make change in my code. Thank you for the help. I comeback here when my issue will be fixed. I let you know about the issue. –  Feb 13 '15 at 08:24
  • In fact, it does not work. I follow all the suggestions (change name of variables ... etc), but I have all the time the same error: `variable` **subcategory** (or if I change, **subcategories**) `does not exist in twig line...`. Maybe If I add the **FK** of **subcategories** in `Categories.php` with a relation of `OneToMany` can be a solution, what do you think? –  Feb 13 '15 at 09:46
  • @dopebeat: I think that if you pass a variable to a view, and you use correct names, this variable will be available to template. Probably you're messing something up :) – DonCallisto Feb 13 '15 at 09:49
  • @DonCallisto, in fact, like I have two **controller**, one for **Categories**, and another for **SubCategories**, it's normal I have this error, because in the controller for **Categories** in the method I call, variable for subCategories does not exist. And the **FK** is `$category`, and appears in my entity `SubCategories.php`. That's why I think I need to use my **Subcategories controller** as a `service`. Or maybe there is another solution, but I'm still looking for... –  Feb 13 '15 at 10:11
  • @dopebeat: why don't you simply use the second controller (subcategories) as service into first controller (categories) and make subcategories available to category view? – DonCallisto Feb 13 '15 at 10:14
  • @DonCallisto, I think it is a better solution. I try but I don't know really how it works. Look at my update, I have an error, and I forgot to do something right for using a controller as a service. –  Feb 13 '15 at 10:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70843/discussion-between-dopebeat-and-doncallisto). –  Feb 13 '15 at 10:33