我有文章和类别实体。该实体与映射没有关系,但我需要使用查询构建器将类别加入到文章中,因此我需要在类别实体中注射的文章实体。我该怎么做?
我搜索了Google,但没有找到解决方案
class ArticleRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Article::class);
}
public function getAll()
{
$query = $this->createQueryBuilder('articles')
->select('articles, category')
->join(AppEntityCategory::class, 'category', 'WITH', 'articles.categoryId = category.id')
->getQuery();
echo '<pre>';
print_r($query->getResult());
exit;
}
?>
// output
Array
(
[0] => AppEntityArticle Object
(
[id:AppEntityArticle:private] => 1
[name:AppEntityArticle:private] => title1
[text:AppEntityArticle:private] => text1
[categoryId:AppEntityArticle:private] => 1
[categories:AppEntityArticle:private] =>
)
[1] => AppEntityCategory Object
(
[id:AppEntityCategory:private] => 1
[name:AppEntityCategory:private] => first cat
)
[2] => AppEntityArticle Object
(
[id:AppEntityArticle:private] => 2
[name:AppEntityArticle:private] => title2
[text:AppEntityArticle:private] => text2
[categoryId:AppEntityArticle:private] => 1
[categories:AppEntityArticle:private] =>
)
[3] => AppEntityArticle Object
(
[id:AppEntityArticle:private] => 3
[name:AppEntityArticle:private] => title3
[text:AppEntityArticle:private] => text3
[categoryId:AppEntityArticle:private] => 2
[categories:AppEntityArticle:private] =>
)
[4] => AppEntityCategory Object
(
[id:AppEntityCategory:private] => 2
[name:AppEntityCategory:private] => second cat
)
)
尝试以下方式:
->select('articles.id','articles.name','articles.text','articles.categoryId','articles.categories','category.id', 'category.name')
而不是
->select('articles, category')