Dieses Blog durchsuchen

Samstag, 16. Juli 2016

symfony 3 manyToOne Relation / foreignkeys in doctrine2

If you have multiple table related to one table, lets say via a userid you can tell symfony to do the job for you by generating enties for you.

You can also do this by yourself by editing the entities.
this is the database with all the many2one relations 

CREATE DATABASE `blog` /*!40100 DEFAULT CHARACTER SET utf8 */;
CREATE TABLE `post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`content` longtext COLLATE utf8_unicode_ci NOT NULL,
`userid` int(11) NOT NULL,
`created` datetime NOT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `userid` (`userid`),
CONSTRAINT `post_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `replies` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`content` longtext COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`postid` int(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `userid` (`userid`),
KEY `postid` (`postid`),
CONSTRAINT `replies_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `replies_ibfk_2` FOREIGN KEY (`postid`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`firstname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`lastname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`updated` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_8D93D649F85E0677` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
view raw gistfile1.txt hosted with ❤ by GitHub

At next we generate our entityclasses Posts, Replies and User
Have a look on the annotations of the userid in the related classes replies and post. In both cases the userid is tagged like that:
This annotation tells doctrine to relate the userid to the column "id" in the usertable
/**
* @var \Check\BlogBundle\Entity\User
*
* @ORM\ManyToOne(targetEntity="Check\BlogBundle\Entity\User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="userid", referencedColumnName="id")
* })
*/
view raw gistfile1.txt hosted with ❤ by GitHub


<?php
namespace Check\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* @ORM\Table(name="post", indexes={@ORM\Index(name="userid", columns={"userid"})})
* @ORM\Entity
*/
class Post
{
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255, nullable=false)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime", nullable=false)
*/
private $created;
/**
* @var \DateTime
*
* @ORM\Column(name="updated", type="datetime", nullable=true)
*/
private $updated;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Check\BlogBundle\Entity\User
*
* @ORM\ManyToOne(targetEntity="Check\BlogBundle\Entity\User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="userid", referencedColumnName="id")
* })
*/
private $userid;
/**
* Set title
*
* @param string $title
*
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* @param string $content
*
* @return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set created
*
* @param \DateTime $created
*
* @return Post
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param \DateTime $updated
*
* @return Post
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userid
*
* @param \Check\BlogBundle\Entity\User $userid
*
* @return Post
*/
public function setUserid(\Check\BlogBundle\Entity\User $userid = null)
{
$this->userid = $userid;
return $this;
}
/**
* Get userid
*
* @return \Check\BlogBundle\Entity\User
*/
public function getUserid()
{
return $this->userid;
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

<?php
namespace Check\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Replies
*
* @ORM\Table(name="replies", indexes={@ORM\Index(name="userid", columns={"userid"}), @ORM\Index(name="postid", columns={"postid"})})
* @ORM\Entity
*/
class Replies
{
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255, nullable=false)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime", nullable=false)
*/
private $created;
/**
* @var string
*
* @ORM\Column(name="updated", type="string", length=255, nullable=false)
*/
private $updated;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Check\BlogBundle\Entity\User
*
* @ORM\ManyToOne(targetEntity="Check\BlogBundle\Entity\User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="userid", referencedColumnName="id")
* })
*/
private $userid;
/**
* @var \Check\BlogBundle\Entity\Post
*
* @ORM\ManyToOne(targetEntity="Check\BlogBundle\Entity\Post")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="postid", referencedColumnName="id")
* })
*/
private $postid;
/**
* Set title
*
* @param string $title
*
* @return Replies
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* @param string $content
*
* @return Replies
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set created
*
* @param \DateTime $created
*
* @return Replies
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param string $updated
*
* @return Replies
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return string
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userid
*
* @param \Check\BlogBundle\Entity\User $userid
*
* @return Replies
*/
public function setUserid(\Check\BlogBundle\Entity\User $userid = null)
{
$this->userid = $userid;
return $this;
}
/**
* Get userid
*
* @return \Check\BlogBundle\Entity\User
*/
public function getUserid()
{
return $this->userid;
}
/**
* Set postid
*
* @param \Check\BlogBundle\Entity\Post $postid
*
* @return Replies
*/
public function setPostid(\Check\BlogBundle\Entity\Post $postid = null)
{
$this->postid = $postid;
return $this;
}
/**
* Get postid
*
* @return \Check\BlogBundle\Entity\Post
*/
public function getPostid()
{
return $this->postid;
}
}
view raw gistfile1.txt hosted with ❤ by GitHub
<?php
namespace Check\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="user", uniqueConstraints={@ORM\UniqueConstraint(name="UNIQ_8D93D649F85E0677", columns={"username"})})
* @ORM\Entity
*/
class User
{
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, nullable=false)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, nullable=false)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255, nullable=false)
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", length=255, nullable=false)
*/
private $firstname;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=255, nullable=false)
*/
private $lastname;
/**
* @var string
*
* @ORM\Column(name="created", type="string", length=255, nullable=false)
*/
private $created;
/**
* @var string
*
* @ORM\Column(name="updated", type="string", length=255, nullable=false)
*/
private $updated;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set firstname
*
* @param string $firstname
*
* @return User
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
*
* @return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set created
*
* @param string $created
*
* @return User
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return string
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param string $updated
*
* @return User
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return string
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

Keine Kommentare:

Kommentar veröffentlichen