Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did some tests with the EventSubscriber but couldn't get it to work properly so I decided on just directly adding the code to my class (q&amp;d). I use Zend Framework so change the fetching of the entity manager if you have a different solution.</p> <p>It works as follows:</p> <ol> <li>When creating the entity use the getLast() and add the new entity last ($last_entity->getSort() + 1)</li> <li>Add the sortUp and sortDown functions and their subroutines to the entity code</li> <li>Add "sort ASC" in all your DQL queries where you want it sorted</li> </ol> <p>The entity code is as follows:</p> <pre><code>/** * Searches for the element above and switches them * with eachother. Performs a EntityManager::flush() * to save the results after the switch. */ public function sortUp() { try { $em = \Zend_Registry::get("entitymanager"); $class_name = get_class($this); $dql = "SELECT ut FROM $class_name ut WHERE ut.inactive IS NULL AND ut.inactive IS NULL AND ut.sort &lt; '" . $this-&gt;getSort() . "' ORDER BY ut.sort DESC"; $query = $em-&gt;createQuery($dql); $query-&gt;setMaxResults(1); $ut = $query-&gt;getResult(); } catch (Exception $exc) { throw new Exception("Error when looking for sortable partner: " . $exc-&gt;getMessage()); } if (count($ut)) { $this-&gt;_switchSortAndSave($ut[0]); } } /** * Searches for the element below and switches them * with eachother. Performs a EntityManager::flush() * to save the results after the switch. */ public function sortDown() { try { $em = \Zend_Registry::get("entitymanager"); $class_name = get_class($this); $dql = "SELECT ut FROM $class_name ut WHERE ut.inactive IS NULL AND ut.sort &gt; '" . $this-&gt;getSort() . "' ORDER BY ut.sort ASC"; $query = $em-&gt;createQuery($dql); $query-&gt;setMaxResults(1); $ut = $query-&gt;getResult(); } catch (Exception $exc) { throw new Exception("Error when looking for sortable partner: " . $exc-&gt;getMessage()); } if (count($ut)) { $this-&gt;_switchSortAndSave($ut[0]); } } private function _switchSortAndSave(\Entities\Usertype $switch_entity) { $new_sort = $switch_entity-&gt;getSort(); $switch_entity-&gt;setSort($this-&gt;getSort()); $this-&gt;setSort($new_sort); $em = \Zend_Registry::get("entitymanager"); $em-&gt;persist($switch_entity); $em-&gt;persist($this); $em-&gt;flush(); } /** * Looks for the last entry according to sort order * and returns that if found. * * @return Entity|null */ public static function findLast() { try { $em = \Zend_Registry::get("entitymanager"); $class_name = get_called_class(); $dql = "SELECT ut FROM $class_name ut ORDER BY ut.sort DESC"; $query = $em-&gt;createQuery($dql); $query-&gt;setMaxResults(1); $ut = $query-&gt;getResult(); } catch (Exception $exc) { throw new Exception("Error when searching for last $class_name: " . $exc-&gt;getMessage()); } if (count($ut)) return $ut[0]; else return null; } </code></pre> <p>I'm not so pleased with this solution so if someone comes up with a nice Sortable for Doctrine 2 please share :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload