Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Created October 23, 2011 14:22
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save Ocramius/1307410 to your computer and use it in GitHub Desktop.
Save Ocramius/1307410 to your computer and use it in GitHub Desktop.
Doctrine 2 @OneToMany @manytoone example
<?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);
}
}
}
<?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;
}
}
@uniqode
Copy link

uniqode commented Dec 10, 2015

what would be cli command to generate this classes from database

@sebaquevedo
Copy link

sebaquevedo commented Dec 7, 2017

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

@wloczynutka
Copy link

Example is not very good, because in real life User to UserGroup should be many to many relation

@cybernet
Copy link

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