Created
December 2, 2012 21:54
-
-
Save basz/4191233 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| $user = new \SndAdmin\Entity\User(); | |
| $role = new \SndAdmin\Entity\UserRole(); | |
| $role->setRole('loser'); | |
| $user->setRole($role); | |
| $em->persist($user); | |
| $em->flush(); | |
| I would expect UserRole to have user_id set and (pointing to the user that was created and saved to the database, but the user_id field is null, as var_dumps shows. | |
| object(SndAdmin\Entity\User)[603] | |
| protected 'role' => | |
| object(SndAdmin\Entity\UserRole)[602] | |
| private 'id' => null | |
| private 'user' => null | |
| private 'role' => string 'loser' (length=5) | |
| private 'created' => null | |
| private 'updated' => null | |
| protected 'id' => null | |
| protected 'username' => null | |
| protected 'email' => string '0b9b6d6d154e98ce34b3f2e4ef76eae9@e3844e186e6eb8736e9f53c0c5889527.nl' (length=68) | |
| protected 'displayName' => null | |
| protected 'password' => string 'b139e104214a08ae3f2ebcce149cdf6e' (length=32) | |
| <?php | |
| namespace SndAdmin\Entity; | |
| use Doctrine\ORM\Mapping as ORM; | |
| use Gedmo\Mapping\Annotation as Gedmo; | |
| use ZfcUser\Entity\UserInterface; | |
| /** | |
| * @ORM\Entity | |
| * @ORM\Table(name="user") | |
| */ | |
| class User extends \ZfcUserDoctrineORM\Entity\User | |
| { | |
| /** | |
| * @ORM\OneToOne(targetEntity="SndAdmin\Entity\UserRole", mappedBy="user", cascade={"persist", "remove", "merge"}, orphanRemoval=true) | |
| * | |
| * @var \SndAdmin\Entity\UserRole $role | |
| */ | |
| protected $role; | |
| /** | |
| * @Gedmo\Timestampable(on="create") | |
| * @ORM\Column(type="datetime") | |
| */ | |
| private $created; | |
| /** | |
| * @Gedmo\Timestampable(on="update") | |
| * @ORM\Column(type="datetime") | |
| */ | |
| private $updated; | |
| /** | |
| * @param \SndAdmin\Entity\UserRole $role | |
| */ | |
| public function setRole($role) | |
| { | |
| $this->role = $role; | |
| } | |
| /** | |
| * @return \SndAdmin\Entity\UserRole | |
| */ | |
| public function getRole() | |
| { | |
| return $this->role; | |
| } | |
| public function setCreated($created) | |
| { | |
| $this->created = $created; | |
| } | |
| public function getCreated() | |
| { | |
| return $this->created; | |
| } | |
| public function setUpdated($updated) | |
| { | |
| $this->updated = $updated; | |
| } | |
| public function getUpdated() | |
| { | |
| return $this->updated; | |
| } | |
| } | |
| ======================================= | |
| <?php | |
| namespace SndAdmin\Entity; | |
| use Doctrine\ORM\Mapping as ORM; | |
| /** | |
| * @ORM\Entity | |
| * @ORM\Table(name="user_role_linker") | |
| */ | |
| class UserRole | |
| { | |
| /** | |
| * @ORM\Id | |
| * @ORM\Column(name="id",type="integer"); | |
| * @ORM\GeneratedValue(strategy="AUTO") | |
| * | |
| * @var integer $id | |
| */ | |
| private $id; | |
| /** | |
| * @ORM\OneToOne(targetEntity="SndAdmin\Entity\User", inversedBy="role", cascade={"persist", "remove", "merge"}, orphanRemoval=true) | |
| * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id") | |
| * | |
| * @var \SndAdmin\Entity\User | |
| */ | |
| private $user; | |
| /** | |
| * @ORM\Column(name="role_id",type="string",length=255); | |
| * | |
| * @var string $role | |
| */ | |
| private $role; | |
| /** | |
| * @param int $id | |
| */ | |
| public function setId($id) | |
| { | |
| $this->id = $id; | |
| } | |
| /** | |
| * @return int | |
| */ | |
| public function getId() | |
| { | |
| return $this->id; | |
| } | |
| /** | |
| * @param string $role | |
| */ | |
| public function setRole($role) | |
| { | |
| $this->role = $role; | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getRole() | |
| { | |
| return $this->role; | |
| } | |
| /** | |
| * @param \SndAdmin\Entity\User $user | |
| */ | |
| public function setUser($user) | |
| { | |
| $this->user = $user; | |
| } | |
| /** | |
| * @return \SndAdmin\Entity\User | |
| */ | |
| public function getUser() | |
| { | |
| return $this->user; | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-- Table structure for table
user_role_linkerCREATE TABLE
user_role_linker(idint(11) NOT NULL AUTO_INCREMENT,user_idint(11) DEFAULT NULL,role_idvarchar(255) COLLATE utf8_unicode_ci NOT NULL,PRIMARY KEY (
id),UNIQUE KEY
UNIQ_61117899A76ED395(user_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- Dumping data for table
user_role_linkerINSERT INTO
user_role_linkerVALUES(6, NULL, 'loser');
-- Constraints for dumped tables
-- Constraints for table
user_role_linkerALTER TABLE
user_role_linkerADD CONSTRAINT
FK_61117899A76ED395FOREIGN KEY (user_id) REFERENCESuser(user_id);