Skip to content

Instantly share code, notes, and snippets.

@alihammad-gist
Last active August 29, 2015 14:01
Show Gist options
  • Save alihammad-gist/61cbcf5f9bf36ac07b7b to your computer and use it in GitHub Desktop.
Save alihammad-gist/61cbcf5f9bf36ac07b7b to your computer and use it in GitHub Desktop.
doctrine Mapping.. mappedBy .. inversedBy ... relation tables updates not uncluded
<?php namespace Forum\Entity;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Comment
*
* @author nousheeza
*/
class Comment
{
/**
*
* @var int
*/
protected $id;
/**
*
* @var string
*/
protected $content;
/**
*
* @var string
*/
protected $author;
/**
*
* @var Post
*/
protected $post;
public function getId()
{
return $this->id;
}
public function getContent()
{
return $this->content;
}
public function getAuthor()
{
return $this->author;
}
public function getPost()
{
return $this->post;
}
public function setId($id)
{
$this->id = $id;
}
public function setContent($content)
{
$this->content = $content;
}
public function setAuthor($author)
{
$this->author = $author;
}
public function setPost(Post $post)
{
$this->post = $post;
}
}
Forum\Entity\Comment:
type: entity
table: comments
id:
id:
type: integer
generator:
strategy: AUTO
fields:
content:
type: string
length: 1500
OneToOne:
author:
targetEntity: Forum\Entity\User
ManyToOne:
post:
targetEntity: Forum\Entity\Post
inversedBy: comments
Forum\Entity\Post:
type: entity
table: posts
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string
length: 250
content:
type: string
length: 3500
ManyToOne:
author:
targetEntity: Forum\Entity\User
inversedBy: posts
OneToMany:
comments:
targetEntity: Forum\Entity\Comment
mappedBy: post
Forum\Entity\User:
type: entity
table: users
id:
id:
type: integer
generator:
strategy: AUTO
fields:
username:
type: string
length: 75
displayName:
type: string
length: 75
OneToMany:
posts:
targetEntity: Forum\Entity\Post
mappedBy: author
<?php namespace Forum\Entity;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Post
*
* @author nousheeza
*/
class Post
{
/**
*
* @var int
*/
protected $id;
/**
*
* @var string
*/
protected $title;
/**
*
* @var string
*/
protected $content;
/**
*
* @var mixed
*/
protected $comments;
/**
*
* @var User
*/
protected $author;
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function getContent()
{
return $this->content;
}
public function getComments()
{
return $this->comments;
}
public function getAuthor()
{
return $this->author;
}
public function setId($id)
{
$this->id = $id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function setContent($content)
{
$this->content = $content;
}
public function setComments($comments)
{
$this->comments = $comments;
}
public function setAuthor(User $author)
{
$this->author = $author;
}
}
$ vendor/bin/doctrine orm:schema-tool:create --dump-sql
CREATE TABLE comments (id INTEGER NOT NULL, content VARCHAR(1500) NOT NULL, PRIMARY KEY(id));
CREATE TABLE posts (id INTEGER NOT NULL, title VARCHAR(250) NOT NULL, content VARCHAR(3500) NOT NULL, PRIMARY KEY(id));
CREATE TABLE users (id INTEGER NOT NULL, username VARCHAR(75) NOT NULL, displayName VARCHAR(75) NOT NULL, PRIMARY KEY(id));
<?php namespace Forum\Entity;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of User
*
* @author nousheeza
*/
class User
{
/**
*
* @var int
*/
protected $id;
/**
*
* @var string
*/
protected $username;
/**
*
* @var string
*/
protected $displayName;
/**
*
* @var string
*/
protected $posts;
public function getId()
{
return $this->id;
}
public function getUsername()
{
return $this->username;
}
public function getDisplayName()
{
return $this->displayName;
}
public function getPosts()
{
return $this->posts;
}
public function setId($id)
{
$this->id = $id;
}
public function setUsername($username)
{
$this->username = $username;
}
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
}
public function setPosts($posts)
{
$this->posts = $posts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment