【问题标题】:Symfony MongoDB ODM Left Join Query BuilderSymfony MongoDB ODM 左连接查询生成器
【发布时间】:2018-07-29 15:51:59
【问题描述】:

我有两个 ODM 文档一个是 Item contains

class Items {

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

}

另一个文件是

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

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

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

我怎样才能让所有的项目都加入到项目位置,该位置是按位置过滤并按优先级排序的。

【问题讨论】:

标签: mongodb symfony left-join doctrine-odm odm


【解决方案1】:

为了让所有项目与项目位置保持连接,您可以使用 inversedBy 和 mappedBy 选项。

文档项将是这样的:

class Items {

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

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

}

文档 ItemLocation 将是这样的:

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;
}

为了生成getter和setter使用:

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

控制器会是这样的:

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

阅读更多here

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 2019-10-23
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多