简单的 Doctrine 查询生成器
simple Doctrine query builder
我是 Symfony 和 Doctrine 的新手。无法理解如何编写 queryBuilder。
简单来说 SQL 两者之一:
select c.*, s.result
from client c
inner join score s on c.id = s.client_id;
select c.*, s.result
from client c, score s where c.id = s.client_id;
我从 ClientRepository 尝试了以下内容:
$clients = $this->createQueryBuilder('c')
->select('c')
->innerJoin(Score::class, 's', 'with', 's.client = c.id')
->getQuery()
->getResult();
没有看到 score.result 数据。
关系客户端<=>得分为OneToOne
那么如何 return 带有附加结果 属性 的客户端对象数组?
实体:
客户:
/**
* @ORM\Entity(repositoryClass="App\Repository\ClientRepository")
*/
class Client
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
*/
private $id;
/**
* @ORM\Column(type="string", length=64)
* @Assert\NotBlank()
*/
private $name;
/**
* @ORM\Column(type="string", length=64)
* @Assert\NotBlank()
*/
private $surname;
/**
* @ORM\Column(type="string", length=128, unique=true)
* @Assert\NotBlank()
* @Assert\Email(
* message = "The email '{{ value }}' is not a valid email."
* )
*/
private $email;
/**
* @ORM\Column(type="string", length=32)
* @Assert\NotBlank()
* @Assert\Type(
* type="numeric",
* message="The value {{ value }} is not a number {{ type }}."
* )
* @Assert\Length(
* min = 11,
* max = 32,
* minMessage = "Your phone number must be at least {{ limit }} characters long",
* maxMessage = "Your phone number cannot be longer than {{ limit }} characters",
* allowEmptyString = false
* )
*/
private $phone;
/**
* @ORM\Column(type="boolean")
*/
private $processData;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Education")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank()
*/
private $education;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getProcessData(): ?bool
{
return $this->processData;
}
public function setProcessData(bool $processData): self
{
$this->processData = $processData;
return $this;
}
public function getEducation(): ?Education
{
return $this->education;
}
public function setEducation(?Education $education): self
{
$this->education = $education;
return $this;
}
}
得分:
/**
* @ORM\Entity(repositoryClass="App\Repository\ScoreRepository")
*/
class Score
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $result;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Client", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $client;
public function getId(): ?int
{
return $this->id;
}
public function getResult(): ?int
{
return $this->result;
}
public function setResult(?int $result): self
{
$this->result = $result;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(Client $client): self
{
$this->client = $client;
return $this;
}
}
那么如何 return 带有附加结果 属性 的客户端对象数组?
正常的 Symfony 和 Doctrine 方法是将 Client - Score 关联更改为 bidirectional 而不是当前的单向关联。
正在更新您的客户端实体端:
/**
* @ORM\OneToOne(targetEntity="App\Entity\Score", mappedBy="client")
*/
private $score;
public function getScore(): ?Score
{
return $this->score;
}
public function setScore(Score $score): self
{
$this->score = $score;
return $this;
}
你的分数实体:
/**
* @ORM\OneToOne(targetEntity="App\Entity\Client", inversedBy="score" cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $client;
然后您不需要编写任何特定的查询生成器。当您有可用的 Client 对象时,您可以调用 getter 来获取关联的 Score 对象。
$client->getScore();
我是 Symfony 和 Doctrine 的新手。无法理解如何编写 queryBuilder。 简单来说 SQL 两者之一:
select c.*, s.result
from client c
inner join score s on c.id = s.client_id;
select c.*, s.result
from client c, score s where c.id = s.client_id;
我从 ClientRepository 尝试了以下内容:
$clients = $this->createQueryBuilder('c')
->select('c')
->innerJoin(Score::class, 's', 'with', 's.client = c.id')
->getQuery()
->getResult();
没有看到 score.result 数据。 关系客户端<=>得分为OneToOne 那么如何 return 带有附加结果 属性 的客户端对象数组?
实体:
客户:
/**
* @ORM\Entity(repositoryClass="App\Repository\ClientRepository")
*/
class Client
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
*/
private $id;
/**
* @ORM\Column(type="string", length=64)
* @Assert\NotBlank()
*/
private $name;
/**
* @ORM\Column(type="string", length=64)
* @Assert\NotBlank()
*/
private $surname;
/**
* @ORM\Column(type="string", length=128, unique=true)
* @Assert\NotBlank()
* @Assert\Email(
* message = "The email '{{ value }}' is not a valid email."
* )
*/
private $email;
/**
* @ORM\Column(type="string", length=32)
* @Assert\NotBlank()
* @Assert\Type(
* type="numeric",
* message="The value {{ value }} is not a number {{ type }}."
* )
* @Assert\Length(
* min = 11,
* max = 32,
* minMessage = "Your phone number must be at least {{ limit }} characters long",
* maxMessage = "Your phone number cannot be longer than {{ limit }} characters",
* allowEmptyString = false
* )
*/
private $phone;
/**
* @ORM\Column(type="boolean")
*/
private $processData;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Education")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank()
*/
private $education;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getProcessData(): ?bool
{
return $this->processData;
}
public function setProcessData(bool $processData): self
{
$this->processData = $processData;
return $this;
}
public function getEducation(): ?Education
{
return $this->education;
}
public function setEducation(?Education $education): self
{
$this->education = $education;
return $this;
}
}
得分:
/**
* @ORM\Entity(repositoryClass="App\Repository\ScoreRepository")
*/
class Score
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $result;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Client", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $client;
public function getId(): ?int
{
return $this->id;
}
public function getResult(): ?int
{
return $this->result;
}
public function setResult(?int $result): self
{
$this->result = $result;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(Client $client): self
{
$this->client = $client;
return $this;
}
}
那么如何 return 带有附加结果 属性 的客户端对象数组?
正常的 Symfony 和 Doctrine 方法是将 Client - Score 关联更改为 bidirectional 而不是当前的单向关联。
正在更新您的客户端实体端:
/**
* @ORM\OneToOne(targetEntity="App\Entity\Score", mappedBy="client")
*/
private $score;
public function getScore(): ?Score
{
return $this->score;
}
public function setScore(Score $score): self
{
$this->score = $score;
return $this;
}
你的分数实体:
/**
* @ORM\OneToOne(targetEntity="App\Entity\Client", inversedBy="score" cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $client;
然后您不需要编写任何特定的查询生成器。当您有可用的 Client 对象时,您可以调用 getter 来获取关联的 Score 对象。
$client->getScore();