sonata: Dealing with sonata_type_model (one-to-many)

788 views Asked by At

1- I have an Entity:

EmployeeMedicalService

/**
 * @ORM\Entity
 * @ORM\Table(name="employee_medical_file")
 */
class EmployeeMedicalService extends BaseEntity
{

   //
   // Some
   // Fields
   //


    /**
     * @Assert\NotBlank
     * @ORM\ManyToOne(targetEntity="PersonnelBundle\Entity\Lookup\Lookup")
     * @ORM\JoinColumn(name="medical_service_id", referencedColumnName="id")
     */
    private $medicalService;

   //
   // getters
   // & setters
   //

2- Another Entity:

Lookup

/**
 * @ORM\Entity
 * @ORM\Table(name="lookup")
 * @UniqueEntity(fields="name")
 */
class Lookup extends BaseEntity
{
    // const ...
    const TYPE_MEDICAL_SERVICE = 'medical_service';
    // more constants ...

    public function __construct($type)
    {
        $this->type = $type;
    }

     //
     // Some Fields
     //

    /**
     * @var string
     * --stuff--
     */
    private $name;

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

    //getters
    // &setters

Now in the

EmployeeMedicalServiceAdmin

protected function configureFormFields(\Sonata\AdminBundle\Form\FormMapper $formMapper)
    {

        $msquery = $this->getModelManager()
                ->getEntityManager('PersonnelBundle:Lookup\Lookup')
                ->createQueryBuilder();

        $msquery->select('l')->from('PersonnelBundle:Lookup\Lookup', 'l')->where('l.type = :type')
                ->orderBy('l.name', 'ASC')
                ->setParameter('type', 'medical_service');

        $formMapper
                ->add(..)

                ->add('medicalService', 'sonata_type_model', array(
                    'label' => 'personnel.employee.medical_service.form.medical_service',
                    'property' => 'name',
                    'placeholder' => '',
                    'required' => false,
                    'query' => $msquery,
                ))

                ->add(..)
        ;
    }

** My Problem: **

I need the form for add new lookup(medical service) from inside the EmployeeMedicalService Admin Form to be preloaded with the field Type value set to 'medical_service' When I attempt to add a new Medical Service from inside the EmployeeMedicalService Admin Form or else a new lookup is added without the value if Type set to NULL


This is the

LookupAdmin

protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
                ->add('name', 'text', array(
                    'label' => 'personnel.lookup.form.name'
                ))
                ->add('type', 'hidden', array(
                    'label' => 'personnel.lookup.form.type',
                ))
        ;
    }
1

There are 1 answers

11
Vadim Ashikhman On BEST ANSWER

If you inspect ajax request for the popup form you will notice extra query parameters, such as pcode. You could check if this parameter exists and equals EmployeeMedicalServiceAdmin admin class code then set lookup type to medical_service.

UPDATE

Add this king of logic in the getNewInstance() method:

public function getNewInstance()
{
    $type = isset($_GET['pcode']) ? 'medical_service' : '';
    $instance = new \PersonnelBundle\Entity\Employee\EmployeeMedicalService($type);

    return $object;
}