php - 如何从 ArrayCollection (Symfony) 中检索数据?
问题描述
动物:
| id | name |
|----|------|
| 1 | cat |
| 2 | dog |
| 3 | frog |
类别:
| id | name |
|----|--------|
| 1 | green |
| 2 | blue |
| 3 | orange |
动物_类别:
| animals_id | category_id |
|------------|-------------|
| 1 | 1 |
| 2 | 1 |
| 2 | 2 |
我想要做的是获得categories
for dog
:
green, blue
这是我的方法:
控制器:
$id = '2';
$result = $this->getDoctrine()->getRepository('Animals:Category')->findByIdJoinedToCategory(['id'=>$id]);
动物资料库:
public function findByIdJoinedToCategory()
{
$query = $this->getEntityManager()
->createQuery(
'SELECT a, b FROM Animals:Category a
JOIN a.category b');
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
但我收到一条错误消息:
未知实体命名空间别名“动物”。
实体Animals
:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="App\Repository\AnimalsRepository")
*/
class Animals
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="Category")
* @ORM\JoinColumn(name="category", referencedColumnName="id")
*/
private $category;
public function getId(): ?int
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getCategory()
{
return $this->category;
}
public function setCategory($category): self
{
$this->category = $category;
return $this;
}
public function addCategory(Category $category): self
{
$this->category[] = $category;
return $this;
}
public function __construct()
{
$this->category = new ArrayCollection();
}
}
解决方案
没有Animals:Category
实体。您有实体Animals
和Category
.
正确答案取决于您使用的是 Symfony 3 还是 4,因为 Symfony 3 使用实体别名(使用:
您尝试使用的符号的命名空间),而 Symfony 4 更喜欢完全限定的命名空间(\App\Entity\Animals
)。
因此,第一个错误是您尝试获取存储库的位置:
getRepository('Animals:Category')
而findByIdJoinedToCategory()
DQL 查询中的第二个:
'SELECT a, b FROM Animals:Category a
JOIN a.category b'
现在解决方案:
Symfony 3
因为看起来你没有任何捆绑包(我猜它是 Symfony 4 但无论如何),你没有任何实体命名空间别名,所以你应该简单地使用它的名字。
getRepository('Animals')
现在,我假设a
你想引用Animals
实体/表,所以它应该是
'SELECT a, b FROM Animals a
JOIN a.category b'
Symfony 4
如果你使用 Symfony 4,那么使用应该使用实体 FQNS 作为实体名称 ( App\Entity\Animals
)。
所以它会是
getRepository('\App\Entity\Animals')
或者
getRepository(\App\Entity\Animals::class)
获取存储库。第二个更好,因为它会在需要时更容易重构(IDE 将能够找到类的用法)。
在查询中它会是
'SELECT a, b FROM App\Entity\Animals a
JOIN a.category b'
或者如果您想避免使用硬编码的字符串类名:
'SELECT a, b FROM ' . \App\Entity\Animals:class . ' a
JOIN a.category b'
推荐阅读
- ruby-on-rails - 如何为 simple_form 添加多个标签?
- docker - Docker 启动错误:无法检查网桥接口是否存在:没有可用的缓冲区空间
- python - 从 TSV 列中的嵌套 JSON 中提取 n 个值
- kubernetes - istio-proxy监听配置socketaddress.address设置为集群中某个Service的clusterIP是什么意思?
- php - 有没有办法使用 laravel sanctum 保护一些没有登录要求的 api?
- google-cloud-platform - 气流:GoogleCloudStorageToBigQueryOperator 错误
- reactjs - Paper.js 在尝试使用 React net::ERR_ABORTED 404(未找到)实现时未加载
- dialogflow-cx - 在用于 QA 的 Dialogflow CX 中将环境和版本作为参数引用
- sql - dense_rank ()over ID values - 如何按顺序而不是值对 ID 编号进行排名
- node.js - 无法达到 60K 到 1L 的并发连接连接数