我正在使用这个bundle,我想通过%LIKE%
mysql找到我的元素
我的文件是这样的:
{
"_id": "60a403542a2b7d44bf84ed569d1e6efe",
"_rev": "1-9e1ed380dd91ac8c835ef0f8e152f0a8",
"type": "mytype",
"doctrine_metadata": {
"indexes": [
"name",
]
},
"name": "barfoo",
}
我想查找以barfo
我看到了link,但我不知道这是否是正确的解决方案。
如何在couchDb + symfony2中进行Like
查询?
答案 0 :(得分:1)
couchDb(1.6.1)中没有%Like%
,但您可以使用startkey和endkey。
您必须创建一个类似的视图:
function (doc) {
if (doc.type = 'myType') {
emit(doc.name, doc.name);
}
}
名称为people
。
例如简单
Designname = people
viewname = people
之后你可以做这个卷曲
curl -X GET 'http://localhost:5984/db/_design/people/_view/people?startkey="foo"&endkey="foo\ufff0"'
通过这种方式,您可以找到以foo开头的所有元素。
为什么\ ufff0?请检查此link
String Ranges
如果你需要包含每个字符串的开始和结束键 给定前缀,最好使用高值unicode字符,而不是 使用' ZZZZ'后缀。
即,而不是:
startkey =" ABC"&安培; endkey =" abcZZZZZZZZZ"你应该使用:
startkey =" ABC"&安培; endkey =" ABC \ ufff0"
你如何在Symfony中做到这一点?
如果您想创建View by Symfony
您可以在yourapp/AppBundle/CouchDocument/View/PeopleView.php
<?php
namespace yourapp\AppBundle\CouchDocument\View;
use Doctrine\CouchDB\View\DesignDocument;
class PeopleView implements DesignDocument
{
public function getData()
{
$people = <<<'JS'
function (doc) {
if (doc.type = 'myType') {
emit(doc.name, doc.name);
}
}
JS;
return array(
'views' => array(
'people' => array(
'map' => $people,
),
)
);
}
}
在控制器中你可以做这样的事情
/**
* @Route("/testPeople/{code}", name="test_people")
* @Method({"GET"})
* @Template("myAppBundle:People:test_query.html.twig")
*/
public function testPeopleAction($code)
{
$dm = $this->container->get(
'doctrine_couchdb.odm.default_document_manager'
);
$peopleView = new PeopleView();
$client = $this->container->get('doctrine_couchdb.odm.default_document_manager');
$view = $client->createDesignDocument('people', $peopleView);
$relatedObjects = $dm->createNativeQuery('people', 'people')
->setStartKey($code)
->setEndKey($code . "\xEF\xBF\xB0")
->execute();
$a = array();
foreach ($relatedObjects as $doc) {
$a[] = $doc['value'] //it is your results
}
return array(
'results' => $a
);
}
我在vendor/doctrine/couchdb/tests/Doctrine/Tests/CouchDB/CouchDBClientTest.php
中看到了unitTest来找到有用的内容
我不知道这是否是最好的方式,但对我来说它有效:)