我正在构建API并使用表单进行数据绑定,现在我希望能够传递实体的ID,获取引用并将其附加到项目,这是一个示例:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('credit');
$builder->add('currency', 'currency');
$builder->add('borrower', BorrowerType::class, [
'required' => true
]);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
$borrower = $this->em->getReference(Borrower::class, $data['borrower']);
$data['borrower'] = $borrower;
});
}
返回"错误:此值无效。\ n"
这是我的测试请求:
$client->request('POST', $endpoint, [
'loan' => [
'credit' => '1000',
'currency' => 'EUR',
'borrower' => $borrower->getId()
]
]);
答案 0 :(得分:1)
“$ event-> getData()”是您的实际实体,如果此表单在其选项解析程序中使用默认实体,或者您在制作表单时通过控制器传递实体。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('credit');
$builder->add('currency', 'currency');
$builder->add('borrower', BorrowerType::class, [
'required' => true
]);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$entity = $event->getData();
//The following doesn't make a lot of sense
//The borrower will already be set
$borrower = $this->em->getReference(Borrower::class, $entity->getBorrower());
$entity->setBorrower($borrower);
});
}
就像用这种检查用户是否必须输入密码的标准方式(如果它们是新的)一样,或者如果用户已经存在则不需要输入密码。
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$user = $event->getData();
$form = $event->getForm();
$formParams = [
'type' => PasswordType::class,
'first_options' => ['label' => 'Password'],
'second_options' => ['label' => 'Repeat Password'],
];
if ((!$user || null === $user->getId())) {
$formParams['constraints'] = new NotBlank();
} else {
$formParams['required'] = false;
}
$form->add('plainPassword', RepeatedType::class, $formParams);
});
如果此表单正在创建或更新实体,则借用者信息在创建时设置为实体。
答案 1 :(得分:1)
一种直接的方法来达到这个" symfonic"方法是使用DataTransformer
DataTransformer将采用数据格式并将其转换为另一种格式(类型)。
您可以使用的是ViewTransformer
Transform()
,在id
阶段,将获取实体并返回其ReverseTransform()
,而在id
中则需要class BorrowerToIdTransform implements DataTransformerInterface
{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
/**
* Transforms an object (Borrower) to a string (id).
*
* @param Borrower|null $issue
* @return string
*/
public function transform($borrower)
{
if (null === $borrower) {
return '';
}
return $borrower->getId();
}
/**
* Transforms a string (id) to an object (Borrower).
*
* @param string $id
* @return Borrower|null
* @throws TransformationFailedException if object (issue) is not found.
*/
public function reverseTransform($id)
{
if (!$id) {
return;
}
$borrower = $this->manager
->getRepository(Borrower::class)
->find($id)
;
if (null === $borrower) {
throw new TransformationFailedException(sprintf(
'A borrower with "%s" id does not exist!',
$id
));
}
return $issue;
}
}
并且,通过使用存储库,将检索实体。
像
这样的东西 @Repository
public class OhlcDaoImpl implements OhlcDao {
@Autowired
SessionFactory sessionFactory;
Session session;
public List getOhlc(Result result) {
session = sessionFactory.openSession();
SQLQuery query = session.createSQLQuery("EXEC getOhlc :stockSymbol, :statementType, :financialDataTypeId, :fiscalYearId");
query.setString("stockSymbol",result.getStockSymbol());
query.setString("statementType",result.getStatementType());
query.setInteger("financialDataTypeId",result.getFinancialDataTypeId());
query.setInteger("fiscalYearId",result.getFiscalYearId());
List l = query.list();
return l;
}
}