ajile
ajile

Reputation: 686

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

Upvotes: 5

Views: 4641

Answers (3)

J0HN
J0HN

Reputation: 26951

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.

Upvotes: 10

koral
koral

Reputation: 2965

As noted you should define getters and setters. You can do this manually or on console:

php app/console doctrine:generate:entities Acme/StoreBundle/Entity/SomeEntity

Upvotes: 1

Cody
Cody

Reputation: 11

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

public function __set($property,$value)
{
    $this->$property = $value;
}

Upvotes: 1

Related Questions