T
T
TrueDrago2013-02-05 10:52:12
symfony
TrueDrago, 2013-02-05 10:52:12

Sorting entities in Doctrine 2?

For the last project, I created an entity that should support sorting. I added Sortable behavior for it according to the instructions , but the following problem appeared. In addition to position getters and setters, I need the following methods:

<?php<br>
public function moveUp() {<br>
    ++$this->position;<br>
}<br><br>
public function moveDown() {<br>
    if($this->position != 0)<br>
        --$this->position;<br>
}<br>

With a downward shift, in principle, everything is clear - it will not move below zero. But what about the maximum positions? As far as I understand, making queries to the database directly from the entity in order to determine the maximum value is not the best architectural solution. How then to forbid to increment an element with the maximum position?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JekaRu, 2013-02-05
@JekaRu

If my memory serves me right, it doesn't seem to make much sense to do it, everything is automatically recalculated when saving.
Do you really need to limit the maximum value?

S
Sergey, 2013-02-05
Protko @Fesor

Well, it seems to me not a very convenient implementation. Because...
a - 1
b - 2
c - 3
if you do c.moveUp()
a - 1
b - 2
c - 2
And it will be difficult to count, don't you think?
In general, everything related to data manipulation before saving, whether it is recalculating the order of entities or hashing passwords, should be performed in listeners (this is just my opinion, and yes, this method does not always work)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question