0

使用事例:

ユーザーは、専門知識の記録に関する公開および/または非公開のメモを作成します。これらのメモは、専門知識の記録を示すページのサイドバーに表示されます。メモは、現在のユーザーのメモ (公開と非公開の両方) と他のユーザーからの公開メモの 2 つのセットにグループ化されます。現在のユーザーのメモはすべて編集および削除可能です。各削除アイコンはルートへのリンクです。各編集アイコンは、特定のメモのフォームを表示するための JavaScript トグルです (編集フォームは、入力されていることを除けば、追加フォームのように見えます)。送信アクションごとに追加/編集または削除できるメモは​​ 1 つだけです。送信はajaxではありません。ページは次のようになります。

ノート サイドバーのある専門知識ページ

問題

「新しいメモ」と「マイ メモ」セクションの各メモの両方について、独立したフォームを表示して処理するにはどうすればよいですか?

NoteType フォームのコレクションを ExpertiseType フォームに埋め込んだ場合、処理は正しく行われますが、フォームを「メモの追加」、「私のメモ」、および「公開メモ」セクションにグループ化できないようです。さらに悪いことに、公開メモは編集可能です。どのメモにフォームがあるかを簡単にフィルターする方法がないため、このアプローチを断念しました。

コントローラーでメモをサブセット化し、それぞれのフォームを作成すると、名前付きフォームを使用しない限り、すべてのメモが新しいメモであるかのように処理されます。名前付きフォームを使用すると、新しいメモが正しく作成されます。ただし、すべての編集は最新のメモに適用され、他すべてのメモのプライバシー フィールドとテキスト フィールドは列のデフォルトに設定されています。どうすればこれを回避できますか?

サイドバーの小枝テンプレート:

{% block sidebar %}
    <div id='sidebar'><h2>Profile Notes</h2>
        {% for flashMessage in app.session.flashbag.get('note_success') %}
            <div class="flash-success">
                {{ flashMessage }}
            </div>
        {% endfor %}
        <fieldset class='wrapper'><legend>Add a note</legend>
            {{ form(form) }}
        </fieldset>
        {% if usernotes|length > 0 %}
            <fieldset><legend>My notes</legend>
            <dl>
            {% for  note in usernotes%}
                <dt>{{ note.moddate|date("Y-m-d H:i") }}
                    <a class='actionlink' href='{{ path('exp_delnote', {'id':note.id}) }}' title="delete"><img src="{{ asset('images/silk_icons/delete.png') }}" alt="delete note"></a>
                    <a class='actionlink inlineedit' href='#' data-editform="{{ form(note.myform)|e }}" title="edit note"><img src="{{ asset('images/silk_icons/pencil.png') }}" alt="edit note"></a>
                </dt>
                <dd class='note_{{ note.private ? 'priv' : 'pub' }}' title='{{ note.private ? 'Private Note' : 'Public Note'}}'>{{ note.note }}</dd>
            {% endfor %}
            </dl>
            </fieldset>
        {% endif %}
        {% if publicnotes|length > 0 %}
            <fieldset><legend>Public notes</legend>
            <dl>
            {% for  note in publicnotes%}
                <dt>{{ note.moddate|date("Y-m-d H:i") }}, {{ note.person|expadinfo("fullname") }}</dt>
                <dd>{{ note.note }}</dd>
            {% endfor %}
            </dl>
            </fieldset>
        {% endif %}
    </div>
{% endblock %}

エンティティ

3 つのエンティティ クラスがあります: ExpertiseNoteおよびPersonExpertiseエンティティには のコレクションがありますNotes。それぞれが(その「所有者」) とレコードNoteに関連付けられています。エンティティは Doctrine によって管理されます。PersonExpertise

私の現在のコントローラーメソッド

これにより、新しいメモが正常に作成されます。ただし、どのメモを編集しても、変更は最新のメモに適用され、他の編集可能なメモのテキスト フィールドとプライバシー フィールドは列のデフォルトに設定されます。

public function profileAction($uname){
    $request = $this->get('request');
    $adsearcher = $this->get('exp_adsearcher');

    $userrecord = $adsearcher->getRecordByUserName($uname);
    if(!$userrecord) throw $this->createNotFoundException('No such person exists.');
    $userrecord->setLocale($request->get('_locale'));
    $person = $adsearcher->getPersonByRecord($userrecord);
    $expertise = $this->getExpertiseEntity($person);
    $curuser = $adsearcher->getPersonByUserName($this->getUser()->getUsername());


    //add a new note, and trying to use a named form
    $newnote = $this->getNewNote($expertise, $curuser);
    $addnoteform = $this->get('form.factory')->createNamedBuilder('newnote', new NoteType(), $newnote)->getForm();

    $addnoteform->handleRequest($request);
    if($addnoteform->isValid()){
        $em = $this->getDoctrine()->getManager();
        $em->persist($newnote);
        $em->flush();

        $this->get('session')->getFlashBag()->add(
                'note_success',
                $this->get('translator')->trans('message.note.add.success')
        );
        $addnoteform = $this->createForm(new NoteType, $this->getNewNote($expertise, $curuser)); //so that add form is always empty
    }

    //get all the editable notes
    $usernotes = $expertise->getUserNotes($curuser);
    foreach($usernotes as $note){
        $form = $this->get('form.factory')->createNamedBuilder('note'.$note->getID(), new NoteType(), $note)->getForm();
        $form->handleRequest($request);
        if($form->isValid()){
            $msg = 'message.note.edit.success';
            $em = $this->getDoctrine()->getManager();
            $em->persist($expertise);
            $em->flush();

            $this->get('session')->getFlashBag()->add(
                    'note_success',
                    $this->get('translator')->trans('message.note.edit.success')
            );
            return $this->redirect($this->generateUrl('exp_profile', array('uname'=>$uname)));
        }
        $note->myform = $form->createView();
    }

    //get all the public notes, not editable
    $publicnotes = $expertise->getPublicNotes($curuser, $adsearcher);

    return array('userrecord'=> $userrecord, 'expertise'=>$expertise,'usernotes'=>$usernotes, 'publicnotes'=>$publicnotes, 'form'=>$addnoteform->createView());

}
4

1 に答える 1