坚持拥有关系的实体并形成数据

时间:2015-12-29 22:12:46

标签: symfony doctrine-orm

我有AppBundle \ Entity \ Comments实体,我希望在用户填写表单时将其保留到数据库中我找不到确切如何将表单数据保存到数据库的信息:

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="Comments")
 */

class Comments
{
        /**
        * @ORM\Column(type="integer")
        * @ORM\Id
        * @ORM\GeneratedValue(strategy="AUTO")
        */
        protected $id;

        /**
        * @ORM\ManyToOne(targetEntity="Employer")
        * @ORM\JoinColumn(name="employer_id", referencedColumnName="id")
        */
        private $employer;

        /**
        * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User")
        * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
        */
        private $company;

        /**
        * @ORM\ManyToOne(targetEntity="Commenttypes")
        * @ORM\JoinColumn(name="comment_id", referencedColumnName="id")
        */
        private $comment;

        /**
        * @ORM\Column(type="date")
        */
        private $from_date;

        /**
        * @ORM\Column(type="date")
        */
        private $to_date;
?>

以下是表格类型:

<?
namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class WorkerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('comment', 'entity', array(
                'class' => 'AppBundle\Entity\CommentTypes',
                'label' => 'Comment:'
                ))
            ->add('from_date', 'Symfony\Component\Form\Extension\Core\Type\DateType', array(
                'label'  => 'From date:',
                'input'  => 'datetime',
                'widget' => 'single_text',
                'format' => 'dd-MM-yyyy',
                'attr'   => [ 'class' => 'datepicker' ]
                ))
            ->add('to_date', 'Symfony\Component\Form\Extension\Core\Type\DateType', array(
                'label'  => 'To date:',
                'input'  => 'datetime',
                'widget' => 'single_text',
                'format' => 'dd-MM-yyyy',
                'attr'   => [ 'class' => 'datepicker' ]
                ))
            ->add('submit', 'Symfony\Component\Form\Extension\Core\Type\SubmitType', array(
                'label'  => 'Submit',
                'attr'   => [ 'class' => 'buttonColor' ]
                ));
    }

    public function configureOptions(OptionsResolver $resolver)
        {
                $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\Comments',
        ));
        }
}
?>

所以到现在为止,我收到了来自表单的字段$ from_date和$ to_date以及$ comment(来自下拉列表的评论的ID)的数据,我需要在控制器中继续保存注释对象到数据库。这是我的控制器:

<?php
namespace AppBundle\Controller;

use AppBundle\Entity\Comments;
use AppBundle\Form\Type\WorkerType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class Worker extends Controller
{
    /**
     * @Route("/worker/{id}", name="worker")
     */
    public function indexAction($id, Request $request)
    {       

        $comment = new Comments();

        $form = $this->createForm('AppBundle\Form\Type\WorkerType', $comment);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {          

        {{{{ CODE HERE }}}}

        }

        return $this->render('default/worker.html.twig',array(
        'form' => $form->createView()
        ));
    }
}

我确实想知道如果表单有效,我需要做什么来保留评论对象,我正在尝试这个:

if ($form->isSubmitted() && $form->isValid()) {
$comment->setEmployer($id);
$comment->setCompany($this->getUser());
$comment->setComment($form->get('comment')->getData());
$comment->setfrom_date($form->get('from_date')->getData());
$comment->setto_date($form->get('to_date')->getData());
}

但是我收到了这个错误:

Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\Comments::setEmployer() must be an instance of AppBundle\Entity\Employer, string given, called in /home1/rabotnici/src/AppBundle/Controller/Worker.php on line 42 and defined 

0 个答案:

没有答案