Hi,
I wonder if someone could point me in the right direction dealing/querying "referenceMany" fields. I have a "News" document which are for News items, and I want to add tags to these individual news items, so I've created a "Tags" Document and in the News document I've added:
/**
*@PHPCR\ReferenceMany(targetDocument="Web\Bundle\WebsiteBundle\Document\Tags")
*/
protected $tags;
I am using the sonata admin doctrine-phpcr-admin-bundle, and as my tag field (configureFormFields) in "NewsAdmin":
->add('tags', null, array('required' => false), array(), array('expanded' => true, 'multiple' => true))
Everything is working great, I can add tags to my News Documents through the Sonata Admin backend.
Now on one of my web pages I want to show all News items that were tagged by "Bill" (in the following I'm querying by Bill's (Tag) uuid) . So I dug around, and I tried using what I found in Joining with an Association - Doctrine ODM docs where it has:
$qb->fromDocument('Doctrine\Tests\Models\CMS\CmsUser', 'u');
->addJoinInner()
->right()->document('Doctrine\Tests\Models\CMS\CmsAddress', 'a')->end()
->condition()->equi('u.address', 'a.uuid');
->where()->eq()->field('a.city')->literal('Lyon');
$users = $qb->getQuery()->execute();
So I have run:
$dm = $this->get('doctrine_phpcr')->getManager();
$qb = $dm->createQueryBuilder();
$qb->fromDocument('WebWebsiteBundle:News', 'n');
$qb->addJoinInner()
->right()->document('WebWebsiteBundle:Tags', 't')->end()
->condition()->equi('n.tags', 't.uuid');
$qb->where()->like()->field('t.uuid')->literal('a9bea86b-da2a-43ff-a436-05878e78c6f2');
$qb->orderBy()->desc()->field('n.date')->end();
$news = $qb->getQuery()->execute();
If every News document is tagged with just one tag it would work, but if I have Newsitem1 tagged with "Bill" and "Tim", Newsitem2 tagged with "Bob" and "Bill", Newsitem3 tagged with "Bill" and "Jill" I would only get Newsitem1 and Newsitem3 for Bill's uuid with that query because Bill's uuid comes up first in the list.
So basically when I do a dump of the a news item I'll see:
-property: "tags"
-referencedNodes: array:2 [
0 => "fa0f9c09-141d-47e9-811d-41d1a199446b"
1 => "a9bea86b-da2a-43ff-a436-05878e78c6f2"
]
-targetDocument: "Web\Bundle\WebsiteBundle\Document\Tags"
and if the uuid of my Tag is not in the 0 position of the array, I won't be able to get it.
I've also tried just doing the following (to try "Querying multivalue fields") to see if that would work:
$dm = $this->get('doctrine_phpcr')->getManager();
$qb = $dm->createQueryBuilder();
$qb->fromDocument('WebWebsiteBundle:News', 'n');
$qb->where()->eq()->field('n.tags')->literal('a9bea86b-da2a-43ff-a436-05878e78c6f2');
$news = $qb->getQuery()->execute();
Which gives me the following error: "Cannot use association property "tags" of class "Web\Bundle\WebsiteBundle\Document\News" as a dynamic operand."
I have also looked around at other answers on github/stackoverflow which don't quite seem to get me there. A push in the right direction would be much appreciated. Any other help or pointers as how to query referenceMany fields would be also be great.
As for what I'm using:
- sonata-project/doctrine-phpcr-admin-bundle 1.2.3
- phpcr/phpcr 2.1.2
- phpcr/phpcr-utils 1.2.6
- doctrine/phpcr-bundle 1.2.4
- doctrine/phpcr-odm 1.2.3
- jackalope/jackalope 1.2.0
- jackalope/jackalope-doctrine-dbal 1.2.1
Thanks,
- Steve