Skip to content

Instantly share code, notes, and snippets.

@hpatoio
Created February 19, 2013 11:05
Show Gist options
  • Select an option

  • Save hpatoio/4984915 to your computer and use it in GitHub Desktop.

Select an option

Save hpatoio/4984915 to your computer and use it in GitHub Desktop.
Team entity for OneToMany how to
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Team
*
* @ORM\Table()
* @ORM\Entity
*/
class Team
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* The "one" side is ALWAYS the INVERSE side in a OneToMany bidirectional relation
* See -> http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html#bidirectional-associations
*
* @ORM\OneToMany(targetEntity="Player", mappedBy="playfor", cascade={"persist", "remove"})
*
* You can read this using this "rule"
* One {$class_name} ToMany {targetEntity} referred with {private_attribute_below}
*
* The mappedBy attribute contains the name of the association-field on the owning side.
*
*/
private $players;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* Constructor
*/
public function __construct()
{
$this->players = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __toString()
{
return $this->getName();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Team
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add players
*
* @param \Acme\DemoBundle\Entity\Player $players
* @return Team
*/
public function addPlayer(\Acme\DemoBundle\Entity\Player $players)
{
$this->players[] = $players;
$players->setPlayfor($this);
return $this;
}
/**
* Remove players
*
* @param \Acme\DemoBundle\Entity\Player $players
*/
public function removePlayer(\Acme\DemoBundle\Entity\Player $players)
{
$this->players->removeElement($players);
}
/**
* Get players
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPlayers()
{
return $this->players;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment