Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2统计与其他实体相关的实体字段_Php_Symfony - Fatal编程技术网

Php Symfony2统计与其他实体相关的实体字段

Php Symfony2统计与其他实体相关的实体字段,php,symfony,Php,Symfony,我有两个实体-作者和书,一个作者可能有很多书。我想在表格中显示每个作者有多少本书(每个作者的数量不同)。我也已经看到了这个问题,并且尝试了这个,因为我认为这将是一个更优雅的解决方案: <td>{{books|length}}</td> 作者的选择如下: $query = $em->createQuery('SELECT b FROM AB\ProjectBundle\Entity\Books u WHERE b.authorid in (:authorids)')

我有两个实体-作者和书,一个作者可能有很多书。我想在表格中显示每个作者有多少本书(每个作者的数量不同)。我也已经看到了这个问题,并且尝试了这个,因为我认为这将是一个更优雅的解决方案:

<td>{{books|length}}</td>
作者的选择如下:

$query = $em->createQuery('SELECT b FROM AB\ProjectBundle\Entity\Books u WHERE b.authorid in (:authorids)');
$query->setParameter('authorid',$authorids);
$books = $query->getResult();
$query = $em->createQuery('SELECT a FROM AB\ProjectBundle\Entity\Authors a');
$authorids = $query->getResult();
编辑:我的小枝循环

<tbody>
            {% for authors in author %}
        <tr>
            <td>{{ authors.name }}</td>
            <td>{{ authors.isactive }}</td>         
            <td>{{ books.authorid|length}}</td>
        </tr>
            {% endfor %}
</tbody>
编辑3个图书实体

<?php

namespace AB\ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * 
 */
class Books
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var \AB\ProjectBundle\Entity\Author
     */
    private $authorid;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set Authorid
     *
     * @param \AB\ProjectBundle\Entity\Author $authorid
     * @return Author
     */
    public function setAuthorid(\AB\ProjectBundle\Entity\Author $authorid = null)
    {
        $this->authorid = $authorid;

    return $this;
    }

    /**
     * Get Authorid
     *
     * @return \AB\ProjectBundle\Entity\Author
     */
    public function getAuthorid()
    {
        return $this->authorid;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return string
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}
Author.orm.yml

AB\ProjectBundle\Entity\Author:
type: entity
table: Author
id:
    id:
        type: integer
        nullable: false
        unsigned: false
        id: true
        generator:
            strategy: IDENTITY
fields:
    name:
        type: text
        nullable: false
lifecycleCallbacks: {  }

您不需要为此获取所有书籍,因此,如果您不在本页面的其他地方使用系统中所有书籍的数组,我将删除该查询(并查看参数)

然后我会开始清理一些命名

AB\ProjectBundle\Entity\Books:
...
manyToOne:
    authorid:
        targetEntity: Author
实体是你的应用程序处理一件事情的方法,因此我们将使用单数名称

在Doctrine中创建关系时,您(通常)根本不需要设置自己的联接表或考虑外键(ID)。按实体命名关系更有意义(就像文档中那样)

所以以上应该是

AB\ProjectBundle\Entity\Book:
...
manyToOne:
    author:
        targetEntity: Author
您的作者实体缺少与书籍实体的关系

AB\ProjectBundle\Entity\Author:
...
oneToMany:
    books:
        targetEntity: Book
请注意,在一个实体中,我们如何与单数名称相关,而在另一个实体中,我们如何与复数名称相关。这些名字很简单,因为一个作者可以有很多书,但一本书只能属于一个作者

因此,在你的应用程序中(如果你从这些映射中生成实体),你会得到像
$book->getAuthor()
$author->getBooks()
这样的方法,它们正确地描述了它们的功能以及你期望返回的内容


有了这些关系,这应该简单到:

选择所需的作者:

$query = $em->createQuery('SELECT a, b FROM AB\ProjectBundle\Entity\Authors a JOIN a.books b');
$authors = $query->getResult();
请注意$authors而不是$authords,这也是因为我们得到的是authors而不是id数组。我们加入书籍(通过作者实体中称为“书籍”的关系)以避免以后强制条令运行一个或多个单独的查询(如果不加入关系,则延迟加载关系)

然后在细枝上,我们就这么做了

 <tbody>
        {% for author in authors %}
    <tr>
        <td>{{ author.name }}</td>
        <td>{{ author.isactive }}</td>         
        <td>{{ author.books|length}}</td>
    </tr>
        {% endfor %}
 </tbody>

{作者中的作者%}
{{author.name}
{{author.isactive}}
{{作者.书籍|长度}
{%endfor%}

此处更改的最后一位命名
{%对于author%}
中的作者来说是错误的。指出这一点可能显得微不足道或愚蠢,但编写清晰易懂的代码对于帮助他人(和您自己)理解实际发生的事情是至关重要的。如果我读“authors”,那么我期望的是一个authors数组,而不是一个包含一个author信息的对象。

author和Book实体是否有实际关系?如果是这样的话,你可以只做
{{author.books | length}}
(其中“books”是作者实体中你命名的图书关系)是的,有一个实际的关系,但我仍然继续得到所有图书的总数。我只是不能告诉它单独显示与每个作者相关的图书数量((你能在输出作者/图书信息的地方添加细枝循环吗?当我获得图书总数并尝试使用其某些属性(比如authorid)时,我得到一个错误“带有0、1、2、3、4等键的数组的键'authorid',在我的细枝模板中不存在”向我们展示您的作者实体(如预期的那样))非常感谢您的帮助!太好了,希望有道理:)回答得很好!详细而精确的解释+1我宁愿让作者这样做:
$em->getRepository('APProjectBundle:Author”)->findAll()
@cezar我同意也不同意。查询应该在存储库中,而不是在控制器中-是。但是默认的
findAll()
query将不包括书籍,因此,如果您像在Twig中一样在for循环中获取书籍,您将强制Doctrine运行n+1查询(它将为循环中的每个作者运行一个selectbooks from author查询)。您可以重写findAll方法,或者将books关系设置为eager loading,但我建议您创建一个自定义的
findAllWithBooks
方法
$query = $em->createQuery('SELECT a, b FROM AB\ProjectBundle\Entity\Authors a JOIN a.books b');
$authors = $query->getResult();
 <tbody>
        {% for author in authors %}
    <tr>
        <td>{{ author.name }}</td>
        <td>{{ author.isactive }}</td>         
        <td>{{ author.books|length}}</td>
    </tr>
        {% endfor %}
 </tbody>