Hi,

I have a Category-Subcategories relationship, that is a 1:m relationship 
self-referencing. Besides, each Category or Subcategory can have different 
Products (1:m).

*1:m self-referencing:* 
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-self-referencing
*1:m: *
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-bidirectional

My question: how to retrieve only the categories but not the subcategories 
and viceversa? Below there is my approach, that I consider not very 
smart..do you consider it is smart enough? is there any smarter way to do 
it?

*NOTE*: Item=Category, Subitem=Product


    public function categoryListAction(Request $request)
    {
      $repository = $this->getDoctrine()->getRepository(
'ProjectFrontendBundle:Item');

      $qb = $repository->createQueryBuilder('i')
        ->where('i.parent IS NULL')
        ->getQuery();
      $items = $qb->getResult();


      return $this->render(
'ProjectBackendBundle:Default:item_list.html.twig', array('items' => $items
));
    }

    public function subcategoryListAction(Request $request, $id)
    {
      $repository = $this->getDoctrine()->getRepository(
'ProjectFrontendBundle:Item');

      $qb = $repository->createQueryBuilder('i')
        ->where('i.parent = :parent')
        ->setParameter('parent', $id)
        ->getQuery();
      $items = $qb->getResult();

      return $this->render(
'ProjectBackendBundle:Default:item_list.html.twig', array('items' => $items
));
    }

    public function subitemListAction($id)
    {
       $repository = $this->getDoctrine()->getRepository(
'ProjectFrontendBundle:Item'); 

       $category = $repository->find($id);
       $products = $category->getSubItems();

       return $this->render(
'ProjectFrontendBundle:Default:list-products.html.twig', array('products' => 
$products));
    }


This is the view (twig), that I don't like too much because those "ifs".

{% for item in items %}
{% if item.subitems is empty %}
  <a href="{{ path('project_backend_subcategory_list', {'id': item.id }) }}"
>{{ item.name }}</a>
{% endif %}
{% if item.subitems is not empty %}
  <a href="{{ path('project_backend_subitem_list', {'id': item.id }) }}">{{ 
item.name }}</a>
{% endif %}
<!-- -->
{% endfor %}

And this is the routing:

project_backend_category_list:
    path:     /category/list
    defaults: { _controller: ProjectBackendBundle:Default:categoryList }

project_backend_subcategory_list:
    path:     /subcategory/list/{id}
    defaults: { _controller: ProjectBackendBundle:Default:subcategoryList }

project_backend_subitem_list:
    path:     /subitem/list/{id}
    defaults: { _controller: ProjectBackendBundle:Default:subitemList }


Let me know if this is a question for the doctrine google group, but since 
there are view and routing code I prefered to ask here.

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to