Instantly share code, notes, and snippets.
Created
May 20, 2011 23:05
-
Star
11
(11)
You must be signed in to star a gist -
Fork
5
(5)
You must be signed in to fork a gist
-
Save Problematic/983982 to your computer and use it in GitHub Desktop.
Working example of Symfony2 entities with Class Table Inheritance
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
<?php | |
namespace UP\SocialBundle\Entity; | |
/** | |
* UP\SocialBundle\Entity\SocialBase | |
* @orm:Entity | |
* @orm:HasLifecycleCallbacks | |
* @orm:InheritanceType("JOINED") | |
* @orm:DiscriminatorColumn(name="class_name", type="string") | |
* @orm:DiscriminatorMap({ | |
* "UP\SocialBundle\Entity\Friendship" = "UP\SocialBundle\Entity\Friendship", | |
* "UP\SocialBundle\Entity\SocialFeedItem" = "UP\SocialBundle\Entity\SocialFeedItem" | |
* }) | |
*/ | |
abstract class SocialBase { | |
/** | |
* @orm:Id | |
* @orm:Column(type="integer") | |
* @orm:GeneratedValue | |
*/ | |
protected $id; | |
/** | |
* @orm:Column(type="datetime") | |
*/ | |
protected $created_at; | |
/** | |
* @orm:Column(type="datetime") | |
*/ | |
protected $updated_at; | |
/** | |
* @orm:ManyToMany(targetEntity="UP\SocialBundle\Entity\Tag") | |
* @orm:JoinTable(name="social_tags", | |
* joinColumns={@orm:JoinColumn(name="social_id", referencedColumnName="id")}, | |
* inverseJoinColumns={@orm:JoinColumn(name="tag_id", referencedColumnName="id")} | |
* ) | |
*/ | |
protected $tags; | |
/** | |
* @orm:OneToMany(targetEntity="UP\SocialBundle\Entity\Comment", mappedBy="root") | |
*/ | |
protected $comments; | |
public function __construct() | |
{ | |
$this->tags = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function addTag(\UP\SocialBundle\Entity\Tag $tag) | |
{ | |
$this->tags[] = $tag; | |
} | |
public function getTags() | |
{ | |
return $this->tags; | |
} | |
public function addComment(\UP\SocialBundle\Entity\Comment $comment) | |
{ | |
$this->comments[] = $comment; | |
} | |
public function getComments() | |
{ | |
return $this->comments; | |
} | |
public function setCreatedAt(\DateTime $created_at) { | |
$this->created_at = $created_at; | |
} | |
public function getCreatedAt() { | |
return $this->created_at; | |
} | |
public function setUpdatedAt(\DateTime $updated_at) { | |
$this->updated_at = $updated_at; | |
} | |
public function getUpdatedAt() { | |
return $this->updated_at; | |
} | |
/** | |
* @orm:prePersist | |
*/ | |
public function prePersist() { | |
$this->setCreatedAt(new \DateTime()); | |
$this->setUpdatedAt(new \DateTime()); | |
} | |
/** | |
* @orm:preUpdate | |
*/ | |
public function preUpdate() { | |
$this->setUpdatedAt(new \DateTime()); | |
} | |
} |
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
<?php | |
namespace UP\SocialBundle\Entity; | |
use UP\SocialBundle\Entity\SocialBase; | |
/** | |
* UP\SocialBundle\Entity\SocialFeedItem | |
* @orm:Entity | |
*/ | |
class SocialFeedItem extends SocialBase | |
{ | |
/** | |
* @orm:Column(type="string", nullable="true") | |
*/ | |
protected $user_message; | |
/** | |
* @orm:Column(type="string") | |
*/ | |
protected $system_message; | |
/** | |
* @orm:ManyToOne(targetEntity="UP\UserBundle\Entity\User", inversedBy="id") | |
*/ | |
protected $source_user; | |
/** | |
* @orm:ManyToOne(targetEntity="UP\UserBundle\Entity\User", inversedBy="id") | |
*/ | |
protected $target_user; | |
public function setUserMessage($userMessage) | |
{ | |
$this->user_message = $userMessage; | |
} | |
public function getUserMessage() | |
{ | |
return $this->user_message; | |
} | |
public function setSystemMessage($systemMessage) | |
{ | |
$this->system_message = $systemMessage; | |
} | |
public function getSystemMessage() | |
{ | |
return $this->system_message; | |
} | |
public function setSourceUser(\UP\UserBundle\Entity\User $sourceUser) | |
{ | |
$this->source_user = $sourceUser; | |
} | |
public function getSourceUser() | |
{ | |
return $this->source_user; | |
} | |
public function setTargetUser(\UP\UserBundle\Entity\User $targetUser = null) | |
{ | |
$this->target_user = $targetUser; | |
} | |
public function getTargetUser() | |
{ | |
return $this->target_user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks ! But, what about construction of form ?... That's the main issue arround "Class Table Inheritance"