Skip to content

Instantly share code, notes, and snippets.

@cod1ingcoding
Last active August 23, 2017 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cod1ingcoding/e1296d8314ab2f54d157f43139d101cf to your computer and use it in GitHub Desktop.
Save cod1ingcoding/e1296d8314ab2f54d157f43139d101cf to your computer and use it in GitHub Desktop.
<?php
// Epost.php
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity @Table(name="e_post", options={"collate"="utf8_general_ci"})
*/
class Epost
{
/**
* @Id @GeneratedValue @Column(type="integer")
* @var int
*/
protected $id;
public function getId() { return $this->id; }
public function __construct()
{
$this->etags = new ArrayCollection();
}
/**
* @var Etags[]|ArrayCollection
* @ManyToMany(targetEntity="Etag", inversedBy="eposts")
* @JoinTable(name="e_posts_tags")
* @JoinColumn(name="epost_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $etags;
public function getEtags() { return $this->etags; }
/**
* @var string
* @Column(type="string", name="post_title", length=128)
*/
protected $title;
public function getTitle() { return $this->title; }
public function setTitle($title) { $this->title = $title; return $this; }
/**
* @var string
* @Column(type="text", name="post_content")
*/
protected $content;
public function getContent() { return $this->content; }
public function setContent($content) { $this->content = $content; return $this; }
public function addEtag(Etag $etag)
{
if ($this->etags->contains($etag)) return;
$this->etags->add($etag);
$etag->addEpost($this); // for 双向
}
public function removeEtag(Etag $etag)
{
if (!$this->etags->contains($etag)) return;
$this->etags->removeElement($etag);
$etag->removeEpost($this); // for 双向
}
}
// Etag.php
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity @Table(name="e_tags", options={"collate"="utf8_general_ci"})
*/
class Etag
{
/**
* @Id @GeneratedValue @Column(type="integer")
* @var int
*/
protected $id;
public function getId() { return $this->id; }
public function __construct()
{
$this->eposts = new ArrayCollection();
}
/**
* @var Epost[]|ArrayCollection
* @ManyToMany(targetEntity="Epost", mappedBy="etags")
* @JoinColumn(name="etag_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $eposts;
public function getEposts() { return $this->eposts; }
/**
* @var string
* @Column(type="string", name="tag_name", length=128)
*/
protected $name;
public function getName() { return $this->name; }
public function setName($name) { $this->name = $name; return $this; }
public function addEpost(Epost $epost)
{
if ($this->eposts->contains($epost)) return;
$this->eposts->add($epost);
$epost->addEtag($this);
}
public function removeEpost(Epost $epost)
{
if (!$this->eposts->contains($epost)) return;
$this->eposts->removeElement($epost);
$epost->removeEtag($this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment