【问题标题】:Doctrine entity not setting parameters when persisting data在持久化数据时,Doctrine 实体不设置参数
【发布时间】:2014-01-27 15:08:24
【问题描述】:

我正在尝试使用 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->model->name = 'A name';等,这是可行的,但显然这不是最佳实践。

如果我 var_dump $this->model 表明 setter 方法都有正确的值,但参数都设置为 null。

有人知道为什么我的二传手不工作吗?

干杯,

伊万

【问题讨论】:

    标签: php symfony doctrine-orm doctrine


    【解决方案1】:

    正确使用setter是$this-&gt;model-&gt;setDate(new DateTime());而不是$this-&gt;model-&gt;setDate =...

    【讨论】:

    • 啊啊!这应该很明显,谢谢!我以前在 Doctrine 中使用 ActiveRecord,这就是为什么我把 = $value;而不是 ( $value );再次感谢
    【解决方案2】:

    您在注释中错过了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

    【讨论】:

    • 在setter上很好看,以前是用activerecord来设置参数,而不是把数据传给setter函数,粗心的错误,但我已经盯着它看了几个小时!!
    猜你喜欢
    • 2016-04-13
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2016-05-12
    • 2015-08-24
    • 1970-01-01
    相关资源
    最近更新 更多