【问题标题】:How can I retrieve data from ArrayCollection (Symfony)?如何从 ArrayCollection (Symfony) 中检索数据?
【发布时间】:2019-01-07 09:24:01
【问题描述】:

动物:

| 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           |

我想做的是为dog 获取categories

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();
  }
}

【问题讨论】:

  • 可以添加实体表名吗?
  • @hoover_D 抱歉,Animals 是实体名称
  • 您必须被放置在 Animals 存储库中才能获取它们或进行加入..
  • @hoover_D 是的,抱歉,我的问题有误。现在是正确的
  • 你能发布你的Entity Animals.php吗?

标签: php symfony doctrine entity arraycollection


【解决方案1】:

没有Animals:Category 实体。您有实体 AnimalsCategory

正确答案取决于您使用的是 Symfony 3 还是 4,因为 Symfony 3 使用实体别名(命名空间使用您尝试使用的 : 表示法),而 Symfony 4 更喜欢完全限定的命名空间(\App\Entity\Animals )。

所以,第一个错误是在您尝试获取存储库的位置:

getRepository('Animals:Category')

在 DQL 查询中 findByIdJoinedToCategory() 中的第二个:

'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'

【讨论】:

  • 感谢您提供的非常好的和详细的回答。我会立即测试它。我有 Symfony 4。所以只有一个问题。然后在我的控制器中写:$result = $this-&gt;getDoctrine()-&gt;getRepository(Animals::class)-&gt;findByIdJoinedToCategory(['id'=&gt;$id]);?
  • 其实我刚刚注意到,您还有另一个问题。方法findByIdJoinedToCategory() 方法不带任何参数。所以你也必须改进它。它应该期待$id 参数,然后你应该在方法中使用它来做你想做的事。
  • 如果我测试你的代码并转储result,那么我会得到一个包含所有动物的数组cat, dog, frog
  • 这是因为,正如我在之前的评论中所写,您并没有真正使用 $id 参数来过滤查询。
  • 是的,Jarla,请用你的 id 参数检查我的回答,看看它是否有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 2023-04-01
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多