我正在尝试使用存储库方法findOneBy
(id)从数据库中获取对象。
基本上,该行如下所示:
public function findAssignedTickets(User $user)
{
$userId = $user->getId();
$ticketMapping = new ResultSetMapping;
$ticketMapping->addEntityResult(Ticket::class, 't');
$ticketMapping->addFieldResult('t', 'id', 'id');
// Postgresql Native query, select all tickets where participants array includes the userId
$query = "SELECT *
FROM (
SELECT id, array_agg(e::text::int) arr
FROM ticket, json_array_elements(participants) e
GROUP BY 1
) s
WHERE
$userId = ANY(arr);
";
$results = $this->getEntityManager()->createNativeQuery($query, $ticketMapping)->getResult();
$results = array_map(function($item) {
return $item->getId();
}, $results); // Transform to array in integers
dump($results); // array:2 [0 => 83, 1 => 84] -> It's correct
$tickets = [];
foreach ($results as $ticketId) {
dump($this->findOneById($ticketId));
// $ticket = $this->findOneById($ticketId);
// $tickets[] = [
// 'identifier' => $ticket->getIdentifier(),
// 'title' => $ticket->getTitle(),
// 'author' => $ticket->getAuthor()->getUsername(),
// 'status' => $ticket->getStatus(),
// 'created' => $ticket->getCreatedAt()->format('c'),
// 'updated' => $ticket->getUpdatedAt()->format('c'),
// ]; // Ticket formatting to send in json
}
return $tickets;
}
并且我确定接收到的ID与数据库中的一行匹配,并且数据库包含数据,并且所有字段都直接属于该实体,除了author代表ManyToOne以外,我听说过懒惰显示教义,但不应在其他领域发生。
为什么即使使用吸气剂也无法从Object检索数据,为什么将所有值都设置为null Except for id?
编辑:我想知道那是否与我以前用来在完全独立的请求中获取票证ID的ResultSetMapping有关,当我添加一个addFieldResult('t', 'title', 'title');
时,它确实起作用了,但对另一个却没有领域,另一个谜。