Syntax Error line 0, col 60: Error: Expected end of string, got 'ON' - Symfony 4/Doctrine2
Syntax Error line 0, col 60: Error: Expected end of string, got 'ON' - Symfony 4/Doctrine2
我在 Symfony 4.4 和 Omines/Datatables Bundle 中使用 Doctrine 2 时遇到问题。
我有两个实体,用户和日志,如下所示:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=50)
*/
private $lastname;
/**
* @ORM\OneToMany(targetEntity=Log::class, mappedBy="user")
*/
private $logs;
public function __construct()
{
$this->logs = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
/**
* @return Collection|Log[]
*/
public function getLogs(): Collection
{
return $this->logs;
}
public function addLog(Log $log): self
{
if (!$this->logs->contains($log)) {
$this->logs[] = $log;
$log->setUser($this);
}
return $this;
}
public function removeLog(Log $log): self
{
if ($this->logs->contains($log)) {
$this->logs->removeElement($log);
// set the owning side to null (unless already changed)
if ($log->getUser() === $this) {
$log->setUser(null);
}
}
return $this;
}
}
实体日志:
<?php
namespace App\Entity;
use App\Repository\LogRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=LogRepository::class)
*/
class Log
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $logStart;
/**
* @ORM\Column(type="string", length=15)
*/
private $ip;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="logs")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
*/
private $user;
public function getId(): ?int
{
return $this->id;
}
public function getLogStart(): ?\DateTimeInterface
{
return $this->logStart;
}
public function setLogStart(\DateTimeInterface $logStart): self
{
$this->logStart = $logStart;
return $this;
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(string $ip): self
{
$this->ip = $ip;
return $this;
}
public function getUser(): ?user
{
return $this->user;
}
public function setUser(?user $user): self
{
$this->user = $user;
return $this;
}
}
我也使用 omines/datatables 捆绑包 (Documentation and link to github)
我试图构建一个对我的用户实体进行左连接的查询。我在控制器中的代码如下:
$table = $this->datatableFactory->create([])
->add('id', TextColumn::class, ['label' => '#', 'className' => 'bold', 'searchable' => true])
->add('firstname lastname', TextColumn::class, ['label' => $translator->trans('Customer name'), 'className' => 'bold', 'searchable' => true])
->add('logStart', DateTimeColumn::class, ['label' => $translator->trans('Time'), 'className' => 'bold', 'searchable' => false])
->createAdapter(ORMAdapter::class, [
'entity' => Log::class,
'query' => function (QueryBuilder $queryBuilder) {
$queryBuilder
->select('l, u.firstname, u.lastname')
->from(Log::class, 'l')
->leftJoin(User::class, 'u', Join::ON, 'l.user = u');
}
]);
$table->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
我收到以下错误:Syntax Error line 0, col 60: Error: Expected end of string, got 'ON'
但是当我更改以下内容时:->leftJoin(User::class, 'u', Join::ON, 'l.user = u');
至:->leftJoin(User::class, 'u', Join::WITH, 'l.user = u.id');
我收到以下错误:
Cannot read property "id" from an array. Maybe you intended to write
the property path as "[id]" instead.
有人知道我做错了什么吗?
感谢您的每一次帮助:)
编辑:
我在 github 上找到了解决方案,并将我的代码更改为:
->createAdapter(ORMAdapter::class, [
'hydrate' => \Doctrine\ORM\Query::HYDRATE_ARRAY,
'entity' => Log::class,
'query' => function (QueryBuilder $queryBuilder) {
$queryBuilder
->select('l, u')
->from(Log::class, 'l')
->leftJoin('l.user', 'u');
}
]);
但这对我来说没有任何改变。我仍然无法访问用户实体(在这种情况下,例如名字和姓氏)。
问题已解决。在多对一关系中,我们必须在列中使用 'field' 选项。例如:
$table = $dataTableFactory->create()
->add('firstName', TextColumn::class, ['label' => 'Firstname', 'field' => 'user.firstname'])
->add('logStart', DateTimeColumn::class, ['label' => 'Log start'])
->createAdapter(ORMAdapter::class, [
'entity' => Log::class,
])
->handleRequest($request);
我在 Symfony 4.4 和 Omines/Datatables Bundle 中使用 Doctrine 2 时遇到问题。
我有两个实体,用户和日志,如下所示:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=50)
*/
private $lastname;
/**
* @ORM\OneToMany(targetEntity=Log::class, mappedBy="user")
*/
private $logs;
public function __construct()
{
$this->logs = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
/**
* @return Collection|Log[]
*/
public function getLogs(): Collection
{
return $this->logs;
}
public function addLog(Log $log): self
{
if (!$this->logs->contains($log)) {
$this->logs[] = $log;
$log->setUser($this);
}
return $this;
}
public function removeLog(Log $log): self
{
if ($this->logs->contains($log)) {
$this->logs->removeElement($log);
// set the owning side to null (unless already changed)
if ($log->getUser() === $this) {
$log->setUser(null);
}
}
return $this;
}
}
实体日志:
<?php
namespace App\Entity;
use App\Repository\LogRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=LogRepository::class)
*/
class Log
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $logStart;
/**
* @ORM\Column(type="string", length=15)
*/
private $ip;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="logs")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
*/
private $user;
public function getId(): ?int
{
return $this->id;
}
public function getLogStart(): ?\DateTimeInterface
{
return $this->logStart;
}
public function setLogStart(\DateTimeInterface $logStart): self
{
$this->logStart = $logStart;
return $this;
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(string $ip): self
{
$this->ip = $ip;
return $this;
}
public function getUser(): ?user
{
return $this->user;
}
public function setUser(?user $user): self
{
$this->user = $user;
return $this;
}
}
我也使用 omines/datatables 捆绑包 (Documentation and link to github)
我试图构建一个对我的用户实体进行左连接的查询。我在控制器中的代码如下:
$table = $this->datatableFactory->create([])
->add('id', TextColumn::class, ['label' => '#', 'className' => 'bold', 'searchable' => true])
->add('firstname lastname', TextColumn::class, ['label' => $translator->trans('Customer name'), 'className' => 'bold', 'searchable' => true])
->add('logStart', DateTimeColumn::class, ['label' => $translator->trans('Time'), 'className' => 'bold', 'searchable' => false])
->createAdapter(ORMAdapter::class, [
'entity' => Log::class,
'query' => function (QueryBuilder $queryBuilder) {
$queryBuilder
->select('l, u.firstname, u.lastname')
->from(Log::class, 'l')
->leftJoin(User::class, 'u', Join::ON, 'l.user = u');
}
]);
$table->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
我收到以下错误:Syntax Error line 0, col 60: Error: Expected end of string, got 'ON'
但是当我更改以下内容时:->leftJoin(User::class, 'u', Join::ON, 'l.user = u');
至:->leftJoin(User::class, 'u', Join::WITH, 'l.user = u.id');
我收到以下错误:
Cannot read property "id" from an array. Maybe you intended to write the property path as "[id]" instead.
有人知道我做错了什么吗? 感谢您的每一次帮助:)
编辑:
我在 github 上找到了解决方案,并将我的代码更改为:
->createAdapter(ORMAdapter::class, [
'hydrate' => \Doctrine\ORM\Query::HYDRATE_ARRAY,
'entity' => Log::class,
'query' => function (QueryBuilder $queryBuilder) {
$queryBuilder
->select('l, u')
->from(Log::class, 'l')
->leftJoin('l.user', 'u');
}
]);
但这对我来说没有任何改变。我仍然无法访问用户实体(在这种情况下,例如名字和姓氏)。
问题已解决。在多对一关系中,我们必须在列中使用 'field' 选项。例如:
$table = $dataTableFactory->create()
->add('firstName', TextColumn::class, ['label' => 'Firstname', 'field' => 'user.firstname'])
->add('logStart', DateTimeColumn::class, ['label' => 'Log start'])
->createAdapter(ORMAdapter::class, [
'entity' => Log::class,
])
->handleRequest($request);