我需要访问表单对象中的属性。问题是,我想访问的属性不是在表单中呈现,也不是在contractType类中声明的。
class ContractType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('rcode1', new TextType(), array('label' => 'rcode 1'))
->add('rcode2', new TextType(), array('label' => 'rcode 2'));
}
...
}
转储表单对象:
array:6 [▼
"contract" => Contract {#2003 ▼
- id: null
- actionCode: "104"
- productCode: "20106"
- created: null
- updated: null
- resumeId: null
- rcode1: null
- rcode2: null
- downloadId: null
- businessContractDetails: BusinessContractDetails {#1999 ▶}
- privateContractDetails: null
- company: Company {#2000 ▶}
- persons: ArrayCollection {#1998 ▶}
}
"businessContractDetails" => BusinessContractDetails {#1999 ▶}
"company" => Company {#2000 ▶}
"contactPerson" => ContactPerson {#1987 ▶}
"landlord" => Landlord {#1993 ▶}
"businessRealEstate" => BusinessRealEstate {#1994 ▶}
]
合同实体的属性是rcode1和rcode2。但我需要访问downloadID。
我试过
$form->get('contract')->get('downloadId')->getData();
并收到以下错误消息: Child" downloadId"不存在。
有什么建议吗?提前谢谢!
答案 0 :(得分:0)
尝试
$form->get('contract')->getData()->getDownloadId();
请注意我假设您的getDownloadId
课程中有一个getter Contract
答案 1 :(得分:0)
您的控制器不应通过表单访问实体的值。它会起作用,但这是一种不好的做法,例如,如果表单类型contract
的名称会发生变化。
在handleRequest
该实体填充表单中的数据之后,您可以依赖您创建表单的实体。所以:
# Controller POST action
$entity = new Contract(); // Or retrieve it from database: $em->find(Contract, $id);
$form = $this->createForm(FormType::class, $entity)->handleRequest($request);
echo $entity->getDownloadId();