以下显示: Marlena有12幅画(基本上来自文档)
如何访问collect(绘画)中的数据 例如:标题
$query = "MATCH (n:Artist)-[:PAINTED]->(Painting) RETURN n.first_name, collect(Painting) as paintings";
$result = $client->run($query);
foreach ($result->getRecords() as $record) {
echo sprintf('%s has %d paintings', $record->value('n.first_name'), count($record->value('paintings')));
echo '<br/>';
}
我想展示:
艺术家姓名:
我认为这些数据可以来自绘画或绘画。我只是不确定如何整理查询。它将使用print_r和记录显示,因此我知道数据正在通过。
答案 0 :(得分:1)
这应该适合你:
$query = "MATCH (n:Artist)-[:PAINTED]->(Painting) RETURN n.first_name, collect(Painting) as paintings";
$result = $client->run($query);
foreach ($result->getRecords() as $record) {
echo sprintf('%s has %d paintings:<br/>', $record->value('n.first_name'), count($record->value('paintings')));
foreach ($record->value('n.paintings') as $painting) {
echo sprintf('- %s<br/>', $painting->value('title'));
}
echo '<br/>';
}
答案 1 :(得分:0)
我最终得到了以下内容:
foreach ($result->getRecords() as $record) {
$fname = $record->values()[0]->get('first_name');
$lname = $record->values()[0]->get('last_name');
echo '<strong>'.$fname.' '.$lname.' painted:</strong><br/>';
for ($x = 0; $x < count($record->values()[1]); $x++) {
print_r($record->values()[1][$x]->get('title'));
echo ' - ';
print_r($record->values()[1][$x]->get('views'));
echo ' views<br/>';
}
echo '<br/>';
}
提供以下输出:
姓名姓氏:
最终笔记 我实际上尝试的代码类似于你在努力实现这一目标时所建议的代码。我有点困惑,为什么不这样做。
所以我想知道。我想出的是可以接受的吗?
答案 2 :(得分:0)
a)我建议您为返回值设置别名,在驱动程序级别获取它们更容易
b)绘画记录值返回一个Node
个对象的数组,因此是可迭代的,不需要计算for循环:
$query = "MATCH (n:Artist)-[:PAINTED]->(Painting) RETURN n.first_name as firstName, collect(Painting) as paintings";
$result = $client->run($query);
foreach($result->records() as $record) {
echo sprintf('%s painted %d paintings', $record->get('firstName'), count($record->get('paintings'))) . PHP_EOL;
foreach ($record->get('paintings') as $painting) {
echo sprintf('%s - %d views', $painting->value('title'), $painting->value('views')) . PHP_EOL;
}
}