sonata:处理sonata_type_model(一对多)



1-我有一个实体:

EmployeeMedicalService

/**
 * @ORMEntity
 * @ORMTable(name="employee_medical_file")
 */
class EmployeeMedicalService extends BaseEntity
{
   //
   // Some
   // Fields
   //

    /**
     * @AssertNotBlank
     * @ORMManyToOne(targetEntity="PersonnelBundleEntityLookupLookup")
     * @ORMJoinColumn(name="medical_service_id", referencedColumnName="id")
     */
    private $medicalService;
   //
   // getters
   // & setters
   //

2-另一个实体:

查找

/**
 * @ORMEntity
 * @ORMTable(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
     * 
     * @ORMColumn(name="type", type="string", length=50)
     * @AssertNotBlank
     */
    private $type;
    //getters
    // &setters

现在在

EmployeeMedicalServiceAdmin

protected function configureFormFields(SonataAdminBundleFormFormMapper $formMapper)
    {
        $msquery = $this->getModelManager()
                ->getEntityManager('PersonnelBundle:LookupLookup')
                ->createQueryBuilder();
        $msquery->select('l')->from('PersonnelBundle:LookupLookup', '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管理表单中添加新查找(医疗服务)的表单预加载字段Type值设置为'medical_service'当我尝试从Employer MedicalService管理表中添加新的医疗服务时,或者如果Type设置为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',
                ))
        ;
    }

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

更新

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

public function getNewInstance()
{
    $type = isset($_GET['pcode']) ? 'medical_service' : '';
    $instance = new PersonnelBundleEntityEmployeeEmployeeMedicalService($type);
    return $object;
}

相关内容

  • 没有找到相关文章

最新更新