问题
I have the following DQL query:
public function findByIdJoinedToCodeExample($pageId)
{
$query = $this->getEntityManager()
->createQuery('
SELECT c FROM acmeStyleGuideBundle:codeExample c
JOIN c.PageContent p
WHERE p.codeExample = :cex'
)
->setParameter('cex', $pageId);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
It is attempting to retreive data from an entity called codeExample which has a ManyToOne relationship with an entity called PageContent. The relationships seem to be set up correctly as the database is being correctly set up and the fixtures are being populated but when I try to run the above query I am faced with the following error:
An exception has been thrown during the rendering of a template ("[Semantical Error] line 0, col 130 near 'codeExample =': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.") in acmeStyleGuideBundle:Page:pages.html.twig at line 16.
It is being called by the following controller:
// find the current pages code examples (if there are any)
public function findCodeExamplesAction($pageId =10)
{
$em = $this->getDoctrine()->getManager();
$codeExample = $this->getDoctrine()
->getRepository('acmeStyleGuideBundle:codeExample')
->findByIdJoinedToCodeExample($pageId);
return $this->render(
'acmeStyleGuideBundle:Page:codeExample.html.twig',
array(
'Code' => $codeExample
)
);
}
Note: $pageID = 10 is because it was telling me that pageID wasn't being populated. That seems to be a separate issue than this one so I set a default for that for now.
I've been looking at this for hours but I'm still learning Doctrine and Symfony and I just cannot figure this one out by myself.
Here are my entities for codeExample:
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="textExample", type="text")
*/
private $textExample;
/**
* @var string
*
* @ORM\Column(name="codeExample", type="text")
*/
private $codeExample;
/**
* @var integer
*
* @ORM\Column(name="lang", type="integer")
*/
private $lang;
/**
* @ORM\ManyToMany(targetEntity="PageContent", mappedBy="codeExample")
*/
protected $PageContent;
/**
* Constructor
*/
public function __construct()
{
$this->PageContent = new \Doctrine\Common\Collections\ArrayCollection();
}
Here are my entities for PageContent:
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection $pageSector_Id;
* @ORM\ManyToMany(targetEntity="pageSector", inversedBy="PageContent")
* @ORM\JoinTable(
* name="pageSector_PageContent",
* joinColumns={@ORM\JoinColumn(name="PageContent_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="pageSector_Id", referencedColumnName="id")}
* )
*/
protected $pageSector;
/**
* @var ArrayCollection $pageCategory_Id;
* @ORM\ManyToMany(targetEntity="pageCategory", inversedBy="PageContent")
* @ORM\JoinTable(
* name="pageCategory_PageContent",
* joinColumns={@ORM\JoinColumn(name="PageContent_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="pageCategory_Id", referencedColumnName="id")}
* )
*/
protected $pageCategory;
/**
* @ORM\ManyToOne(targetEntity="pageTypes", inversedBy="PageContent")
* @ORM\JoinColumn(name="pageTypesId", referencedColumnName="id")
*/
protected $pageTypes;
/**
* @var integer
*
* @ORM\Column(name="pageTypesId", type="integer")
*/
private $pageTypesId;
/**
* @ORM\OneToMany(targetEntity="PageContent", mappedBy="parentPage")
*/
private $childPages;
/** @ORM\ManyToOne(targetEntity="PageContent", inversedBy="childPages")
* @ORM\JoinColumn(name="parentPage_id", referencedColumnName="id")
**/
private $parentPage;
/**
* @var string
*
* @ORM\Column(name="pageName", type="string", length=255)
*/
private $pageName;
/**
* @var string
*
* @ORM\Column(name="pageUrl", type="string", length=255)
*/
private $pageUrl;
/**
* @var string
*
* @ORM\Column(name="richText", type="text")
*/
private $richText;
/**
* @var ArrayCollection $pageSector_Id;
* @ORM\ManyToMany(targetEntity="codeExample", inversedBy="PageContent")
* @ORM\JoinTable(
* name="codeExample_PageContent",
* joinColumns={@ORM\JoinColumn(name="PageContent_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="codeExample_Id", referencedColumnName="id")}
* )
*/
protected $codeExample;
Any advice you can give me would be amazing. I'm completely stuck here.
回答1:
I managed to work it out myself:
$query = $this->getEntityManager()
->createQuery('
SELECT c FROM acmeStyleGuideBundle:codeExample c
JOIN c.PageContent p
WHERE p.codeExample = :cex'
)
->setParameter('cex', $pageId);
Should have been:
$query = $this->getEntityManager()
->createQuery('
SELECT c FROM acmeStyleGuideBundle:codeExample c
JOIN c.PageContent p
WHERE p.id = :cex'
)
->setParameter('cex', $pageId);
This seems odd to me as I thought that the point of objects is to NOT use the id but it worked so that's good enough for me. If there is a better way, please feel free to tell me.
来源:https://stackoverflow.com/questions/19161223/dql-query-returns-statefieldpathexpression-or-singlevaluedassociationfield-expe