奏鸣曲:处理sonata_type_model(一对多)

时间:2015-06-21 12:31:12

标签: symfony doctrine sonata-admin symfony-sonata sonata

1-我有一个实体:

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-另一个实体:

查找

/**
 * @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

现在在

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(..)
        ;
    }

**我的问题:**

我需要从EmployeeMedicalService管理表单中添加新查找(医疗服务)的表单,预先将字段类型设置为&#39; medical_service&#39; < / strong>当我尝试从EmployeeMedicalService管理表单中添加新的医疗服务时,如果类型设置为 NULL

这是

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 个答案:

答案 0 :(得分:1)

如果检查弹出窗体的ajax请求,您会注意到额外的查询参数,例如pcode。您可以检查此参数是否存在并等于EmployeeMedicalServiceAdmin管理类代码,然后将lookup类型设置为medical_service

<强>更新

getNewInstance()方法中添加此逻辑之王:

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

    return $object;
}