现在有两张表,category 和 article。
category:
/** * @Mapping\Entity * @Mapping\Table(name="category") */ class Category { /** * @Mapping\Id * @Mapping\Column(type="integer") * @Mapping\GeneratedValue(strategy="AUTO") */ protected $id; /** * @Mapping\OneToMany(targetEntity="Article", mappedBy="category") */ protected $articles; // ... }
article:
<code>/** * @Mapping\Entity * @Mapping\Table(name="article") */ class Article { /** * @Mapping\Id * @Mapping\Column(type="integer") * @Mapping\GeneratedValue(strategy="AUTO") */ protected $id; /** * @Mapping\ManyToOne(targetEntity="Category", inversedBy="articles") * @Mapping\JoinColumn(name="categoryid", referencedColumnName="id") */ protected $category; /** * @Mapping\Column(type="string", columnDefinition="enum('published', 'draft')") */ protected $status; // ... }</code>
我的问题是, $category->getArticles() 方法可以获取指定分类下的 articles,但是,如果我只想获取 status="published" 时怎么办呢?我看过文档,oneToMany 中不能设定条件
同时,在 twig 中使用 category.articles|length 来显示指定分类下的文章数,也只能返回全部的文章,这种场景怎么写呢?。
另外,我现在在 CategoryRepository 中使用 JOIN 加上条件查询出来,但在 twig 中显示出来的结果就还是忽略了 status 条件:
return $this->createQueryBuilder('c') ->leftJoin('c.articles', 'a', 'WITH', 'a.status = :status') ->groupBy('c.id') ->setParameter('status', 'published') ->getQuery() ->getResult();
回复内容:
现在有两张表,category 和 article。
category:
/** * @Mapping\Entity * @Mapping\Table(name="category") */ class Category { /** * @Mapping\Id * @Mapping\Column(type="integer") * @Mapping\GeneratedValue(strategy="AUTO") */ protected $id; /** * @Mapping\OneToMany(targetEntity="Article", mappedBy="category") */ protected $articles; // ... }
article:
<code>/** * @Mapping\Entity * @Mapping\Table(name="article") */ class Article { /** * @Mapping\Id * @Mapping\Column(type="integer") * @Mapping\GeneratedValue(strategy="AUTO") */ protected $id; /** * @Mapping\ManyToOne(targetEntity="Category", inversedBy="articles") * @Mapping\JoinColumn(name="categoryid", referencedColumnName="id") */ protected $category; /** * @Mapping\Column(type="string", columnDefinition="enum('published', 'draft')") */ protected $status; // ... }</code>
我的问题是, $category->getArticles() 方法可以获取指定分类下的 articles,但是,如果我只想获取 status="published" 时怎么办呢?我看过文档,oneToMany 中不能设定条件
同时,在 twig 中使用 category.articles|length 来显示指定分类下的文章数,也只能返回全部的文章,这种场景怎么写呢?。
另外,我现在在 CategoryRepository 中使用 JOIN 加上条件查询出来,但在 twig 中显示出来的结果就还是忽略了 status 条件:
return $this->createQueryBuilder('c') ->leftJoin('c.articles', 'a', 'WITH', 'a.status = :status') ->groupBy('c.id') ->setParameter('status', 'published') ->getQuery() ->getResult();
class Category
{
...
public function getPublishedArticles()
{
<code>$result = new ArrayCollection(); foreach ($this->articles as $article) { if ($article->getStatus() == 'published') { $result[] = $article; } } return $result;</code>
}
从articles的角度解决问题。
在controller里面
<code>$articles = $this ->getDoctrine() ->getRepository('Article') ->findBy([ 'category' => $category, 'status' => 'published', ]);</code>
然后view里面就可以用articles|length了。

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。

自動加載PHP會在需要時自動加載類文件,從而通過減少內存使用和增強代碼組織來提高性能。最佳實踐包括使用PSR-4和有效組織代碼。

本文討論了在PHP中管理文件上傳大小的管理,重點是2MB的默認限制以及如何通過修改PHP.INI設置來增加它。

本文討論了PHP 7.1中引入的PHP中的無效類型,允許變量或參數為指定類型或NULL。它突出顯示了諸如提高可讀性,類型安全性和明確意圖的好處,並解釋瞭如何聲明

本文討論了unset()和unlink()功能在編程中的差異,重點關注其目的和用例。 unset()從內存中刪除變量,而unlink()從文件系統中刪除文件。兩者都對效率至關重要


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具