简体   繁体   中英

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

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 .

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
public function __get($property)
{
    return $this->$property;
}

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

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