Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save Doctrine2 Entity

How to save Doctrine2 Entity if all fields are private? Is there some kind of mechanism to do that?

How can I save this:

/**
 * @Entity
 */
class SomeEntity
{
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;

    /** @Column */
    private $title;

}

How to change title for example? Maybe it's possible via EntityManager?

PS: Thanks in advance

like image 365
ajile Avatar asked Dec 21 '22 09:12

ajile


2 Answers

class SomeEntity
{
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;

    /** @Column */
    private $title;

    public function setTitle($title){
        $this->title = $title;
    }
}

Use like this:

$entity = new SomeEntity();
$entity->setTitle('title');
$em->persist($entity); //$em is an instance of EntityManager
$em->flush();

This is a proper way emphasized in the manual.

like image 132
J0HN Avatar answered Dec 24 '22 02:12

J0HN


public function __get($property)
{
    return $this->$property;
}

public function __set($property,$value)
{
    $this->$property = $value;
}
like image 22
Cody Avatar answered Dec 24 '22 01:12

Cody