外键未在Doctrine / Symfony中使用OneToMany关系保存

时间:2016-04-02 17:00:11

标签: php symfony

以下是我的两个实体类:

[root@sandbox ~]# java -jar kafkaconsumer.jar
log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties).
log4j:WARN Please initialize the log4j system properly.

<?php

namespace IW\Bundle\ArticlesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="article")
 */
class Article
{

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


    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="article")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;


    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     */
    protected $heading;


    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    protected $body;

    /**
     * @ORM\Column(type="datetime", nullable=false)
     * @ORM\Version
     * @var \DateTime
     */
    protected $created = null;


    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="article", cascade="persist")
     */
    private $comments;



    // non persisted

    protected $username;

    protected $user_id;


    public function setUsername($username) {

        $this->username = $username;
    }

    public function getUsername() {
        return $this->username;
    }
    public function setUserId($id) {



        $this->user_id = $id;
    }
    public function getUserId() {
        return $this->user_id;
    }
    // end non persisted


    // can this be hooked into event?  i.e. JMSSerialiser?
    public function beforeSerialisation() {

        $this->setUserid($this->user->getId());
        $this->setUsername($this->user->getUsername());
        $this->setUser(null);
    }





    public function __construct() {
        $this->created = new \DateTime();
    }


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

    /**
     * Set heading
     *
     * @param string $heading
     * @return Article
     */
    public function setHeading($heading)
    {
        $this->heading = $heading;

        return $this;
    }

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

    /**
     * Set body
     *
     * @param string $body
     * @return Article
     */
    public function setBody($body)
    {
        $this->body = $body;

        return $this;
    }

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

    /**
     * Set user
     *
     * @param \IW\Bundle\ArticlesBundle\Entity\User $user
     * @return Article
     */
    public function setUser(\IW\Bundle\ArticlesBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \IW\Bundle\ArticlesBundle\Entity\User 
     */
    public function getUser()
    { 
        return $this->user;
    }



    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Article
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

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


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

    /**
     * Add comments
     *
     * @param \IW\Bundle\ArticlesBundle\Entity\Comment $comments
     * @return Article
     */
    public function addComment(\IW\Bundle\ArticlesBundle\Entity\Comment $comments)
    {
        $this->comments[] = $comments;

        return $this;
    }

    /**
     * Remove comments
     *
     * @param \IW\Bundle\ArticlesBundle\Entity\Comment $comments
     */
    public function removeComment(\IW\Bundle\ArticlesBundle\Entity\Comment $comments)
    {
        $this->comments->removeElement($comments);
    }
}

稍后在代码中我将数据绑定到注释表单,通过id检索文章并调用$ article-&gt; addComment($ comment)然后调用:

namespace IW\Bundle\ArticlesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity
 * @ORM\Table(name="comment")
 */
class Comment
{

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



    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    protected $body;

    /**
     * @ORM\Column(type="datetime", nullable=false)
     * @ORM\Version
     * @var \DateTime
     */
    protected $created = null;


     /**
     * @ORM\ManyToOne(targetEntity="Article", inversedBy="comments")
     * @ORM\JoinColumn(name="article_id", referencedColumnName="id")
     */
    private $article;



    public function __construct() {
        $this->created = new \DateTime();
    }



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

    /**
     * Set body
     *
     * @param string $body
     * @return Comment
     */
    public function setBody($body)
    {
        $this->body = $body;

        return $this;
    }

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

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Comment
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

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



    // non persisted
    /**
     * @Assert\NotBlank()
     * @Assert\Type(type="digit")
     */
    protected $article_id;

    public function setArticleId($id) {
        $this->article_id = $id;
    }
    public function getArticleId() {
        return $this->article_id;
    }
    // end non persisted



    /**
     * Set article
     *
     * @param \IW\Bundle\ArticlesBundle\Entity\Article $article
     * @return Comment
     */
    public function setArticle(\IW\Bundle\ArticlesBundle\Entity\Article $article = null)
    {
        $this->article = $article;

        return $this;
    }

    /**
     * Get article
     *
     * @return \IW\Bundle\ArticlesBundle\Entity\Article 
     */
    public function getArticle()
    {
        return $this->article;
    }
}

评论正在保存,但不包含文章的引用/外键。

1 个答案:

答案 0 :(得分:0)

您在文章上调用了start_date = '2005/01/01' end_date = '2016/03/31' o=data.DataReader('EWC', "yahoo", start=start_date,end=end_date) g=data.DataReader('ARGT', "yahoo", start=start_date,end=end_date) ,但addComment从未在您的评论中调用。您可以修改设置器setArticle,以便调用addComment

setArticle