0

I have two ODM document One is Item contain

class Items {

/**
 * @MongoDB\Field(name="item_name", type="string")
 */
protected $itemName;

}

and another document is

class ItemLocation {
    /**
     * @var
     * @MongoDB\ReferenceOne(targetDocument="Items")
     */
    private $item;

    /**
     * @MongoDB\Field(name="priority", type="integer")
     */
    protected $priority;

    /**
     * @var
     * @MongoDB\ReferenceOne(targetDocument="Location")
     */
    private $location;
}

How I can get all items left join with item location which is filter by location and order by priority.

  • 1
    You need aggregation and an aggregation builder, there's no left join per se in MongoDB. Read more here: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2.x/reference/aggregation-builder.html – malarzm Feb 19 '18 at 15:12

1 Answers1

1

In order to get all items left join with item location you can use the inversedBy and mappedBy options.

The document Items will be like this:

class Items {

/**
 * @MongoDB\Field(name="item_name", type="string")
 */
protected $itemName;

/**
 * @MongoDB\ReferenceMany(targetDocument=ItemLocation::class, mappedBy="item")
 */
private $items_items;

}

The document ItemLocation will be like this:

class ItemLocation
{
    /**
     * @var
     * @MongoDB\ReferenceOne(targetDocument="Items", inversedBy="items_items")
     */
    private $item;

    /**
     * @MongoDB\Field(name="priority", type="integer")
     */
    protected $priority;

    /**
     * @var
     * @MongoDB\ReferenceOne(targetDocument="Location")
     */
    private $location;
}

In order to generate getter and setter use:

php bin/console doctrine:mongodb:generate:documents appBundle

The controller will be like this:

$dm = $this->get('doctrine_mongodb')->getManager();
$repository = $dm->getRepository('Items:Categorie');
$i = $repository->findOneBy(array('id' => 'example'));
$items = $i->getItemsItems();

Read more here.

ASSILI Taher
  • 1,210
  • 2
  • 9
  • 11