Skip to content

Instantly share code, notes, and snippets.

@rufinus
Last active December 14, 2015 03:49
Show Gist options
  • Select an option

  • Save rufinus/5024167 to your computer and use it in GitHub Desktop.

Select an option

Save rufinus/5024167 to your computer and use it in GitHub Desktop.
<?php
namespace Admin\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Admin\Entity\Article
*
* @ORM\Entity(repositoryClass="Admin\Repository\ArticleRepository")
* @ORM\Table(name="`article`", indexes={@ORM\Index(name="fk_article_category", columns={"category_id"})})
*/
class Article
{
/**
* @ORM\Id
* @ORM\Column(name="`article_id`", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $article_id;
/**
* @ORM\Column(name="`title`", type="string", length=255)
*/
protected $title;
/**
* @ORM\Column(name="`bodytext`", type="text", nullable=true)
*/
protected $bodytext;
/**
* @ORM\Column(name="`created`", type="datetime", nullable=true)
*/
protected $created;
/**
* @ORM\Column(name="`updated`", type="datetime", nullable=true)
*/
protected $updated;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="articles")
* @ORM\JoinColumn(name="category_id", referencedColumnName="category_id", nullable=false)
*/
protected $category;
/**
* @ORM\ManyToMany(targetEntity="Tag", mappedBy="articles")
*/
protected $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
/**
* Set the value of article_id.
*
* @param integer $article_id
* @return \Admin\Entity\Article
*/
public function setArticleId($article_id)
{
$this->article_id = $article_id;
return $this;
}
/**
* Get the value of article_id.
*
* @return integer
*/
public function getArticleId()
{
return $this->article_id;
}
/**
* Set the value of title.
*
* @param string $title
* @return \Admin\Entity\Article
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get the value of title.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set the value of bodytext.
*
* @param string $bodytext
* @return \Admin\Entity\Article
*/
public function setBodytext($bodytext)
{
$this->bodytext = $bodytext;
return $this;
}
/**
* Get the value of bodytext.
*
* @return string
*/
public function getBodytext()
{
return $this->bodytext;
}
/**
* Set the value of created.
*
* @param datetime $created
* @return \Admin\Entity\Article
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get the value of created.
*
* @return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set the value of updated.
*
* @param datetime $updated
* @return \Admin\Entity\Article
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get the value of updated.
*
* @return datetime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set Category entity (many to one).
*
* @param \Admin\Entity\Category $category
* @return \Admin\Entity\Article
*/
public function setCategory(Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get Category entity (many to one).
*
* @return \Admin\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
/**
* Add Tag entity to collection.
*
* @param \Admin\Entity\Tag $tag
* @return \Admin\Entity\Article
*/
public function addTag(Tag $tag)
{
$this->tags[] = $tag;
return $this;
}
/**
* Get Tag entity collection.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTags()
{
return $this->tags;
}
public function __sleep()
{
return array('article_id', 'title', 'bodytext', 'created', 'updated', 'category_id');
}
}
<?php
namespace Admin\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Admin\Entity\Category
*
* @ORM\Entity(repositoryClass="Admin\Repository\CategoryRepository")
* @ORM\Table(name="`category`")
*/
class Category
{
/**
* @ORM\Id
* @ORM\Column(name="`category_id`", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $category_id;
/**
* @ORM\Column(name="`url`", type="string", length=200)
*/
protected $url;
/**
* @ORM\Column(name="`name`", type="string", length=250, nullable=true)
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="Article", mappedBy="category")
* @ORM\JoinColumn(name="category_id", referencedColumnName="category_id", nullable=false)
*/
protected $articles;
public function __construct()
{
$this->articles = new ArrayCollection();
}
/**
* Set the value of category_id.
*
* @param integer $category_id
* @return \Admin\Entity\Category
*/
public function setCategoryId($category_id)
{
$this->category_id = $category_id;
return $this;
}
/**
* Get the value of category_id.
*
* @return integer
*/
public function getCategoryId()
{
return $this->category_id;
}
/**
* Set the value of url.
*
* @param string $url
* @return \Admin\Entity\Category
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get the value of url.
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set the value of name.
*
* @param string $name
* @return \Admin\Entity\Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get the value of name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add Article entity to collection (one to many).
*
* @param \Admin\Entity\Article $article
* @return \Admin\Entity\Category
*/
public function addArticle(Article $article)
{
$this->articles[] = $article;
return $this;
}
/**
* Get Article entity collection (one to many).
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getArticles()
{
return $this->articles;
}
public function __sleep()
{
return array('category_id', 'url', 'name');
}
}
$ php vendor/doctrine/doctrine-module/bin/doctrine-module orm:schema-tool:create --dump-sql
CREATE TABLE `tag` (`tag_id` INT NOT NULL, `name` VARCHAR(100) NOT NULL, PRIMARY KEY(`tag_id`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE article_has_tag (tag_id INT NOT NULL, article_id INT NOT NULL, INDEX IDX_DA9BAECEBAD26311 (tag_id), INDEX IDX_DA9BAECE7294869C (article_id), PRIMARY KEY(tag_id, article_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE `category` (`category_id` INT AUTO_INCREMENT NOT NULL, `url` VARCHAR(200) NOT NULL, `name` VARCHAR(250) DEFAULT NULL, PRIMARY KEY(`category_id`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE `article` (category_id INT NOT NULL, `article_id` INT AUTO_INCREMENT NOT NULL, `title` VARCHAR(255) NOT NULL, `bodytext` LONGTEXT DEFAULT NULL, `created` DATETIME DEFAULT NULL, `updated` DATETIME DEFAULT NULL, INDEX IDX_23A0E6612469DE2 (category_id), PRIMARY KEY(`article_id`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE article_has_tag ADD CONSTRAINT FK_DA9BAECEBAD26311 FOREIGN KEY (tag_id) REFERENCES `tag` (tag_id);
ALTER TABLE article_has_tag ADD CONSTRAINT FK_DA9BAECE7294869C FOREIGN KEY (article_id) REFERENCES `article` (article_id);
ALTER TABLE `article` ADD CONSTRAINT FK_23A0E6612469DE2 FOREIGN KEY (category_id) REFERENCES `category` (category_id);
$ php vendor/doctrine/doctrine-module/bin/doctrine-module orm:schema-tool:update --dump-sql
ALTER TABLE article_has_tag DROP FOREIGN KEY FK_DA9BAECE7294869C;
ALTER TABLE article_has_tag DROP FOREIGN KEY FK_DA9BAECEBAD26311;
ALTER TABLE article_has_tag ADD CONSTRAINT FK_DA9BAECE7294869C FOREIGN KEY (article_id) REFERENCES `article` (article_id);
ALTER TABLE article_has_tag ADD CONSTRAINT FK_DA9BAECEBAD26311 FOREIGN KEY (tag_id) REFERENCES `tag` (tag_id);
ALTER TABLE article DROP FOREIGN KEY FK_23A0E6612469DE2;
ALTER TABLE article ADD CONSTRAINT FK_23A0E6612469DE2 FOREIGN KEY (category_id) REFERENCES `category` (category_id)
<?php
namespace Admin\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Admin\Entity\Tag
*
* @ORM\Entity(repositoryClass="Admin\Repository\TagRepository")
* @ORM\Table(name="`tag`")
*/
class Tag
{
/**
* @ORM\Id
* @ORM\Column(name="`tag_id`", type="integer")
*/
protected $tag_id;
/**
* @ORM\Column(name="`name`", type="string", length=100)
*/
protected $name;
/**
* @ORM\ManyToMany(targetEntity="Article", inversedBy="tags")
* @ORM\JoinTable(name="article_has_tag",
* joinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="tag_id")},
* inverseJoinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="article_id")}
* )
*/
protected $articles;
public function __construct()
{
$this->articles = new ArrayCollection();
}
/**
* Set the value of tag_id.
*
* @param integer $tag_id
* @return \Admin\Entity\Tag
*/
public function setTagId($tag_id)
{
$this->tag_id = $tag_id;
return $this;
}
/**
* Get the value of tag_id.
*
* @return integer
*/
public function getTagId()
{
return $this->tag_id;
}
/**
* Set the value of name.
*
* @param string $name
* @return \Admin\Entity\Tag
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get the value of name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add Article entity to collection.
*
* @param \Admin\Entity\Article $article
* @return \Admin\Entity\Tag
*/
public function addArticle(Article $article)
{
$this->articles[] = $article;
return $this;
}
/**
* Get Article entity collection.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getArticles()
{
return $this->articles;
}
public function __sleep()
{
return array('tag_id', 'name');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment