1-我有一个实体:
/**
* @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
现在在
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
这是
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text', array(
'label' => 'personnel.lookup.form.name'
))
->add('type', 'hidden', array(
'label' => 'personnel.lookup.form.type',
))
;
}
答案 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;
}