Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Created October 23, 2011 14:22
Show Gist options
  • 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;
}
}
@SirCameronMcAlpine
Copy link

I follow these guidelines but keep getting the error that the key linking the tables cannot be null!

@Lavaei
Copy link

Lavaei commented Mar 16, 2015

Thanks. But how i allow users to join in several groups? My question is about @manytomany annotation

@Lavaei
Copy link

Lavaei commented Mar 16, 2015

@SirCameronMcAlpine, When the error has been occurred? On migrate or on insert?

@mleko
Copy link

mleko commented Sep 8, 2015

There is major flaw in this example. It is not symmetric

 <?php
$group = new UserGroup('name');

$user1 = new User('login');
$user1->setGroup($group);
$g1 = $user1->getGroup(); // UserGroup object

$user2 = new User('login2');
$group->getUsers()->add($user2);
$g2 = $user2->getGroup(); // null

@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