Zend Framework 2 Doctrine2 leftJoin创建一个单独的SELECT查询

时间:2014-06-17 13:14:32

标签: php zend-framework doctrine-orm doctrine zend-framework2

有谁知道为什么下面的Doctrine查询会为StandardDedescribed实体创建一个单独的连接?

正如您在下面看到的,此查询从主实体' EquipmentUK'中提取信息。和两个相关的表:' SchemaInfo'和'标准描述'。它适用于SchemaInfo,但为StandardDescribed创建另一个查询。实体中的关系似乎被编码得非常相似,所以我不明白为什么它对StandardDescribed的行为有所不同?

$qb->select('e')
->from('\Lookup\Entity\Fullhistory\EquipmentUK', 'e')
->leftJoin(
    '\Lookup\Entity\Fullhistory\SchemaInfo',
    'si',
    Expr\Join::WITH,
    'e.schema_id = si.schema_id AND si.parent_schema_id != si.schema_id'
)
->leftJoin(
    '\Lookup\Entity\Intelligence\StandardDescribedUK',
    'sd',
    Expr\Join::WITH,
    'e.schema_id = sd.schema_id'
)
->where($qb->expr()->eq('e.vehicle_id', '?1'))
->setParameter(1, $jatoid);

以下是实体:

EquipmentUK:     

namespace Lookup\Entity\Fullhistory;

use Doctrine\ORM\Mapping as ORM;

/**
 * EquipmentUK
 *
 * @ORM\Table(name="equipment",
 *  uniqueConstraints={
 *      @ORM\UniqueConstraint(name="search_idx", columns={"vehicle_id", "option_id", "record_id", "schema_id"})
 *  }
 * )
 * @ORM\Entity
 */
class EquipmentUK
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    private $vehicle_id;

    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    private $schema_id;

    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    private $option_id;

    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    private $record_id;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    private $location;

    /**
     * @ORM\ManyToOne(targetEntity="Lookup\Entity\Fullhistory\SchemaInfo", cascade={"all"}, fetch="EAGER")
     * @ORM\JoinColumn(name="schema_id", referencedColumnName="schema_id")     
     */
    private $schemaInfo;

    /**
     * @ORM\ManyToOne(targetEntity="Lookup\Entity\Intelligence\StandardDescribedUK", cascade={"all"}, fetch="EAGER")
     * @ORM\JoinColumn(name="schema_id", referencedColumnName="schema_id")     
     */
    private $standardDescribed;

    /**
     * @var string
     *
     * @ORM\Column(type="string")
     */
    private $data_value;

    public function getVehicleId()
    {
        return $this->vehicle_id;
    }

    public function getSchemaId()
    {
        return $this->schema_id;
    }

    public function getOptionId()
    {
        return $this->option_id;
    }

    public function getRecordId()
    {
        return $this->record_id;
    }

    public function getLocation()
    {
        return $this->location;
    }

    public function getSchemaInfo()
    {
        return $this->schemaInfo;
    }

    public function getStandardDescribed()
    {
        return $this->standardDescribed;
    }

    public function getDataValue()
    {
        return $this->data_value;
    }
}

SchemaInfo:     

namespace Lookup\Entity\Fullhistory;

use Doctrine\ORM\Mapping as ORM;

/**
 * SchemaInfo
 *
 * @ORM\Table(name="schema_info")
 * @ORM\Entity
 */
class SchemaInfo
{
    /**
     * @var integer
     *
     * @ORM\Column(name="schema_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $schema_id;

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

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

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

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


    public function getSchemaId()
    {
        return $this->schema_id;
    }

    public function getParentSchemaId()
    {
        return $this->parent_schema_id;
    }
}

StandardDescribedUK     

namespace Lookup\Entity\Intelligence;

use Doctrine\ORM\Mapping as ORM;

/**
 * StandardDescribedUK
 *
 * @ORM\Table(name="jato_uk_intelligence.standard_described")
 * @ORM\Entity
 */
class StandardDescribedUK
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $schema_id;

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

    public function getCategory()
    {
        return $this->category;
    }
}

1 个答案:

答案 0 :(得分:0)

您的联接应该使用Doctrine自己的关系架构:您的EquipmentUK已通过属性SchemaInfo声明了与$schemaInfo的关系,并且它还声明了它与{的关系{1}}通过StandardDescribedUK。所以:

$standardDescribed

希望这有帮助!