Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

php - How to fetch class instead of array in Doctrine 2

I am able to fetch my data from database by using this structure:

$user = $this->getDoctrine()
->getRepository('AcmeDemoBundle:Emails')
->find(8081);

When I do that, I am able to get my data like this:

$user->getColumnNameHere();

Basically I am able to use Entity Class.

But if I want to use QueryBuilder instead of find I am only getting associative arrays.

$product->createQueryBuilder('p')
        ->setMaxResults(1)
        ->where('p.idx = :idx')
        ->select('p.columnNameHere')
        ->setParameter('idx', 8081)
        ->orderBy('p.idx', 'DESC')
        ->getQuery();
        $product = $query->getResult();

$product returnds as array. Is it possible to fetch it withj Entity Managaer Class? If yes, how?

I digg the documentation but it seems not possible or not exist in the doc or I'm just blind :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes you can, usually using:

$repository
    ->createQueryBuilder('p')
    ->getQuery()
    ->execute()
;

This should return you an array of entities.

If you want to get a single entity result, use either getSingleResult or getOneOrNullResult:

$repository
    ->createQueryBuilder('p')
    ->getQuery()
    ->getOneOrNullResult()
;

Warning: These method can potentially throw NonUniqueResultException.

Edit: Ok, so the question was about partial objects: http://docs.doctrine-project.org/en/latest/reference/partial-objects.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...