Created
October 23, 2011 14:22
-
-
Save Ocramius/1307410 to your computer and use it in GitHub Desktop.
Doctrine 2 @onetomany @manytoone example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace HelloWorld; | |
use InvalidArgumentException; | |
/** | |
* This class is somewhere in your library | |
* @Entity | |
* @Table(name="users") | |
*/ | |
class User { | |
/** | |
* @var int | |
* @Id | |
* @Column(type="integer",name="id", nullable=false) | |
* @GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @var string | |
* @Column(type="string", length=255, name="login", nullable=false) | |
*/ | |
protected $login; | |
/** | |
* @var UserGroup|null the group this user belongs (if any) | |
* @ManyToOne(targetEntity="HelloWorld\UserGroup", inversedBy="users") | |
* @JoinColumn(name="group_id", referencedColumnName="id") | |
*/ | |
protected $group; | |
/** | |
* @param string $login | |
*/ | |
public function __construct($login) { | |
$this->setLogin($login); | |
} | |
/** | |
* @return string | |
*/ | |
public function getLogin() { | |
return $this->login; | |
} | |
/** | |
* @param string $login | |
*/ | |
public function setLogin($login) { | |
$this->login = (string) $login; | |
} | |
/** | |
* @return HelloWorld\UserGroup|null | |
*/ | |
public function getGroup() { | |
return $this->group; | |
} | |
/** | |
* Sets a new user group and cleans the previous one if set | |
* @param null|HelloWorld\UserGroup $group | |
*/ | |
public function setGroup($group) { | |
if($group === null) { | |
if($this->group !== null) { | |
$this->group->getUsers()->removeElement($this); | |
} | |
$this->group = null; | |
} else { | |
if(!$group instanceof HelloWorld\UserGroup) { | |
throw new InvalidArgumentException('$group must be null or instance of HelloWorld\UserGroup'); | |
} | |
if($this->group !== null) { | |
$this->group->getUsers()->removeElement($this); | |
} | |
$this->group = $group; | |
$group->getUsers()->add($this); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace HelloWorld; | |
use Doctrine\Common\Collections\Collection, | |
Doctrine\Common\Collections\ArrayCollection; | |
/** | |
* This class is somewhere in your library | |
* @Entity | |
* @Table(name="usergroups") | |
*/ | |
class UserGroup { | |
/** | |
* @var int | |
* @Id | |
* @Column(type="integer",name="id", nullable=false) | |
* @GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @var string | |
* @Column(type="string", length=255, name="name", nullable=false) | |
*/ | |
protected $name; | |
/** | |
* @var Collection | |
* @OneToMany(targetEntity="HelloWorld\User", mappedBy="group") | |
*/ | |
protected $users; | |
/** | |
* @param string $name | |
*/ | |
public function __construct($name) { | |
$this->users = new ArrayCollection(); | |
$this->setName($name); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() { | |
return $this->name; | |
} | |
/** | |
* @var string $name | |
*/ | |
public function setName($name) { | |
$this->name = (string) $name; | |
} | |
/** | |
* @return Collection | |
*/ | |
public function getUsers() { | |
return $this->users; | |
} | |
} |
what would be cli command to generate this classes from database
go to your project folder into the console and type this:
vendor/bin/doctrine orm:convert-mapping --from-database --force xml "path/to/yourxml/mapping"
this will generate the xml files
then on same folder in the console:
generate the entities with annotations
vendor\bin\doctrine orm:generate-entities --generate-annotations=true .\src\repository\entities
Example is not very good, because in real life User to UserGroup should be many to many relation
Example is not very good, because in real life User to UserGroup should be many to many relation
true :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is major flaw in this example. It is not symmetric