Doctrine实体对象到数组

Man*_*nki 15 php arrays doctrine doctrine-orm

想要将doctrine entiry对象转换为普通数组,这是我的代码到目前为止,

 $demo = $this->doctrine->em->find('Entity\User',2);
Run Code Online (Sandbox Code Playgroud)

获取实体对象,

Entity\User Object
(
[id:Entity\User:private] => 2
[username:Entity\User:private] => TestUser
[password:Entity\User:private] => 950715f3f83e20ee154995cd5a89ac75
[email:Entity\User:private] => test@test.com
[firm_id:Entity\User:private] => Entity\Firm Object
    (
        [id:Entity\Firm:private] => 16
        [company_name:Entity\Firm:private] => TestFirm
        [company_detail:Entity\Firm:private] => India
        [created_at:Entity\Firm:private] => DateTime Object
            (
                [date] => 2014-08-01 18:16:08
                [timezone_type] => 3
                [timezone] => Europe/Paris
            )

        [user:Entity\Firm:private] => 
    )

[created_at:Entity\User:private] => DateTime Object
    (
        [date] => 2014-08-01 15:12:36
        [timezone_type] => 3
        [timezone] => Europe/Paris
    )

[updated_at:Entity\User:private] => DateTime Object
    (
        [date] => 2014-08-01 15:12:36
        [timezone_type] => 3
        [timezone] => Europe/Paris
    )

[firm:protected] => 
) ,
Run Code Online (Sandbox Code Playgroud)

试过这个,但根据我的requiremnet不想用户doctrine_query.谢谢.

Anj*_*lva 20

你可以尝试这样的事情,

    $result = $this->em->createQueryBuilder();
    $app_code = $result->select('p')
            ->from('YourUserBundle:User', 'p')
            ->where('p.id= :id')
            ->setParameter('id', 2)
            ->getQuery()
            ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
Run Code Online (Sandbox Code Playgroud)

其他方式,

 $this->em->getRepository('YourUserBundle:User')
      ->findBy(array('id'=>1));
Run Code Online (Sandbox Code Playgroud)

上面将返回一个数组但包含doctrine对象.返回数组的最佳方法是使用doctrine查询.

希望这可以帮助.干杯!

  • 这意味着没有方法将实体对象转换为数组? (3认同)

Kod*_*son 6

由于Doctrine没有提供将实体转换为关联数组的方法,因此您必须自己完成.一种简单的方法是创建一个基类,该基类公开一个返回实体数组表示的函数.这可以通过让基类函数调用get_object_vars自身来完成.此函数获取传入对象的可访问属性,并将它们作为关联数组返回.然后,只要创建一个要转换为数组的实体,就必须扩展此基类.

这是一个非常简单的例子:

abstract class ArrayExpressible {
    public function toArray() {
        return get_object_vars($this);
    }
}

/** @Entity */
class User extends ArrayExpressible {

    /** @Id @Column(type="integer") @GeneratedValue */
    protected $id = 1; // initialized to 1 for testing

    /** @Column(type="string") */
    protected $username = 'abc';

    /** @Column(type="string") */
    protected $password = '123';

}

$user = new User();
print_r($user->toArray());
// Outputs: Array ( [id] => 1 [username] => abc [password] => 123 )
Run Code Online (Sandbox Code Playgroud)

注意:您必须使实体的属性受到保护,以便基类可以使用它来访问它们 get_object_vars()


如果由于某种原因您无法从基类扩展(可能是因为您已经扩展了基类),您至少可以创建一个接口并确保您的实体实现该接口.然后,您必须toArray在每个实体内部实现该功能.

例:

interface ArrayExpressible {
    public function toArray();
}

/** @Entity */
class User extends SomeBaseClass implements ArrayExpressible {

    /** @Id @Column(type="integer") @GeneratedValue */
    protected $id = 1; // initialized to 1 for testing

    /** @Column(type="string") */
    protected $username = 'abc';

    /** @Column(type="string") */
    protected $password = '123';

    public function toArray() {
        return get_object_vars($this);
        // alternatively, you could do:
        // return ['username' => $this->username, 'password' => '****']
    }

}

$user = new User;
print_r($user->toArray());
// Outputs: Array ( [id] => 1 [username] => abc [password] => 123 )
Run Code Online (Sandbox Code Playgroud)