如何在不更改数据库中现有行的情况下编辑多对多关系?

时间:2018-04-12 07:32:22

标签: symfony doctrine

我与连接表有很多关系。当我想编辑现有任务时。我想删除与旧任务的关系并创建一个新任务。

我试图删除Doctrine数组集合中的关系。有没有更好的方法来做到这一点,还是我犯了错误?

这是我的任务回购:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Task
 *
 * @ORM\Table(name="task")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\TaskRepository")
 */
class Task
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

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

/**
 * @var boolean
 *
 * @ORM\Column(name="standart_task", type="boolean")
 */
private $standartTask = true;

/**
 * @var integer
 *
 * @ORM\Column(name="alert", type="integer")
 */
private $alert;

/**
 * @var Project
 *
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Project", mappedBy="tasks")
 */
private $projects;

/**
 * @var TasksToPeriods
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\TasksToPeriods", mappedBy="task")
 */
private $taskToPeriod;

/**
 * @var User
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="tasks")
 * @ORM\JoinColumn(name="user", referencedColumnName="id")
 */
private $user;
/**
 * @var boolean
 *
 * @ORM\Column(name="enabled", type="boolean")
 */
private $enabled = 1;

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

/**
 * @return User
 */
public function getUser()
{
    return $this->user;
}

/**
 * @param User $user
 */
public function setUser($user)
{
    $this->user = $user;
}

/**
 * @return Project
 */
public function getProjects()
{
    return $this->projects;
}

/**
 * @param Project $projects
 */
public function setProjects($projects)
{
    $this->projects = $projects;
}

/**
 * @return string
 */
public function getSubject()
{
    return $this->subject;
}

/**
 * @param string $subject
 */
public function setSubject($subject)
{
    $this->subject = $subject;
}

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

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

/**
 * @return bool
 */
public function isStandartTask()
{
    return $this->standartTask;
}

/**
 * @param bool $standartTask
 */
public function setStandartTask($standartTask)
{
    $this->standartTask = $standartTask;
}

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

/**
 * @return int
 */
public function getAlert()
{
    return $this->alert;
}

/**
 * @param int $alert
 */
public function setAlert($alert)
{
    $this->alert = $alert;
}

/**
 * @return TasksToPeriods
 */
public function getTaskToPeriod()
{
    return $this->taskToPeriod;
}

/**
 * @param TasksToPeriods $taskToPeriod
 */
public function setTaskToPeriod($taskToPeriod)
{
    $this->taskToPeriod = $taskToPeriod;
}

/**
 * @return bool
 */
public function isEnabled()
{
    return $this->enabled;
}

/**
 * @param bool $enabled
 */
public function setEnabled($enabled)
{
    $this->enabled = $enabled;
}

/**
 * @param Project $project
 */
public function addProject(Project $project)
{
    $this->projects[] = $project;
}

/**
 * @param Project $project
 */
public function deleteProject(Project $project)
{
    $this->projects->removeElement($project);
}

}

这是我的控制器

public function editExistingTask(Request $request, Task $task, Project $project)
{
    $form = $this->createForm(TaskType::class, $task);
    $form->remove('standartTask');
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $task->deleteProject($project);
        $project->removeTask($task);
        $this->em->persist($task);
        $this->em->persist($project);


        $this->em->flush();
        return new Response('happy');
    }

    return $this->render('task/edit.html.twig', [
        'form' => $form->createView()
        ]);
}

1 个答案:

答案 0 :(得分:0)

我不确定你想要做什么。

@ORM\ManyToMany(targetEntity="AppBundle\Entity\Project", mappedBy="tasks", cascade={"persist"})