[英]Symfony - Unable to transform value for property path launchdate: Expected a \DateTimeInterface
I'm trying to add a new product with a price containing a launchdate. 我正在尝试添加一个包含启动日期的价格的新产品。 This results into the error in the title.
这导致标题中的错误。
Product.php Product.php
...
public function __construct() {
$this->clients = new ArrayCollection();
$this->priceupdates = new ArrayCollection();
$this->currentPrice = new PriceUpdate();
}
...
ProductController.php ProductController.php
...
public function newAction(Request $request) {
$entity = new Product();
$form = $this->createForm(ProductType::class, $entity);
...
ProductType.php ProductType.php
...
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('description', TextType::class)
->add('ProductPriceUpdate', ProductPriceUpdateType::class ,array(
'data_class' => PriceUpdate::class))
->add('clients', EntityType::class, array(
'class' => 'AppBundle:Client',
'choice_label' => 'naam',
'multiple' => true
))
->add('reset', ResetType::class, array(
'label' => 'Reset',
'attr' => array(
'class' => 'btn btn-danger'
)))
->add('submit', SubmitType::class, array(
'label' => 'Create',
'attr' => array(
'class' => 'btn btn-success'
)));
}
...
ProductPriceUpdateType.php ProductPriceUpdateType.php
class ProductPriceUpdateType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('price', MoneyType::class)
->add('launchdate', DateType::class);
}
...
}
PriceUpdate.php PriceUpdate.php
public function __construct() {
$this->launchdate = \DateTime::createFromFormat('m/d/Y', 'now');
Stacktrace 堆栈跟踪
Symfony\Component\Form\Exception\TransformationFailedException:
Unable to transform value for property path "launchdate": Expected a \DateTimeInterface.
at vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:1107
at Symfony\Component\Form\Form->normToView(false)
(vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:350)
at Symfony\Component\Form\Form->setData(false)
(vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper.php:49)
at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(PriceUpdate), object(RecursiveIteratorIterator))
(vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:383)
at Symfony\Component\Form\Form->setData(object(PriceUpdate))
(vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper.php:49)
at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(Product), object(RecursiveIteratorIterator))
(vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:383)
at Symfony\Component\Form\Form->setData(object(Product))
(vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:487)
at Symfony\Component\Form\Form->initialize()
(vendor\symfony\symfony\src\Symfony\Component\Form\FormBuilder.php:226)
at Symfony\Component\Form\FormBuilder->getForm()
(vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php:30)
at Symfony\Component\Form\FormFactory->create('AppBundle\\Form\\Type\\ProductType', object(Product), array())
(vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait.php:331)
at Symfony\Bundle\FrameworkBundle\Controller\Controller->createForm('AppBundle\\Form\\Type\\ProductType', object(Product))
(src\AppBundle\Controller\ProductController.php:82)
at AppBundle\Controller\ProductController->newAction(object(Request))
at call_user_func_array(array(object(ProductController), 'newAction'), array(object(Request)))
(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:153)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:68)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:169)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(web\app_dev.php:29)
at require('C:\\Users\\rsluimers\\PhpstormProjects\\LunchApp\\LunchApp\\web\\app_dev.php')
(vendor\symfony\symfony\src\Symfony\Bundle\WebServerBundle\Resources\router.php:42)
I don't see anything wrong with it and I don't understand why a \\DateTimeInterface is expected. 我没有发现任何问题,也无法理解为什么需要\\ DateTimeInterface。
It looks like 'now' cannot be used for when datetime is created from format and time()
has to used instead. 看起来当从格式创建datetime而必须使用
time()
时,无法使用“ now”。
PriceHistory.php PriceHistory.php
...
public function __construct() {
$this->activedate = DateTime::createFromFormat('U', time());
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.