Hello,
I am using Symfony with Doctrine and I have a problem inserting Entities.
I had a field in an Entity called "$author" and i was storing the id of an
user. I discovered that I could make use the "ManyToOne" annontation to let
doctrine do the join for me.
So I have a class "Post" :
<?php
namespace Llafon\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="post")
* @ORM\Entity
*/
class Post {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100, nullable=false)
*/
protected $title;
/**
* @ORM\Column(type="text", nullable=false)
*/
protected $content;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
protected $date;
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="id", referencedColumnName="id")
*/
protected $author;
/**
* @ORM\Column(type="string", nullable=false)
*/
protected $image_name;
public function __construct() {
$this->date = new \DateTime();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 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 date
*
* @param \DateTime $date
*
* @return Post
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDateString($format = 'Y-m-d H:i:s')
{
return $this->date->format($format);
}
/**
* Set author
*
* @param integer $author
*
* @return Post
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return integer
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set imageName
*
* @param integer $imageName
*
* @return Post
*/
public function setImageName($imageName)
{
$this->image_name = $imageName;
return $this;
}
/**
* Get imageName
*
* @return \author
*/
public function getImageName()
{
return $this->image_name;
}
}
and a class User (using fosuserbundle):
<?php
// src/AppBundle/Entity/User.php
namespace Llafon\BlogBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
}
}
Now I can get some posts without problem using doctrine. However I have a
problem when I want to insert a Post because the "author" variable is not
part of the insert instruction...
This is what i tried:
public function addAction(Request $request) {
// On crée un objet Advert
$post = new Post();
$user = $this->container->get('security.context')->getToken()->
getUser(); //get connected user (i dispayed it and this variable is not
empty)
$post->setAuthor($user);
$post->setContent("zz");
$post->setImageName("aaa");
$post->setTitle("fff");
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
}
This is the error message:
An exception occurred while executing 'INSERT INTO post (title, content,
date, image_name, id) VALUES (?, ?, ?, ?, ?)' with params ["fff", "zz",
"2015-10-25
20:20:54", "aaa", 1]:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint
failed: post.author
Thanks in advance for any help.
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.