PHPackages                             newquery/symfony-entity-order - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. newquery/symfony-entity-order

ActiveLibrary

newquery/symfony-entity-order
=============================

Add a new property to you entity 'positionOrder' so you can give your items a position in a sorted collection

04PHP

Since Jun 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/newQuery/symfony-entity-order)[ Packagist](https://packagist.org/packages/newquery/symfony-entity-order)[ RSS](/packages/newquery-symfony-entity-order/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

EntityOrderBundle
=================

[](#entityorderbundle)

Helps to create ordered list from Entities.

Usage
=====

[](#usage)

Add the trait to the desired entity

```
namespace App\Entity\Misc;

use App\Repository\Misc\TeamMemberRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use newQuery\Bundle\EntityOrder\Lib\OrderEntity;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass=TeamMemberRepository::class)
 * @ORM\HasLifecycleCallbacks()
 */
 class TeamMember
{

  use OrderEntity;  // THIS

  /**
  * @ORM\Id
  * @ORM\GeneratedValue
  * @ORM\Column(type="integer")
  */
  private $id;
```

Make sure to add `@ORM\HasLifecycleCallbacks() `

Enjoy, your entity now has the positionOrder property.

Create a dynamic Controller for increasing/decreasing the position
------------------------------------------------------------------

[](#create-a-dynamic-controller-for-increasingdecreasing-the-position)

```
/**
 * @Route("/increase/{className}/{id}", name="increase_position")
 */
public function increase(string $className, int $id, OrderPositionHelper $helper, Request $request): Response
{
    $em = $this->getDoctrine()->getManager();
    $meta = $em->getMetadataFactory()->getAllMetadata();

    $repository = null;
    foreach ($meta as $m) {
        if(strpos($m->getName(), 'App\Entity') !== false && strpos($m->getName(), $className) !== false) {
            $repository = $em->getRepository($m->getName());
            break;
        }
    }

    if(null !== $repository) {
        $result = $helper->increase($repository, $id);
        $this->getDoctrine()->getManager()->flush();

        if(true === $result) {
            $this->addFlash('success', 'Ordre mis à jour!');
        }
    }

    return $this->redirect($request->headers->get('referer'));
}
```

```
 /**
 * @Route("/decrease/{className}/{id}", name="decrease_position")
 */
public function decrease(string $className, int $id, OrderPositionHelper $helper, Request $request): Response
{
    $em = $this->getDoctrine()->getManager();
    $meta = $em->getMetadataFactory()->getAllMetadata();

    $repository = null;
    foreach ($meta as $m) {
        if(strpos($m->getName(), 'App\Entity') !== false && strpos($m->getName(), $className) !== false) {
            $repository = $em->getRepository($m->getName());
            break;
        }
    }

    if(null !== $repository) {
        $result = $helper->decrease($repository, $id);
        $this->getDoctrine()->getManager()->flush();

        if(true === $result) {
            $this->addFlash('success', 'Ordre mis à jour!');
        }
    }

    return $this->redirect($request->headers->get('referer'));
}
```

In twig template:

```
-
+
```

Error
-----

[](#error)

You can fix the order of all element from one or all entities using the **trait** with `$ php bin/console nq:position-order:fix
