简体   繁体   English

Symfony-无法转换属性路径启动日期的值:预期为\\ DateTimeInterface

[英]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.

相关问题 Symfony 4-无法转换属性路径“ [myDateTime]”的值:预期为\\ DateTimeInterface - Symfony 4 - Unable to transform value for property path “[myDateTime]”: Expected a \DateTimeInterface 无法转换属性路径的值:预期\\ DateTime或\\ DateTimeInterface - Unable to transform value for property path : Expected a \DateTime or \DateTimeInterface "Symfony 选择类型数组错误:无法转换属性路径的值:需要一个数组" - Symfony choice type array error: Unable to transform value for property path: Expected an array EasyAdmin:“无法转换属性路径XY的值:需要一个字符串。” - EasyAdmin: “Unable to transform value for property path XY: Expected a string.” 无法转换属性路径“fechaReclamacion”的值:datefmt_format:string''不是数字 - Unable to transform value for property path “fechaReclamacion”: datefmt_format: string '' is not numeric empty_data:无法反转属性路径“ countryOfOrigin”的值:预期为字符串或空 - empty_data: Unable to reverse value for property path “countryOfOrigin”: Expected a string or null Symfony 5:在属性路径“myEntity”中给出的“字符串”、“对象”类型的预期参数 - Symfony 5: Expected argument of type “string”, “object” given at property path “myEntity” 如何设置 `DateTimeInterface` 默认值 - How to set the `DateTimeInterface` default value Symfony 不允许实现 DateTimeInterface - Symfony doesn't allowing to implement DateTimeInterface Symfony:属性路径中给出的“?Doctrine\Common\Collections\Collection”类型的预期参数,“array” - Symfony: Expected argument of type "?Doctrine\Common\Collections\Collection", "array" given at property path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM