Skip to content

Instantly share code, notes, and snippets.

@tobiassjosten
Created March 16, 2012 16:48
Show Gist options
  • Save tobiassjosten/2051055 to your computer and use it in GitHub Desktop.
Save tobiassjosten/2051055 to your computer and use it in GitHub Desktop.
Doctrine2 m2m
<?php
namespace Smartburk\Bundle\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(
* repositoryClass="Smartburk\Bundle\MainBundle\Entity\SeriesRepository"
* )
* @ORM\Table(name="series")
*/
class Series
{
// ...
/**
* @ORM\ManyToMany(
* targetEntity="Smartburk\Bundle\MainBundle\Entity\Series",
* inversedBy="followers"
* )
* @ORM\JoinTable(name="user_following")
*/
protected $following;
/**
* Add following
*
* @param Smartburk\Bundle\MainBundle\Entity\Series $following
*/
public function addFollowing(\Smartburk\Bundle\MainBundle\Entity\Series $following)
{
$this->following[] = $following;
}
/**
* Get following
*
* @return Doctrine\Common\Collections\Collection
*/
public function getFollowing()
{
return $this->following;
}
// ...
}
<?php
namespace Smartburk\Bundle\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
// ...
/**
* @ORM\ManyToMany(
* targetEntity="Smartburk\Bundle\MainBundle\Entity\Series",
* inversedBy="followers"
* )
* @ORM\JoinTable(name="user_following")
*/
protected $following;
/**
* Add following
*
* @param Smartburk\Bundle\MainBundle\Entity\Series $following
*/
public function addFollowing(\Smartburk\Bundle\MainBundle\Entity\Series $following)
{
$this->following[] = $following;
}
/**
* Get following
*
* @return Doctrine\Common\Collections\Collection
*/
public function getFollowing()
{
return $this->following;
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment