Symfony2 / doctrine插入whi外键

时间:2015-02-17 10:51:36

标签: php symfony orm doctrine-orm

在symfony2中,当我将数据插入数据库时​​,我收到一个异常:


     An exception occurred while executing 'INSERT INTO forum_posts (topic_id, user_id, is_first, post, cdate) VALUES (?, ?, ?, ?, ?)' with params [null, null, "0", "lorem ipsum dooorrrrllosdosodsdd", "2015-02-17 11:35:22"]:

    SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'topic_id' ne peut être vide (null) (500 Internal Server Error)

ForumPosts.php:



namespace Test\TestBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * ForumCategories
 * @ORM\Table(name="forum_posts")
 * @ORM\Entity
 */
class ForumPosts
{

    /**
     * @ORM\ManyToOne(targetEntity="ForumTopics", inversedBy="forum_posts")
     * @ORM\JoinColumn(name="topic_id", referencedColumnName="id")
     */
    private $topic_id;
    protected $topic;
    private $posts;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var integer
     *
     * @ORM\Column(name="topic_id", type="integer", type="decimal", options={"default"="0"})
     */
    private $topicId;

    /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer", type="decimal", options={"default"="0"})
     */
    private $userId;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="forum_topics")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */

    protected $userName;
    /**
     * @var integer
     *
     * @ORM\Column(name="is_first", type="boolean", options={"default"="false"})
     */
    private $isFirst;
    /**
     * @var string
     *
     * @ORM\Column(name="post", type="string")
     */
    private $post;

    /**
     * @var \DateTime
     *
     * * @ORM\Column(name="cdate", type="datetime")
     */
    private $cdate;


    /**
     * Set topicId
     *
     * @param integer $topicId
     * @return ForumTopics
     */
    public function setTopicId($topicId)
    {
        $this->topicId = $topicId;

        return $this;
    }

    /**
     * Get topicId
     *
     * @return integer 
     */
    public function getTopicId()
    {
        return $this->topicId;
    }

    /**
     * Set userId
     *
     * @param integer $userId
     * @return ForumPosts
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;

        return $this;
    }

    /**
     * Get userId
     *
     * @return integer
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * Set user
     *
     * @param User $user
     * @return User
     */
    public function setUser(User $user)
    {
        $this->user = $user;

        return $this;
    }

/**
     * Set topic
     *
     * @param \Test\TestBundle\Entity\ForumTopics $topic
     * @return ForumTopics
     */
    public function setTopic(\Test\TestBundle\Entity\ForumTopics $topic = null)
    {
        $this->topic = $topic;

        return $this;
    }   

    /**
     * Set isFirst
     *
     * @param integer $isFirst
     * @return ForumPosts
     */
    public function setIsFirst($isFirst)
    {
        $this->isFirst = $isFirst;

        return $this;
    }

    /**
     * Get isFirst
     *
     * @return integer
     */
    public function getIsFirst()
    {
        return $this->isFirst;
    }

    /**
     * Set post
     *
     * @param string $post
     * @return ForumPosts
     */
    public function setPost($post)
    {
        $this->post = $post;

        return $this;
    }

    /**
     * Get post
     *
     * @return string 
     */
    public function getPost()
    {
        return $this->post;
    }

    /**
     * Set cdate
     *
     * @param \DateTime $cdate
     * @return ForumPosts
     */
    public function setCdate($cdate)
    {
        $this->cdate = $cdate;

        return $this;
    }

    /**
     * Get cdate
     *
     * @return \DateTime 
     */
    public function getCdate()
    {
        return $this->cdate;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

ForumTopics.php:



namespace Test\TestBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * ForumCategories
 * @ORM\Table(name="forum_topics")
 * @ORM\Entity
 */
class ForumTopics
{


    /**
     * @ORM\ManyToOne(targetEntity="ForumCategories", inversedBy="forum_topics")
     * @ORM\JoinColumn(name="cat_id", referencedColumnName="id")
     */
    private $cat_id;
    protected $category;

    /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer", type="decimal", options={"default"="0"})
     */
    private $userId;
    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="forum_topics")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */

    protected $userName;


    /**
     * @ORM\OneToMany(targetEntity="ForumPosts", mappedBy="topic_id")
     */
    public $topic_id;
    protected $posts;

    public function __construct()
    {
        $this->posts = new ArrayCollection();


    }




    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="cat_id", type="integer", type="decimal", options={"default"="0"})
     */
    private $catId;



    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;



    /**
     * @var \DateTime
     *
     * * @ORM\Column(name="cdate", type="datetime")
     */
    private $cdate;

    /**
     * @var \DateTime
     *
     * * @ORM\Column(name="lastpost", type="datetime")
     */
    private $lastpost;




    /**
     * Set catId
     *
     * @param integer $catId
     * @return ForumTopics
     */
    public function setCatId($catId)
    {
        $this->catId = $catId;

        return $this;
    }

    /**
     * Get catId
     *
     * @return integer 
     */
    public function getCatId()
    {
        return $this->catId;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return ForumTopics
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set userId
     *
     * @param integer $userId
     * @return User
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;

        return $this;
    }

    /**
     * Get userId
     *
     * @return integer 
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * Set cdate
     *
     * @param \DateTime $cdate
     * @return ForumTopics
     */
    public function setCdate($cdate)
    {
        $this->cdate = $cdate;

        return $this;
    }

    /**
     * Get cdate
     *
     * @return \DateTime 
     */
    public function getCdate()
    {
        return $this->cdate;
    }

    /**
     * Set lastpost
     *
     * @param \DateTime $lastpost
     * @return ForumTopics
     */
    public function setLastpost($lastpost)
    {
        $this->lastpost = $lastpost;

        return $this;
    }

    /**
     * Get lastpost
     *
     * @return \DateTime 
     */
    public function getLastpost()
    {
        return $this->lastpost;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set category
     *
     * @param \Test\TestBundle\Entity\ForumCategories $category
     * @return ForumTopics
     */
    public function setCategory(\Test\TestBundle\Entity\ForumCategories $category = null)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * Get catId
     *
     * @return \Test\TestBundle\Entity\ForumCategories 
     */
    public function getCategory()
    {
        return $this->catId;
    }


    /**
     * Add posts
     *
     * @param \Test\TestBundle\Entity\ForumPosts $posts
     * @return ForumTopics
     */
    public function addPost(\Test\TestBundle\Entity\ForumPosts $posts)
    {
        $this->posts[] = $posts;

        return $this;
    }

    /**
     * Remove posts
     *
     * @param \Test\TestBundle\Entity\ForumPosts $posts
     */
    public function removePost(\Test\TestBundle\Entity\ForumPosts $posts)
    {
        $this->posts->removeElement($posts);
    }

    /**
     * Get posts
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getPosts()
    {
        return $this->posts;
    }


}

User.php:




use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;


/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @ORM\Column(name="avatar", type="string", length=255, nullable=true)
     * @var string
     */
    private $avatar;

    /**
     * @ORM\Column(name="firstName", type="string", length=100, nullable=true)
     * @var string
     */
    private $firstName;

    /**
     * @ORM\Column(name="lastName", type="string", length=100, nullable=true)
     * @var string
     */
    private $lastName;

    /**
     * @ORM\Column(name="description", type="text", nullable=true)
     * @var string
     */
    private $description;

    public $salt;





    public function __construct()
    {
        parent::__construct();

    }


    /**
     * @return mixed
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * @param mixed $lastName
     * @return $this
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * @param mixed $firstName
     * @return $this
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;

        return $this;
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param string $description
     * @return $this
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * @param string $password
     * @return $this
     */
    public function setPassword($password)
    {
        $this->password = $password;

    }



    /**
     * Get userId
     *
     * @return integer
     */
    public function getUserId()
    {
        return $this->userId;
    }


}
当我尝试从ForumPostController插入数据库时​​

代码:



$em = $this->getDoctrine()->getManager();

        $cur_date = date_create(date('Y-m-d H:i:s'));

        $product = new ForumPosts();

        $product->setTopic($em->getRepository('TestTestBundle:ForumTopics')->findOneById($request->request->get('topicId'));
        $product->setUser($em->getRepository('TestTestBundle:User')->findOneById($request->request->get('userId')));

        $product->setIsFirst($request->request->get('isFirst'));

        $product->setPost($request->request->get('post'));
        $product->setCdate($cur_date);
        $em = $this->getDoctrine()->getManager();

        $em->persist($product);
        $em->flush();

        return new Response('Created record id '.$product->getId());

请帮助,因为我坚持了下来:)

1 个答案:

答案 0 :(得分:0)

首先,似乎你在学说2中误解了关系。

class ForumPosts
{
    // ...
    /**
     * @ORM\ManyToOne(targetEntity="ForumTopics", inversedBy="forum_posts")
     * @ORM\JoinColumn(name="topic_id", referencedColumnName="id")
     */
    private $topic_id;
    protected $topic;
    private $posts;

    // ...

    /**
     * @var integer
     *
     * @ORM\Column(name="topic_id", type="integer", type="decimal", options={"default"="0"})
     */
    private $topicId;
}

你的@ORM \ Column是没用的,因为ORM \ JoinColumn这样做(或多或少,可能更好)。此外,您的属性$ topic_id应该被称为$ topic,因为您只能使用ForumTopics对象。

这是处理关系的好方法(允许有一个可以为空的关系,并修复你的sql错误):

class ForumPosts
{
    // ...
    /**
     * @ORM\ManyToOne(targetEntity="ForumTopics", inversedBy="forum_posts")
     * @ORM\JoinColumn(name="topic_id", referencedColumnName="id", nullable=true)
     */
    private $topic;
}