2

Symfony1 クックブックhttp://symfony.com/legacy/doc/cookbook/1_2/en/sortableのこのエントリを Symfony2 に書き直しました。私の最初の目的は、古典的なソート可能なリストです。

ここに画像の説明を入力

すべてのドキュメント (MongoDB) には、表示される順序を持つ整数型の位置フィールドがあります。

次のメソッドがあるサービス クラス Sortable を作成しました。

class Sortable
{
    //Return the position of the document
    public function getByPosition($position = 1, $document, $site, $item = null){ /**/ }

    //Return all elements sorted by position
    public function getAllByPosition($document, $site){/**/}

    //Return the max position of the items within a parent item
    public function getMaxPosition($document, $site, $parent = null){/**/}

    //Swap the position between $itemOne and $itemTwo
    public function swapWith($itemOne, $itemTwo, $document){/**/}

    //Set the new position => +1
    public function executeUp($document, $id){/**/}

    //Set the new position => -1
    public function executeDown($document, $id){/**/}

    //Persist document with the new position
    public function save($item, $document){/**/}
}

これは問題なく動作しますが、主な問題は、このクラスがほとんど再利用できないことです。

-$document var は、たとえばcreateQueryBuilder('MyBundle'.$document)で使用するデータベース内のドキュメントの名前です。

-$site var は、各ユーザーがサイトを持っているため、私のアプリのサイトです。

-$parent var はタイプ $document であり、同じタイプのドキュメントの親です。

私にとっての問題は、これはほとんど再利用できず、コントローラーアクションで上記のすべてのメソッドを呼び出してから、Twig テンプレートの位置を確認する必要があることです。私は自分のサービスを呼び出し、コントローラーと小枝で行うすべてのロジックを実行する小枝拡張を実現したいと考えています。また、どんなドキュメントでも機能します。

どうすればこれを入手できますか?

4

0 に答える 0