Symfony2 & Doctrine – ManyToMany relationship with deleting

I need to create a ManyToMany relationship between two entities. Next I’m trying to delete some rows but it doesn’t work. Could you help me with this?

Tile entity

<?php

namespace AppBundle\Entity;

use AppBundle\Traits\Timestampable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\TileRepository")
 * @ORM\Table(name="tile")
 *
 */
class Tile
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

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

    /**
     * @var string
     * @ORM\Column(type="string", nullable=true)
     */
    protected $start;

    /**
     * @var string
     * @ORM\Column(type="string", nullable=true)
     */
    protected $end;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\User", inversedBy="tiles", cascade={"remove", "persist"})
     * @ORM\JoinTable(name="users_tiles")
     **/
    protected $user;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Set type
     *
     * @param string $type
     *
     * @return Tile
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set start
     *
     * @param string $start
     *
     * @return Tile
     */
    public function setStart($start)
    {
        $this->start = $start;

        return $this;
    }

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

    /**
     * Set end
     *
     * @param string $end
     *
     * @return Tile
     */
    public function setEnd($end)
    {
        $this->end = $end;

        return $this;
    }

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

    /**
     * Add user
     *
     * @param \AppBundle\Entity\User $user
     *
     * @return Tile
     */
    public function addUser(\AppBundle\Entity\User $user)
    {
        $this->user[] = $user;

        return $this;
    }

    /**
     * Remove user
     *
     * @param \AppBundle\Entity\User $user
     */
    public function removeUser(\AppBundle\Entity\User $user)
    {
        $this->user->removeElement($user);
    }

    /**
     * Get user
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getUser()
    {
        return $this->user;
    }
}

User entity

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name=”fos_user”)
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type=”integer”)
* @ORM\GeneratedValue(strategy=”AUTO”)
*/
protected $id;

/**
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Group")
 * @ORM\JoinTable(name="fos_user_user_group",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
 * )
 */
protected $groups;


/**
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Tile", mappedBy="user")
 */
protected $tiles;


/**
 * Add tile
 *
 * @param \AppBundle\Entity\Tile $tile
 *
 * @return User
 */
public function addTile(\AppBundle\Entity\Tile $tile)
{
    $this->tiles[] = $tile;

    return $this;
}

/**
 * Remove tile
 *
 * @param \AppBundle\Entity\Tile $tile
 */
public function removeTile(\AppBundle\Entity\Tile $tile)
{
    $tile->setUser(null);
    $this->tiles->removeElement($tile);
}

/**
 * Get tiles
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getTiles()
{
    return $this->tiles;
}

}

and part of controller class

 $user = $this->getUser();

    foreach($user->getTiles() as $tile) {
        echo $tile->getName();
        $user->removeTile($tile);
    }
    $em->persist($user);
    $em->flush();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s