执行' INSERT INTO标记(name,task_id)VALUES(?,?)'时发生异常。用params [" qw",null]:

时间:2015-11-27 20:55:30

标签: forms symfony collections insert null

请帮我解决此错误: 我有两个实体,任务标记,在TaskType中假装添加N个标记,但是当我保持生成以下错误时:

  

执行' INSERT INTO标记(name,task_id)VALUES(?,?)'时发生异常。用params [" qw",null]:

     

SQLSTATE [23000]:完整性约束违规:1048列' task_id'不能为空

任务:

class Task
{
/**
  * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

/**
 * @ORM\OneToMany(targetEntity="Tag", mappedBy="task", cascade={"persist", "remove"})
 */
private $tags;

public function __construct() {
    $this->tags = new ArrayCollection();
}

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

/**
 * Set description
 *
 * @param string $description
 *
 * @return Task
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

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

/**
 * Set tags
 *
 * @param string $tags
 *
 * @return Task
 */
public function setTags($tags)
{
    $this->tags = $tags;

    return $this;
}

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

/**
 * Add tag
 *
 * @param \AppBundle\Entity\Tag $tag
 *
 * @return Task
 */
public function addTag(\AppBundle\Entity\Tag $tag)
{
    $this->tags[] = $tag;

    return $this;
}
// public function addTag(Tag $tag)
// {
//     $this->tags->add($tag);
// }

/**
 * Remove tag
 *
 * @param \AppBundle\Entity\Tag $tag
 */
public function removeTag(\AppBundle\Entity\Tag $tag)
{
    $this->tags->removeElement($tag);
}
}

标签:

class Tag
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

/**
 * @ORM\ManyToOne(targetEntity="Task", inversedBy="tags")
 * @ORM\JoinColumn(name="task_id", referencedColumnName="id")
 */
private $task;


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

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

    return $this;
}

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

/**
 * Set task
 *
 * @param \AppBundle\Entity\Task $task
 *
 * @return Tag
 */
public function setTask(\AppBundle\Entity\Task $task = null)
{
    $this->task = $task;

    return $this;
}

/**
 * Get task
 *
 * @return \AppBundle\Entity\Task
 */
public function getTask()
{
    return $this->task;
}
}

任务类型:

class TaskType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('description')            
        ->add('tags', 'collection', array(
            'type'         => new TagType(),
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'by_reference' => false,
        ))
        ->add('submit','submit');
    ;                
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'DeteccionBundle\Entity\Task'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'task';
}
}

控制器:

public function newAction(Request $request)
{
    $task = new Task();

    //SE AGREGAN UN NUMERO DE Tags PARA LA task
    // $tag1 = new Tag();
    // $tag1->setName = 'tag1';
    // $task->getTags()->add($tag1);
    // $tag2 = new Tag();
    // $tag2->setName = 'tag2';
    // $task->getTags()->add($tag2);
    // end dummy code

    $form = $this->createForm(new TaskType(), $task);

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($task);
        $em->flush();

        return $this->redirect($this->generateUrl('cpu_show', array('id' => $entity->getId())));
    }

    return $this->render('DeteccionBundle:Task:new.html.twig', array(
        'task' => $task,
        'form' => $form->createView(),
    ));
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您应该在addTag中自动设置任务...

/**
 * Add tag
 *
 * @param \AppBundle\Entity\Tag $tag
 *
 * @return Task
 */
public function addTag(\AppBundle\Entity\Tag $tag)
{
    $this->tags[] = $tag;
    $tag->setTask($this);

    return $this;
}