简体   繁体   中英

Doctrine entity not setting parameters when persisting data

I'm trying to save some data using Doctrine using entities. I've got it reading data in various ways perfectly fine, including associations etc, but I can't seem to get it to save data.

I have the following entity:

    <?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;
    }
}

And I have the following method in my controller:

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' );
    }
}   

I get an error in my error log saying all the values are set to null... if I set the entities parameters to public and set them like $this->model->name = 'A name'; etc, that works, but obviously this isn't best practice.

If I var_dump $this->model it shows that the setter methods all have the correct values, but the parameters are all set to null.

Does anyone know why my setters aren't working?

Cheers,

Ewan

Proper use of setters is $this->model->setDate(new DateTime()); not $this->model->setDate =...

You missed ORM\\ prefix in annotations:

/**
 * @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;
    }
}

and then in action:

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' );
    }
}   

Also will be useful Add Mapping Information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM