PHP-Symfony2计算与另一个实体相关的实体字段
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-Symfony2计算与另一个实体相关的实体字段,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4305字,纯文字阅读大概需要7分钟。
内容图文
![PHP-Symfony2计算与另一个实体相关的实体字段](/upload/InfoBanner/zyjiaocheng/691/0e195b04e7eb4b97ad9276be5f1b53ed.jpg)
我有2个实体-作者和书籍,其中1个作者可能有很多书籍.我想在表格中显示每位作者有多少本书(每位作者的书数不同).我已经看过this问题,还有this和this并尝试了此方法,因为我认为这将是更优雅的解决方案:
<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>
编辑2我的作者实体
class Author
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* 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;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
编辑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;
}
}
没有注释,实体映射在* .orm.yml文件中.
Book.orm.yml:
AB\ProjectBundle\Entity\Books:
type: entity
table: Books
id:
id:
type: integer
nullable: false
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
name:
type: text
nullable: false
manyToOne:
authorid:
targetEntity: Author
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
authorid:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
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而不是$authorids-再次注意,因为我们得到的是作者而不是ID数组.我们加入书本(通过Author实体中称为“书本”的关系)以避免强迫Doctrine稍后再运行一个或多个单独的查询(如果不加入关系,则将延迟加载关系).
然后在Twig中,我们只是做
<tbody>
{% for author in authors %}
<tr>
<td>{{ author.name }}</td>
<td>{{ author.isactive }}</td>
<td>{{ author.books|length}}</td>
</tr>
{% endfor %}
</tbody>
最后的命名更改在这里{%for author in author%}完全是错误的方法.指出这看似微不足道或愚蠢,但编写简洁易懂的代码对于帮助他人(和您自己)了解实际情况至关重要.如果我读“作者”,那么我期望是一个作者数组,而不是包含一个作者信息的对象.
内容总结
以上是互联网集市为您收集整理的PHP-Symfony2计算与另一个实体相关的实体字段全部内容,希望文章能够帮你解决PHP-Symfony2计算与另一个实体相关的实体字段所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。