Select() for QueryBuilder works only for first result #374

Closed
netandreus opened this Issue Jul 27, 2012 · 2 comments

Comments

Projects
None yet
2 participants

I have documents like this:

{
  "_id" : ObjectId("501153b92b5cdc014ba6efe2"),
  "concertHall" : {
    "sql_id" : NumberLong(1),
    "name" : "ClubName",
    "city" : "Moscow"
  }
}

When I querying embedded document for field in embeded document like this:

$qb = $this->_dm->getRepository('Document\Concert')->createQueryBuilder();
$qb->field('concertHall.isPartner')->equals(true);
$qb->select('concertHall.name');
$qb->limit(2);
/** @var $cursor Doctrine\ODM\MongoDB\Cursor */
$cursor = $qb->find()->getQuery()->execute();
foreach($cursor as $doc) {
     var_dump($doc);
}
die();

I have this results:

object(Document\Concert)[661]
  protected 'id' => string '5012491d2b5cdc014ba6f095' (length=24)
  protected 'concertHall' => 
    object(Document\ConcertHall)[745]
      protected 'sqlId' => null
      protected 'name' => string 'ClubName' (length=23)
      protected 'city' => null
      protected 'url' => null
      protected 'isPartner' => null
object(Document\Concert)[646]
  protected 'id' => string '5012491d2b5cdc014ba6f085' (length=24)
  protected 'concertHall' => 
    object(Document\ConcertHall)[603]
      protected 'sqlId' => int 1
      protected 'name' => string 'SecondClub' (length=23)
      protected 'city' => string 'Perm' (length=29)
      protected 'url' => null
      protected 'isPartner' => boolean true

It means, that select(ConcertHall.name) works only for first result, and other results loaded fully (with all fields). How can I fix it?

P.S. console command db.concerts.find({'concertHall.isPartner': true}, {'concertHall.name': 1}).pretty(); works fine

Member

malarzm commented Dec 1, 2014

This seems to be working as expected now, here's passing test case:

<?php

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

class GH374Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
    public function test374()
    {
        $d1 = new GH374Document();
        $d1->embed = new GH374EmbeddedDocument('v1', 'v2', 'v3');
        $this->dm->persist($d1);
        $d2 = new GH374Document();
        $d2->embed = new GH374EmbeddedDocument('v1', 'v4', 'v5');
        $this->dm->persist($d2);
        $d3 = new GH374Document();
        $d3->embed = new GH374EmbeddedDocument('v6', 'v7', 'v8');
        $this->dm->persist($d3);

        $this->dm->flush();
        $this->dm->clear();

        $qb = $this->dm->getRepository(get_class($d1))->createQueryBuilder()
                ->field('embed.v1')->equals('v1')
                ->select('embed.v2')
                ->limit(2);
        $results = $qb->find()->getQuery()->execute();
        foreach ($results as $r) {
            $this->assertNull($r->embed->v1);
            $this->assertNotNull($r->embed->v2);
            $this->assertNull($r->embed->v3);
        }
    }
}

/** @ODM\Document */
class GH374Document
{
    /** @ODM\Id */
    public $id;

    /** @ODM\EmbedOne(targetDocument="GH374EmbeddedDocument") */
    public $embed;
}

/** @ODM\EmbeddedDocument */
class GH374EmbeddedDocument
{
    /** @ODM\String */
    public $v1;

    /** @ODM\String */
    public $v2;

    /** @ODM\String */
    public $v3;

    public function __construct($v1, $v2, $v3)
    {
        $this->v1 = $v1;
        $this->v2 = $v2;
        $this->v3 = $v3;
    }
}

Member

malarzm commented Mar 6, 2015

No objections have been made so I'm closing ticket for now

@malarzm malarzm closed this Mar 6, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment