我试图使用doctrine2执行查询,并需要它返回一个集合对象.
简化代码段:
$players = $this->getEntityManager()
->createQueryBuilder()
->select('p')
->from('...\Player', 'p')
->getQuery()
->getResult();
Run Code Online (Sandbox Code Playgroud)
返回的对象是Player的数组.
有关查询结果格式的信息说:
结果是对象的简单集合(纯)或对象嵌套在结果行(混合)中的数组.
结果类型取决于什么以及如何获得集合对象?
我想在 Symfony 4 中创建一个安全的 PHP 会话
我在Symfony 文档和API 文档中查找信息,但找不到任何对我的问题的参考。
在纯 PHP 中,我曾经执行以下操作来创建安全的 PHP 会话:
$session_name = 'session';
$secure = true;
$httponly = true;
$cookieParams = session_get_cookie_params();
session_set_cookie_params(
$cookieParams["lifetime"],
$cookieParams["path"],
$cookieParams["domain"],
$secure,
$httponly
);
session_name($session_name);
session_start();
Run Code Online (Sandbox Code Playgroud)
你如何在 Symfony 4 中做类似的事情?以下代码不起作用:
$session->set('lifetime', 0);
$session->set('secure', true);
$session->set('httponly', true);
Run Code Online (Sandbox Code Playgroud) 似乎在计数时消耗了迭代器.我如何使用相同的迭代器进行计数然后迭代它?
我正在尝试计算文件中的行,然后打印它们.我能够读取文件内容,我能够计算行数,但是我不再能够遍历这些行,就像内部游标位于迭代器的末尾一样.
use std::fs::File;
use std::io::prelude::*;
fn main() {
let log_file_name = "/home/myuser/test.log";
let mut log_file = File::open(log_file_name).unwrap();
let mut log_content: String = String::from("");
//Reads the log file.
log_file.read_to_string(&mut log_content).unwrap();
//Gets all the lines in a Lines struct.
let mut lines = log_content.lines();
//Uses by_ref() in order to not take ownership
let count = lines.by_ref().count();
println!("{} lines", count); //Prints the count
//Doesn't enter in the loop
for value in lines {
println!("{}", value);
}
}
Run Code Online (Sandbox Code Playgroud)