我已经搜索并没有找到有这个问题的人。
我已经按照 Cookbook 中的说明创建了自己的数据转换器,看起来一切正常,但出现错误:
表单的视图数据应该是 Niche\SecurityBundle\Entity\BusinessUser 类的实例,但它是一个 (n) 整数。您可以通过将“data_class”选项设置为 null 或添加将 a(n) 整数转换为 Niche\SecurityBundle\Entity\BusinessUser 实例的视图转换器来避免此错误。
变压器如下:
<?php
namespace Niche\SecurityBundle\Form\DataTransformer;
use JMS\SecurityExtraBundle\Security\Util\String;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
use Niche\SecurityBundle\Entity\BusinessUser;
class BusinessUserToIdTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
/**
* Transforms an object (BusinessUser) to a string (number)
*
* @param BusinessUser|null $businessUser
* @return String
*/
public function transform($businessUser)
{
if (null === $businessUser) {
return "";
}
return $businessUser->getId();
}
/**
* Transforms a string (number) to an object (BusinessUser).
*
* @param string $number
*
* @return BusinessUser|null
*
* @throws TransformationFailedException if object (BusinessUser) is not found.
*/
public function reverseTransform($id)
{
if (!$id) {
return null;
}
$businessUser = $this->om
->getRepository('NicheSecurityBundle:BusinessUser')
->findOneById($id);
if (null === $businessUser) {
throw new TransformationFailedException(sprintf(
'An issue with number "%s" does not exist!',
$number
));
}
return $businessUser
}
}
我的表单代码是
<?php
namespace Niche\JobBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Niche\SecurityBundle\Form\DataTransformer\BusinessUserToIdTransformer;
class JobType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$entityManager = $options['em'];
$transformer = new BusinessUserToIdTransformer($entityManager);
//Get the BusinessUser object passed in.
$businessUser = $options['businessUser'];
$builder->add('title', 'text');
$builder->add('jobDescription', 'textarea', array(
"label" => "Job Description", )
);
$builder->add('companyDescription', 'textarea', array(
"label" => "Company Description", )
);
$builder->add('salary', 'text');
$builder->add('category', 'entity', array(
'class' => 'NicheJobBundle:Category',
'property' => 'name',
));
$builder->add('responsibilities', 'textarea');
$builder->add('closingDate', 'datetime');
//Add Business User to the form - Need a way for a job to be added by site admin in addition, could just be site admin users logged in with a Business user account
$builder->add(
$builder->create('company', 'hidden', array(
'data' => $businessUser,
))->addViewTransformer($transformer)
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Niche\JobBundle\Entity\Job'
));
$resolver->setRequired(array(
'em', 'businessUser'
));
$resolver->setAllowedTypes(array(
'em' => 'Doctrine\Common\Persistence\ObjectManager',
));
}
public function getName()
{
return 'niche_jobbundle_jobtype';
}
}
该错误消息使我感到困惑,因为它似乎已将 BusinessUser 类转换为整数。我还尝试输入 data_class => null 然后加载表单,但是在提交时我收到一个错误,即该字段为空,尽管在生成表单后隐藏字段中的 ID 正确显示。
有人能指出我正确的方向吗,因为有一种我没有看到的非常简单的感觉。
谢谢
更新:我做了一些更改,因为意识到我应该在控制器中设置公司并将其传递到表单中,这一切似乎都有效,但是当提交表单时,我仍然在数据库中得到 null 或者如果我将字段设置为@Assert\NotBlank() 表单不会提交,因为字段不能为空,即使当我检查源时我可以在隐藏字段中看到 BusinessUser 的 Id。
*更新 - 在下面实施 Coma 的建议时,我终于意识到我哪里出错了 - 它是变压器,它没有返回对象 * - 如果其他人遇到这个问题,我会推荐以下 Coma 的解决方案比必须创建一个要好得多每次都隐藏。如果它是一个单独的解决方案,上面的解决方案已经更新为返回对象并且应该可以正常工作。