2017-12-07 3 views
0

Ich versuche, ein neues Produkt mit einem Preis hinzuzufügen, der ein Startdatum enthält. Dies führt zu dem Fehler im Titel.Symfony3 - Wert für Eigenschaftspfad konnte nicht umgewandelt werden Startdatum: Erwartet a DateTimeInterface

product.php

... 
public function __construct() { 
    $this->clients = new ArrayCollection(); 
    $this->pricehistories = new ArrayCollection(); 
    $this->currentPrice = new PriceHistory(); 
} 
... 

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('ProductPriceHistory', ProductPriceHistoryType::class ,array(
     'data_class' => PriceHistory::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' 
     ))); 
} 
... 

ProductPriceHistoryType.php

class ProductPriceHistoryType extends AbstractType { 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
     ->add('price', MoneyType::class) 
     ->add('startdate', DateType::class); 
    } 
... 
} 

PriceHistory.php

public function __construct() { 
     $this->startdate = \DateTime::createFromFormat('m/d/Y', 'now'); 

Stacktrace

Symfony\Component\Form\Exception\TransformationFailedException: 
Unable to transform value for property path "startdate": 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(PriceHistory), object(RecursiveIteratorIterator)) 
    (vendor\symfony\symfony\src\Symfony\Component\Form\Form.php:383) 
    at Symfony\Component\Form\Form->setData(object(PriceHistory)) 
    (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) 

Ich sehe nichts falsch mit ihm, und ich verstehe nicht, warum ein \ DateTimeInterface wird erwartet.

+0

Welche Codezeile den Fehler auslöst? –

+0

Wer weiß? Ich bin gezwungen PHP/Symfony zu benutzen. – rmsluimers

+0

Warum verwenden Sie '$ this-> startdate = new \ DateTime();'? –

Antwort

0

Ich bin nicht sicher, warum, aber das funktioniert:

PriceHistory.php

... 
public function __construct() { 
    $this->startdatum = new \DateTime(); 
    $this->startdatum->format('m/d/Y'); 
... 
+0

Ja, das wirft keinen Fehler und tut .... nichts, da es eine Zeichenfolge zurückgibt, die die aktuelle Zeit enthält, die als 'm/d/Y' formatiert ist –

Verwandte Themen