Reputation: 9457
I am relatively new to Doctrin 2 and Symfony 3 and picking up a project. I'm having trouble working on a mapping issue when I create a custom class.
In short when I run the following:
<?php
namespace AppBundle\Repository;
/**
* FormRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class FormRepository extends \Doctrine\ORM\EntityRepository
{
public function findWithDocId($id, $clientId=null) {
$docId = '2342';
$query = $this->createQueryBuilder('f');
$query->join('f.documentVersion', 'dv');
$query->where('dv.doc_id = :docId')->setParameter('docId', $docId);
$query->andWhere('f.id = :id')->setParameter('id',$id);
return $query->getQuery()->getOneOrNullResult();
}
}
I get the following error:
"cls": "Doctrine\\ORM\\Query\\QueryException",
"errors": [
"[Semantical Error] line 0, col 67 near 'dv WHERE dv.doc_id': Error: Class AppBundle\\Entity\\Form has no association named documentVersion"
My Form Entity looks like:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\VirtualProperty;
use JMS\Serializer\Annotation\Type;
use Doctrine\Common\Collections\Criteria;
use JMS\Serializer\Annotation\AccessorOrder;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\FormRepository")
* @ORM\Table(name="forms")
* @AccessorOrder("custom", custom = {"id"})
*/
class Form {
......
/**
* @ORM\OneToOne(targetEntity="AppBundle\Entity\DocumentVersion", inversedBy="form")
* @ORM\JoinColumn(name="doc_id", referencedColumnName="id", nullable=false, unique=true, onDelete="CASCADE")
* @SerializedName("document_version")
* @Groups({"form_details_document"})
*/
private $document;
......
}
And my Document Version Entity looks like:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;
/**
* @ORM\Entity
* @ORM\Table(name="document_versions")
*/
class DocumentVersion {
.....
/**
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Form", mappedBy="document", orphanRemoval=true, cascade={"persist"})
* @Groups({"document_versions"})
*/
private $form;
.....
Can any one help me in what I am doing wrong? Big thanks in advance!
Upvotes: 0
Views: 1636
Reputation: 15656
In query builder you should use property names instead of SQL column names.
You have a property:
private $document;
Therefore instead of:
$query->join('f.documentVersion', 'dv');
try with:
$query->join('f.document', 'dv');
Upvotes: 3
Reputation: 883
You have defined private $document;
in form to hold the documentVersion entity so:
$query->join('f.documentVersion', 'dv');
Should be the name of that property:
$query->join('f.document', 'dv');
Basically the error:
Error: Class AppBundle\Entity\Form has no association named documentVersion
Is saying im trying to access documentVersion on entity Form but I cant find it/its getter/setter
Upvotes: 2