两个实体之间的学说关系交叉连接

时间:2014-12-23 11:35:22

标签: doctrine-orm symfony-2.3

我使用了doctrine并且我想在两个实体之间进行交叉连接查询,但我不能这样做,因为在学说中不存在交叉连接。

这是我的sql查询,它将被转移到查询构建器:

select  o.id, c.id
from operation o cross join catalog_type_document c
left join type_document t on t.idnt_typ_docm=c.idnt_typ_docm and o.idnt_oprt=t.idnt_oprt

这是我的查询,我想用查询构建器但不起作用:

$query = $this->getEntityManager()->createQueryBuilder()
        ->select('o, c')
        ->from('DEMOTestBundle:Operation c, DEMOTestBundle:CatalogueTypeDocument c', '')
        ->leftJoin('c.typeDocuments', 't', Expr\Join::WITH, 't.operation=o')
        ->getQuery();
    $entities = $query->getResult;

任何建议都将非常感激。

2 个答案:

答案 0 :(得分:0)

首先......我们能看到实体之间的关系(yaml,xml还是符号)? 您的查询中有一些奇怪的东西...... 您只需从交叉连接表中选择ID即可 - 左连接不要排序行... 你想要选择什么?

答案 1 :(得分:0)

以下是实体之间的关系:

class Operation {

/**
 * @var integer
 *
 * @ORM\Column(name="IDNT_OPRT", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="NOM_OPRT", type="string", length=45, nullable=false)
 */
private $name;

 /**
 * @var DateTime
 *
 * @ORM\Column(name="DATE_CREATION", type="datetime", nullable=true)
 */
private $date;


/**
 * @ORM\OneToMany(targetEntity="Demo\TestBundle\Entity\TypeDocument", mappedBy="operation")
 */
protected $typeDocuments;

}

class CatalogeTypeDocument{

/**
 * @var integer
 *
 * @ORM\Column(name="IDNT_TYP_DOCM", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="NOM_TYP_DOCM", type="string", length=255, nullable=false)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="DATETYPEDOC", type="date", nullable=true)
 */
private $date;

/**
 * @var string
 *
 * @ORM\Column(name="OBS", type="string", length=255, nullable=true)
 */
private $obs;


/**
 * @var \Demo\TestBundle\Entity\Chapitre
 *
 * @ORM\ManyToOne(targetEntity="Demo\TestBundle\Entity\Chapitre")
 * @ORM\JoinColumn(name="IDNT_CHPT", referencedColumnName="IDNT_CHPT")
 * @ORM\OrderBy({"id" = "ASC"})
 */
private $chapitre;

}

class TypeDocument {

/**
 * @var integer
 *
 * @ORM\Column(name="IDNT_TYP_DOCM", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="NOM_TYP_DOCM", type="string", length=255, nullable=false)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="ABREVIATION", type="string", length=45, nullable=false)
 */
private $abreviation;

/**
 * @var string
 *
 * @ORM\Column(name="OBS", type="string", length=255, nullable=true)
 */
private $obs;


/**
 * @var string
 *
 * @ORM\Column(name="DATETYPEDOC", type="date", nullable=true)
 */
private $date;


/**
 * @var integer
 *
 * @ORM\Column(name="ORDRE_TYPE", type="integer", nullable=true)
 */
private $order;


/**
 * @var \Demo\TestBundle\Entity\CatalogeTypeDocument
 *
 * @ORM\ManyToOne(targetEntity="Demo\TestBundle\Entity\CatalogeTypeDocument")
 * @ORM\JoinColumn(name="REF_IDNT_ORG", referencedColumnName="IDNT_TYP_DOCM", onDelete="CASCADE")
 */
private $catalogeTypeDocument;


/**
 * @var \Demo\TestBundle\Entity\Operation
 *
 * @ORM\ManyToOne(targetEntity="Demo\TestBundle\Entity\Operation")
 * @ORM\JoinColumn(name="IDNT_OPRT", referencedColumnName="IDNT_OPRT")
 */
private $operation;

/**
 * @ORM\OneToMany(targetEntity="Demo\TestBundle\Entity\Document", mappedBy="typeDocument")
 * @ORM\OrderBy({"idDocm" = "ASC", "version" = "DESC"})
 */
protected $documents;

}

我要显示的字段是:

$query = $this->getEntityManager()->createQueryBuilder()
        ->select('o, c.id, c.name, t.id, t.name, t.obs, t.ordre, d')
        ->from('DEMOTestBundle:Operation c, DEMOTestBundle:CatalogueTypeDocument c', '')
        ->leftJoin('c.typeDocuments', 't', Expr\Join::WITH, 't.operation=o')
        ->innerJoin('t.documents', 'd')
        ->getQuery();
    $entities = $query->getResult();