1
votes

Please advise with the best solutions for such a task:

Entity hierarchy: Department -> Category -> Product

Currently in Category admin I have a list of ALL categories for ALL departments. BUT, I need to implement ordering feature for categories, which is done with Doctrine Sortable, so I'm having moving arrows in admin list view. BUT, because positions are grouped by Department of category, I have multiple "first" and "last" categories and moving is not that clear (since I cannot post images yet):

1. category [move down]
2. category [move down]
3. category [move down][move up]
4. category [move down][move up]

So, I added a default filter by department and now have shorter lists of categories by department. This has a lot of problems further on, so I'm looking for a better symfony/sonata/something else solution for:

Separate (dynamic) admin/list of categories by same department (same thing happens with products by category).

PS. Subclasses is not the solution, because I have only one category entity class, obviously.

1

1 Answers

1
votes

I had the same issue, my solution was simple.

I wrote a Console Command Function, Where i did a repository call to retrieve all entries in that entity. Then I simply looped through them and assigned them an incremented position value. This will give structure to your admin listing. All the entries added post that command will automatically given the next position value and your sortable functionality will work perfectly.

Example :

 protected function execute(InputInterface $input, OutputInterface $output)
{
  $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
  $repo = $this->em->getRepository('AppBundle:Leadership');
  $entries = $repo->findAll();

  $index = 0;
    foreach ($entries as $entry) {
        $entry->setPosition($index);
        $this->em->persist($entry);
        $this->em->flush();
        $index = $index + 1;
    }

}

Hope this helps.