Symfony - 无法转换 属性 路径启动日期的值:需要一个 \DateTimeInterface
Symfony - Unable to transform value for property path launchdate: Expected a \DateTimeInterface
我正在尝试添加价格包含发布日期的新产品。
这导致标题中出现错误。
Product.php
...
public function __construct() {
$this->clients = new ArrayCollection();
$this->priceupdates = new ArrayCollection();
$this->currentPrice = new PriceUpdate();
}
...
ProductController.php
...
public function newAction(Request $request) {
$entity = new Product();
$form = $this->createForm(ProductType::class, $entity);
...
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
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
public function __construct() {
$this->launchdate = \DateTime::createFromFormat('m/d/Y', 'now');
堆栈跟踪
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)
我没有发现任何问题,我不明白为什么需要 \DateTimeInterface。
看起来 'now' 不能用于从格式创建日期时间并且必须使用 time()
代替。
PriceHistory.php
...
public function __construct() {
$this->activedate = DateTime::createFromFormat('U', time());
...
我正在尝试添加价格包含发布日期的新产品。 这导致标题中出现错误。
Product.php
...
public function __construct() {
$this->clients = new ArrayCollection();
$this->priceupdates = new ArrayCollection();
$this->currentPrice = new PriceUpdate();
}
...
ProductController.php
...
public function newAction(Request $request) {
$entity = new Product();
$form = $this->createForm(ProductType::class, $entity);
...
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
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
public function __construct() {
$this->launchdate = \DateTime::createFromFormat('m/d/Y', 'now');
堆栈跟踪
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)
我没有发现任何问题,我不明白为什么需要 \DateTimeInterface。
看起来 'now' 不能用于从格式创建日期时间并且必须使用 time()
代替。
PriceHistory.php
...
public function __construct() {
$this->activedate = DateTime::createFromFormat('U', time());
...