Sonataadminbundle: How to hide relation in sonata_type_collection

Created on 3 Dec 2012  路  4Comments  路  Source: sonata-project/SonataAdminBundle

Hi,

I have an entity "question" that has one-to-many relation to "answer". So I have made admin classes for both and trying to use embedded form by "sonata_type_collection". The main problem is that in embedded answers also have field "question".

see screenshot at http://i.imgur.com/qJsdL.png

/**
 * Answer
 * @ORM\Table()
 * @ORM\Entity
 */
 class Answer
{
 /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @ORM\ManyToOne(targetEntity="IY\PollBundle\Entity\Question", inversedBy="answers")
 * @ORM\JoinColumn(name="question_id", referencedColumnName="id")
 */
protected $question;
}

/**
 * Question
 * @ORM\Table(name="poll_question")
 * @ORM\Entity
 */
class Question
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @ORM\OneToMany(targetEntity="IY\PollBundle\Entity\Answer", mappedBy="question", cascade={"persist", "remove"})
 */
protected $answers;
}

////// admins

class QuestionAdmin extends Admin
{
 /**
 * Configure the form
 *
 * @param FormMapper $formMapper formMapper
 */
  public function configureFormFields(FormMapper $formMapper)
  {
    $formMapper
        ->with('General')
          ->add('name')
        ->end()
        ->with('Answers')
          ->add('answers', 'sonata_type_collection',  array('by_reference' => false, 'required' => false), array(
                'edit' => 'inline',
                'inline' => 'table',
            ))
        ->end()
;
  }


class AnswerAdmin extends Admin
{

/**
* Configure the form
*
* @param FormMapper $formMapper formMapper
*/
 public function configureFormFields(FormMapper $formMapper)
 {
 $formMapper
->with('General')
->add('question')
->add('name')
->end();
  }

///// services

 <service id="iy.poll.admin.answer" class="IY\PollBundle\Admin\AnswerAdmin">
        <tag name="sonata.admin" manager_type="orm" group="Poll" label="Answer" />
        <argument/>
        <argument>IY\PollBundle\Entity\Answer</argument>
        <argument>NULL</argument>
    </service>

    <service id="iy.poll.admin.question" class="IY\PollBundle\Admin\QuestionAdmin">
        <tag name="sonata.admin" manager_type="orm" group="Poll" label="Question" />
        <argument/>
        <argument>IY\PollBundle\Entity\Question</argument>
        <argument>NULL</argument>
    </service>

//////////////////////////////

So how to hide the file "question"?

I can do it by removing "->add('question')" from answer form, but in that case I have to hide also admin page for answer too.

Most helpful comment

You can try code below in configureFormFields of AnswerAdmin

        if ($this->getRoot()->getClass() != 'IY\PollBundle\Entity\Question') {
            $formMapper
                ->add('question');
        }

It may be better to check root service id instead of entity class, but approach above works for me.
Also i had to add preUpdate and prePersist implementations to set question explicitly for each answer in collection, else answers removal does not work for me.
I'm using symfony 2.1.

All 4 comments

You can try code below in configureFormFields of AnswerAdmin

        if ($this->getRoot()->getClass() != 'IY\PollBundle\Entity\Question') {
            $formMapper
                ->add('question');
        }

It may be better to check root service id instead of entity class, but approach above works for me.
Also i had to add preUpdate and prePersist implementations to set question explicitly for each answer in collection, else answers removal does not work for me.
I'm using symfony 2.1.

thanks

This code might be more comprehensible and more flexible

use IY\PollBundle\Entity\Question;
//...
if ($this->getRoot()->getClass() != Question::class) {
//...
}

or if you want the field only displayed in it's own admin page

if ($this->getRoot()->getClass() == get_class($this->getSubject()) {
//...
}
Was this page helpful?
0 / 5 - 0 ratings