Symofiny2,对实体对象执行标准

Symofiny2, Perfom Criteria on Entity Object

我正在从事 Symfony 项目。我正在从 Bundle 获取商店列表。在下面的控制器操作中,我想知道结果来自哪里。

检查下面代码中的代码段 "EducateToolsBundle:Stores"。

myAction (){
    $stores = c2gr($this, 'EducateToolsBundle:Stores')->findBySelectOption(true); // Where can i Find/change the Data of stores.
    $userLocations = array();
    foreach ($stores as $store) {
        $userLocations[] = $store->getId();
    }
}

我不确定 c2gr 是什么,所以我假设它是一个获取您的学说存储库的自定义函数。类似这样:

$repo = $this->get('doctrine.orm.entity_manager')->getRepository('EducateToolsbundle:Stores');
$stores = $repo->findBySelectOption(true);

结果将来自您项目使用的任何数据库,无论是我 MySQL、Postgres、MongoDB 还是其他。

如果您想更改商店,您可以通过在商店实体上调用 setter 在代码中执行此操作(您必须检查您的实体以查看 setter 实际上是什么存在):

foreach ($stores as $store) {
    // Assuming that a Store has a SetLocation method that takes a string
    $store->setLocation('Las Vegas');
}

如果您只是需要快速更改数据而不想编写代码,则需要访问数据库。

更新 来自评论

findBySelectOption 是 Doctrine 做事的方式,就像 SQL 中的 WHERE 子句一样。所以调用 findBySelectOption(true) 将大致转换为:

SELECT * FROM Stores WHERE SelectOption = true;

如果您想查找 LOCTYPE=9 的商店,那么您可以将 findBySelectOption(true) 替换为:

findBy(['selectOption' => true, 'LOCTYPE' => 9])

这将转换为:

SELECT * FROM Stores WHERE SelectOption = true AND LOCTYPE = 9;

注意:您必须检查您的商店实体,找到 SelectOptionLOCTYPE 的正确大小写,否则它可能无法工作。 IE。 Stores上的属性叫LOCTYPE还是loctype还是LocType

更新 2 如果你想通过不等于来查找,比如:

WHERE LOCTYPE != 9

然后您必须使用查询生成器做一些额外的工作。参见 this question