开发者

How to save Doctrine2 Entity

开发者 https://www.devze.com 2023-04-03 14:02 出处:网络
How to save Doctrine2 Entity if all fields are private? Is there some kind of mechanism to do that? How can I save this:

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 So开发者_如何学运维meEntity
{
    /** @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.


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

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号