-
-
Save Ocramius/1307410 to your computer and use it in GitHub Desktop.
<?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; | |
} | |
} |
Thanks. But how i allow users to join in several groups? My question is about @manytomany annotation
@SirCameronMcAlpine, When the error has been occurred? On migrate or on insert?
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
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 :)
I follow these guidelines but keep getting the error that the key linking the tables cannot be null!