Doctrine实体在持久化数据时不设置参数

时间:2014-01-27 15:08:24

标签: php symfony doctrine-orm doctrine

我试图使用Doctrine使用实体保存一些数据。我已经以各种方式读取数据,包括协会等,但我似乎无法保存数据。

我有以下实体:

    <?php

namespace CommentsBundle\Entities;

use Doctrine\ORM\Mapping AS ORM;

/**
 * @Entity
 * @Table(name="comments")
 */
class Comments
{
    /**
     * @Column(type="integer")
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @Column(type="string") */
    protected $name;

    /** @Column(type="string") */
    protected $email;

    /** @Column(type="string") */
    protected $content;

    /** @Column(type="string") */
    protected $date;

    /** @Column(type="integer") */
    protected $user_id;

    /** @Column(type="integer") */
    protected $post_id;

    /** @ManyToOne(targetEntity="UserBundle\Entities\Users") */
    protected $user;

    /** @ManyToOne(targetEntity="ContentBundle\Entities\Posts") */
    protected $post;

    public function setId( $id ) 
    {
        $this->id = $id;
    }

    public function getId() 
    {
        return $this->id;
    }

    public function setName( $name ) 
    {
        $this->name = $name;
    }

    public function getName() 
    {
        return $this->name;
    }

    public function setEmail( $email ) 
    {
        $this->email = $email;
    }

    public function getEmail() 
    {
        return $this->email;
    }

    public function setContent( $content ) 
    {
        $this->content = $content;
    }

    public function getContent() 
    {
        return $this->content;
    }

    public function setDate( $date ) 
    {
        $this->date = $date;
    }

    public function getDate() 
    {
        return $this->date;
    }

    public function setUser_id( $user_id ) 
    {
        $this->user_id = $user_id;
    }

    public function getUser_id() 
    {
        return $this->user_id;
    }

    public function setPost_id( $post_id ) 
    {
        $this->post_id = $post_id;
    }

    public function getPost_id() 
    {
        return $this->post_id;
    }
}

我的控制器中有以下方法:

public function newComment( Request $request ) 
{
    $this->model->setDate       = new \DateTime();
    $this->model->setName       = $request->get( 'name' );
    $this->model->setEmail      = $request->get( 'email' );
    $this->model->setContent    = $request->get( 'content' );
    $this->model->setPost_id    = $request->get( 'id' );
    $this->model->setUser_id    = $request->get( 1 );


    self::$app['orm.em']->persist( $this->model );
    self::$app['orm.em']->flush();

    if ( $save ) {
        de( 'Worked' );
    } else {
        de( 'Did not work' );
    }
}   

我的错误日志中出现错误,说所有值都设置为null ...如果我将实体参数设置为public并将它们设置为$ this-&gt; model-&gt; name =&#39; A命名&#39 ;;等,这是有效的,但显然这不是最好的做法。

如果我var_dump $ this-&gt;模型,则表明setter方法都具有正确的值,但参数都设置为null。

有谁知道为什么我的安装人员不工作?

干杯,

伊万

2 个答案:

答案 0 :(得分:2)

正确使用制定者是$this->model->setDate(new DateTime());而不是$this->model->setDate =...

答案 1 :(得分:1)

您在注释中错过了ORM\前缀:

/**
 * @ORM\Entity
 * @ORM\Table(name="comments")
 */
class Comments
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @ORM\Column(type="string") */
    protected $name;

    /** @ORM\Column(type="string") */
    protected $email;

    /** @ORM\Column(type="string") */
    protected $content;

    /** @ORM\Column(type="string") */
    protected $date;

    /** @ORM\Column(type="integer") */
    protected $user_id;

    /** @ORM\Column(type="integer") */
    protected $post_id;

    /** @ORM\ManyToOne(targetEntity="UserBundle\Entities\Users") */
    protected $user;

    /** @ORM\ManyToOne(targetEntity="ContentBundle\Entities\Posts") */
    protected $post;

    public function setId( $id ) 
    {
        $this->id = $id;
    }

    public function getId() 
    {
        return $this->id;
    }

    public function setName( $name ) 
    {
        $this->name = $name;
    }

    public function getName() 
    {
        return $this->name;
    }

    public function setEmail( $email ) 
    {
        $this->email = $email;
    }

    public function getEmail() 
    {
        return $this->email;
    }

    public function setContent( $content ) 
    {
        $this->content = $content;
    }

    public function getContent() 
    {
        return $this->content;
    }

    public function setDate( $date ) 
    {
        $this->date = $date;
    }

    public function getDate() 
    {
        return $this->date;
    }

    public function setUser_id( $user_id ) 
    {
        $this->user_id = $user_id;
    }

    public function getUser_id() 
    {
        return $this->user_id;
    }

    public function setPost_id( $post_id ) 
    {
        $this->post_id = $post_id;
    }

    public function getPost_id() 
    {
        return $this->post_id;
    }
}

然后在行动中:

public function newComment( Request $request ) 
{
    $this->model->setDate( new \DateTime() );
    $this->model->setName( $request->get( 'name' ) );
    $this->model->setEmail( $request->get( 'email' ) );
    $this->model->setContent( $request->get( 'content' ) );
    $this->model->setPost_id( $request->get( 'id' ) );
    $this->model->setUser_id( $request->get( 1 ) );


    self::$app['orm.em']->persist( $this->model );
    self::$app['orm.em']->flush();

    if ( $save ) {
        de( 'Worked' );
    } else {
        de( 'Did not work' );
    }
}   

同样有用Add Mapping Information