我是Symfony & Doctrine的新手。无法理解如何在纯SQL中写一个queryBuilder.在两个中的任何一个。
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 数据 .Relationship Client<=>Score is OneToOneSo 如何返回带有附加结果属性的 Client 对象数组?
实体:
Client:
/**
* @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;
}
}
Score.Score.Results.Relation是OneToOne的关系,所以如何返回带有附加结果属性的Client对象数组?
/**
* @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;
}
}
如何返回带有附加结果属性的Client对象数组?
正常的Symfony & Doctrine方式是将Client - Score关联改为 双向 而不是你现在的单向的。
更新你的客户端实体端。
/**
* @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;
}
和你的 Score 实体。
/**
* @ORM\OneToOne(targetEntity="App\Entity\Client", inversedBy="score" cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $client;
然后你就不需要写任何特定的查询生成器。当你有了Client对象后,你可以直接调用getter来获取相关的Score对象。
$client->getScore();