douhong4452 2017-04-18 19:19
浏览 41

Symfony 2.8在控制器中获取正确的表格

I have a EventArticleMergedType which contains 2 others forms :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('event', EventType::class)
    ->add('article', ArticleType::class);
}

In my controller I send this EventArticleMergedType to my view :

$article = new Article();
$event = new Event();

$formData['article'] = $article;
$formData['event'] = $event;

$form = $this->createForm('AppBundle\Form\EventArticleMergedType', $formData);

Obviously I get an error at :

$form->handleRequest($request);

My question is how do i get the right Form ? Or do you have another solution to my problem ?

My Post Entity :

<?php

namespace AppBundle\Entity;

use AppBundle\Entity\Image;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"event" = "Event", "article" = "Article"})
 * @ORM\Table(name="posts")
 */
abstract class Post
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;

    /**
     * @var string
     *
     * @ORM\Column(name="text", type="text")
     */
    protected $text;

    /**
     * @var DateTime
     *
     * @ORM\Column(name="createdAt", type="datetime")
     */
    protected $createdAt;

    /**
     * @ORM\OneToOne(targetEntity="Image", cascade={"persist", "remove"})
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    protected $featuredImage;

    /**
     * @var UploadedFile
     */
    protected $file;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Post
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set text
     *
     * @param string $text
     *
     * @return Post
     */
    public function setText($text)
    {
        $this->text = $text;

        return $this;
    }

    /**
     * Get text
     *
     * @return string
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * Set createdAt
     *
     * @param DateTime $createdAt
     * @return Post
     */
    public function setCreatedAt(DateTime $createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set Featured Image
     * @param Image $featuredImage
     *
     * @return Post
     */
    public function setFeaturedImage(Image $featuredImage)
    {
        $this->featuredImage = $featuredImage;

        return $this;
    }

    /**
     * Get Featured Image
     *
     * @return Image
     */
    public function getFeaturedImage()
    {
        return $this->featuredImage;
    }

    /**
     * Set File
     * @param UploadedFile $file
     *
     * @return Post
     */
    public function setFile(UploadedFile $file)
    {
        $this->file = $file;

        return $this;
    }

    /**
     * Get File
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }
}

Event Entity :

<?php

namespace AppBundle\Entity;

use AppBundle\Entity\Image;
use AppBundle\Entity\Post;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Event
 *
 * @ORM\Entity
 */
class Event extends Post
{

    /**
     * @var DateTime
     *
     * @ORM\Column(name="eventDate", type="datetime")
     */
    private $eventDate;

    /**
     * Set eventDate
     *
     * @param DateTime $eventDate
     * @return Gallery
     */
    public function setEventDate(DateTime $eventDate)
    {
        $this->eventDate = $eventDate;

        return $this;
    }

    /**
     * Get eventDate
     *
     * @return DateTime
     */
    public function getEventDate()
    {
        return $this->eventDate;
    }
}

And Article Entity :

<?php

namespace AppBundle\Entity;

use AppBundle\Entity\Image;
use AppBundle\Entity\Post;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Event
 *
 * @ORM\Entity
 */
class Article extends Post
{

    /**
     * @var string
     *
     * @ORM\Column(name="author", type="string", length=255)
     */
    private $author;

    /**
     * Set author
     *
     * @param string $author
     *
     * @return Article
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get author
     *
     * @return string
     */
    public function getAuthor()
    {
        return $this->author;
    }
}

Here is the stack trace :

[1] Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: Expected argument of type "DateTime", "NULL" given
    at n/a
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 254

    at Symfony\Component\PropertyAccess\PropertyAccessor::throwInvalidArgumentException('Argument 1 passed to AppBundle\Entity\Post::setCreatedAt() must be an instance of DateTime, null given, called in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 605 and defined', array(array('file' => '/var/www/filadelfia/src/AppBundle/Entity/Post.php', 'line' => '124', 'function' => 'handleError', 'class' => 'Symfony\Component\PropertyAccess\PropertyAccessor', 'type' => '::', 'args' => array('4096', 'Argument 1 passed to AppBundle\Entity\Post::setCreatedAt() must be an instance of DateTime, null given, called in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 605 and defined', '/var/www/filadelfia/src/AppBundle/Entity/Post.php', '124', array('this' => object(Article)))), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php', 'line' => '605', 'function' => 'setCreatedAt', 'class' => 'AppBundle\Entity\Post', 'type' => '->', 'args' => array(null)), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php', 'line' => '201', 'function' => 'writeProperty', 'class' => 'Symfony\Component\PropertyAccess\PropertyAccessor', 'type' => '->', 'args' => array(array(object(Article), object(Article)), 'createdAt', null)), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php', 'line' => '93', 'function' => 'setValue', 'class' => 'Symfony\Component\PropertyAccess\PropertyAccessor', 'type' => '->', 'args' => array(object(Article), object(PropertyPath), null)), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php', 'line' => '623', 'function' => 'mapFormsToData', 'class' => 'Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper', 'type' => '->', 'args' => array(object(RecursiveIteratorIterator), object(Article))), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php', 'line' => '567', 'function' => 'submit', 'class' => 'Symfony\Component\Form\Form', 'type' => '->', 'args' => array(array('post' => array('name' => 'dfdfdfd', 'text' => 'dfdfdfd', 'file' => object(UploadedFile)), 'author' => 'dfdfdf'), true)), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php', 'line' => '113', 'function' => 'submit', 'class' => 'Symfony\Component\Form\Form', 'type' => '->', 'args' => array(array('article' => array('post' => array('name' => 'dfdfdfd', 'text' => 'dfdfdfd', 'file' => object(UploadedFile)), 'author' => 'dfdfdf'), 'event' => array('eventDate' => array('date' => array('year' => '2012', 'month' => '1', 'day' => '1'), 'time' => array('hour' => '0', 'minute' => '0')))), true)), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php', 'line' => '489', 'function' => 'handleRequest', 'class' => 'Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler', 'type' => '->', 'args' => array(object(Form), object(Request))), array('file' => '/var/www/filadelfia/src/AppBundle/Controller/Admin/PostAdminController.php', 'line' => '58', 'function' => 'handleRequest', 'class' => 'Symfony\Component\Form\Form', 'type' => '->', 'args' => array(object(Request))), array('function' => 'newAction', 'class' => 'AppBundle\Controller\Admin\PostAdminController', 'type' => '->', 'args' => array(object(Request))), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php', 'line' => '144', 'function' => 'call_user_func_array', 'args' => array(array(object(PostAdminController), 'newAction'), array(object(Request)))), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php', 'line' => '64', 'function' => 'handleRaw', 'class' => 'Symfony\Component\HttpKernel\HttpKernel', 'type' => '->', 'args' => array(object(Request), '1')), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php', 'line' => '69', 'function' => 'handle', 'class' => 'Symfony\Component\HttpKernel\HttpKernel', 'type' => '->', 'args' => array(object(Request), '1', true)), array('file' => '/var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php', 'line' => '185', 'function' => 'handle', 'class' => 'Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel', 'type' => '->', 'args' => array(object(Request), '1', true)), array('file' => '/var/www/filadelfia/web/app_dev.php', 'line' => '20', 'function' => 'handle', 'class' => 'Symfony\Component\HttpKernel\Kernel', 'type' => '->', 'args' => array(object(Request)))), '1')
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 240

    at Symfony\Component\PropertyAccess\PropertyAccessor::handleError('4096', 'Argument 1 passed to AppBundle\Entity\Post::setCreatedAt() must be an instance of DateTime, null given, called in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 605 and defined', '/var/www/filadelfia/src/AppBundle/Entity/Post.php', '124', array('this' => object(Article)))
        in /var/www/filadelfia/src/AppBundle/Entity/Post.php line 124

    at AppBundle\Entity\Post->setCreatedAt(null)
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 605

    at Symfony\Component\PropertyAccess\PropertyAccessor->writeProperty(array(object(Article), object(Article)), 'createdAt', null)
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 201

    at Symfony\Component\PropertyAccess\PropertyAccessor->setValue(object(Article), object(PropertyPath), null)
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 93

    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapFormsToData(object(RecursiveIteratorIterator), object(Article))
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 623

    at Symfony\Component\Form\Form->submit(array('post' => array('name' => 'dfdfdfd', 'text' => 'dfdfdfd', 'file' => object(UploadedFile)), 'author' => 'dfdfdf'), true)
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 567

    at Symfony\Component\Form\Form->submit(array('article' => array('post' => array('name' => 'dfdfdfd', 'text' => 'dfdfdfd', 'file' => object(UploadedFile)), 'author' => 'dfdfdf'), 'event' => array('eventDate' => array('date' => array('year' => '2012', 'month' => '1', 'day' => '1'), 'time' => array('hour' => '0', 'minute' => '0')))), true)
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php line 113

    at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest(object(Form), object(Request))
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 489

    at Symfony\Component\Form\Form->handleRequest(object(Request))
        in /var/www/filadelfia/src/AppBundle/Controller/Admin/PostAdminController.php line 58

    at AppBundle\Controller\Admin\PostAdminController->newAction(object(Request))
        in  line 

    at call_user_func_array(array(object(PostAdminController), 'newAction'), array(object(Request)))
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 144

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 64

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php line 69

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in /var/www/filadelfia/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 185

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in /var/www/filadelfia/web/app_dev.php line 20
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题