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

时间:2017-04-18 19:19:16

标签: php symfony symfony-forms

我有一个EventArticleMergedType,其中包含另外两种形式:

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

在我的控制器中,我将此EventArticleMergedType发送到我的视图:

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

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

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

显然我收到了错误:

$form->handleRequest($request);

我的问题是如何获得正确的表格? 或者你有另一个问题的解决方案吗?

我的帖子实体:

<?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;
    }
}

活动实体:

<?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;
    }
}

文章实体:

<?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;
    }
}

这是堆栈跟踪:

[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 个答案:

没有答案