我正在構建一個查詢,該查詢將用于創建具有與用戶關聯的專案的帖子串列,并且在該結構中達到“tierAccess”的正確標準。
我的查詢生成器:
$qb = $this->em->createQueryBuilder();
foreach($subs as $sub)
{
if($sub->getDisabled() == true)
{
continue;
}
$qb->select('p')
->from('App\Entity\ProjectPost', 'p')
->where('project = '.$sub->getProject()->getId())
->andwhere('p.Published = true')
->andwhere('p.TierAccess = '.$sub->getProjectTier()->getId())
->orderBy('p.PostTime', 'DESC');
$query = $qb->getQuery();
$object[] = $query->execute();
}
我的目標是添加用戶訂閱允許的帖子,并在該訂閱中確保允許他們訪問該帖子(即:tierAccess)。
然后我回傳物件變數以傳遞給我的 Twig 模板檔案。
我收到的錯誤是:
[語意錯誤] 第 0 行,第 45 列靠近“專案 = 3 AND”:錯誤:“專案”未定義。
我的 ProjectPost 物體:
class ProjectPost
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $PostTitle;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $PostHero;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $PostType;
/**
* @ORM\Column(type="text")
*/
private $PostBody;
/**
* @ORM\ManyToOne(targetEntity=Project::class, inversedBy="projectPosts")
* @ORM\JoinColumn(nullable=false)
*/
private $Project;
/**
* @ORM\Column(type="array", nullable=true)
*/
private $TierAccess = [];
/**
* @ORM\Column(type="datetimetz", nullable=true)
*/
private $PostTime;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="projectPosts")
* @ORM\JoinColumn(nullable=true)
*/
private $PostBy;
/**
* @ORM\Column(type="array", nullable=true)
*/
private $PostCategories = [];
/**
* @ORM\Column(type="boolean")
*/
private $Published;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $PostCover;
/**
* @ORM\Column(type="boolean")
*/
private $PostSupporter = 0;
}
uj5u.com熱心網友回復:
基本錯誤是這個:
->where('p.Project = '.$sub->getProject()->getId())
請注意,您宣告p為 的別名Post,然后您不使用它。即使您將屬性定義為Project,您也試圖將其用作project。
盡管如此,整個事情還是很可疑的。在回圈中執行查詢通常表明設計有問題。
一種更簡單的方法,使用WHERE IN而不是回圈和多項選擇:
// get the "subs" ids in an array:
$subsIds = array_map(fn($s) => $s->getProject()->getId(), $subs);
qb->select('p')
->from('App\Entity\ProjectPost', 'p')
->where('p.Project IN :subsIds')
->andwhere('p.Published = true')
->andwhere('p.TierAccess = '.$sub->getProjectTier()->getId())
->orderBy('p.PostTime', 'DESC')
->setParameter('subsIds', $subsIds)
;
$result = $qb->getQuery()->getResult;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427360.html
上一篇:MySQL查詢:嘗試加入喜歡
