AppBundle类没有名称的关联

时间:2017-05-10 10:18:12

标签: symfony doctrine-orm

我对Doctrin 2和Symfony 3相对较新,并选择了一个项目。在创建自定义类时,我在处理映射问题时遇到了问题。

总之,当我运行以下内容时:

    <?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();
        }
    }

我收到以下错误:

"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"

我的表单实体如下:

<?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;

  ......

}

我的文档版本实体看起来像:

<?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;
.....

任何人都可以帮我解决我做错的事吗?非常感谢提前!

2 个答案:

答案 0 :(得分:3)

在查询构建器中,您应该使用属性名称而不是SQL列名称。

你有一个财产:

admin_countries_url

因此而不是:

private $document;

尝试:

$query->join('f.documentVersion', 'dv');

答案 1 :(得分:2)

您已在表单中定义private $document;以保存documentVersion实体:

$query->join('f.documentVersion', 'dv');

应该是该属性的名称:

$query->join('f.document', 'dv');

基本上是错误:

  

错误:类AppBundle \ Entity \ Form没有指定的关联   documentVersion

是说我试图在实体表格上访问documentVersion但是我找不到它/它的getter / setter