Reputation:
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?
Upvotes: 1
Views: 157
Reputation: 29912
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
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>
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)
Upvotes: 1