Skip to content

Instantly share code, notes, and snippets.

@kslimani
Created September 3, 2015 08:44
Show Gist options
  • Save kslimani/82b236e65e330d584001 to your computer and use it in GitHub Desktop.
Save kslimani/82b236e65e330d584001 to your computer and use it in GitHub Desktop.
Doctrine 2

Doctrine ORM entities

Add in composer.json :

"doctrine/orm": "2.5.*",
"symfony/validator": "2.6.*",
"jms/serializer": "1.0.0"

Many to Many relation

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="Acme\Repository\Foo")
 * @ORM\Table(name="table_foo")
 * @ORM\HasLifecycleCallbacks
 */
class Foo
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @Serializer\Type("integer")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Bar", inversedBy="foos")
     * @ORM\JoinTable(name="table_foo_bar")
     * @Serializer\Exclude
     **/
    private $bars;

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

    /**
     * Add a bar to foo. (many-to-many relation)
     *
     * @param Bar $bar The bar
     */
    public function addBar(Bar $bar)
    {
        if ($this->bars->contains($bar)) {
            return;
        }

        $this->bars->add($bar);
        $bar->addFoo($this);
    }

    /**
     * Remove a bar from foo. (many-to-many relation)
     *
     * @param Bar $bar The bar
     */
    public function removeBar(Bar $bar)
    {
        if (!$this->bars->contains($bar)) {
            return;
        }

        $this->bars->removeElement($bar);
        $bar->removeFoo($this);
    }

    /**
     * Get foo's bars. (many-to-many relation)
     *
     * @return ArrayCollection The bars
     */
    public function getBars()
    {
        return $this->bars;
    }

    public function getId()
    {
        return $this->id;
    }
}

/**
 * @ORM\Entity(repositoryClass="Acme\Repository\Bar")
 * @ORM\Table(name="table_bar")
 * @ORM\HasLifecycleCallbacks
 */
class Bar
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @Serializer\Type("integer")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Foo", mappedBy="bars")
     * @Serializer\Exclude
     **/
    private $foos;

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

    /**
     * Add a foo to foo. (many-to-many relation)
     *
     * @param Foo $foo The foo
     */
    public function addFoo(Foo $foo)
    {
        if ($this->foos->contains($foo)) {
            return;
        }

        $this->foos->add($foo);
        $foo->addBar($this);
    }

    /**
     * Remove a foo from foo. (many-to-many relation)
     *
     * @param Foo $foo The foo
     */
    public function removeFoo(Foo $foo)
    {
        if (!$this->foos->contains($foo)) {
            return;
        }

        $this->foos->removeElement($foo);
        $foo->removeBar($this);
    }

    /**
     * Get foo's foos. (many-to-many relation)
     *
     * @return ArrayCollection The foos
     */
    public function getFoos()
    {
        return $this->foos;
    }

    public function getId()
    {
        return $this->id;
    }
}

One to Many relation

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="Acme\Repository\Foo")
 * @ORM\Table(name="table_foo")
 * @ORM\HasLifecycleCallbacks
 */
class Foo
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @Serializer\Type("integer")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Zor", mappedBy="foo")
     * @Serializer\Exclude
     */
    private $zors;

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

    /**
     * Get foo's zors. (one-to-many relation)
     *
     * @return ArrayCollection The zors
     */
    public function getZors()
    {
        return $this->zors;
    }

    public function getId()
    {
        return $this->id;
    }
}

/**
 * @ORM\Entity(repositoryClass="Acme\Repository\ZorRepository")
 * @ORM\Table(name="table_zor")
 * @ORM\HasLifecycleCallbacks
 */
class Zor
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @Serializer\Type("integer")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Foo", inversedBy="zors")
     * @ORM\JoinColumn(name="foo_id", referencedColumnName="id")
     * @Serializer\Exclude
     **/
    private $foo;

    /**
     * Get the linked foo. (many-to-one relation)
     *
     * @return Foo The foo
     */
    public function getFoo()
    {
        return $this->foo;
    }

    /**
     * Set the linked foo. (many-to-one relation)
     *
     * @param Foo $foo The foo
     *
     * @return Zor This zor
     */
    public function setFoo(Foo $foo)
    {
        $this->foo = $foo;
        $this->fooId = $foo->getId();

        return $this;
    }

    public function getId()
    {
        return $this->id;
    }
}

Doctrine 2 Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment