【问题标题】:Zend Framework 2 Doctrine2 leftJoin creating a separate SELECT queryZend Framework 2 Doctrine2 leftJoin 创建单独的 SELECT 查询
【发布时间】:2014-06-17 13:14:31
【问题描述】:

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

正如您在下面看到的,此查询从主实体“EquipmentUK”和两个相关表中提取信息:“SchemaInfo”和“StandardDescribed”。它适用于 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);

这里是实体:

设备英国:

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

架构信息:

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

标准描述英国

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

【问题讨论】:

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


    【解决方案1】:

    你的连接应该使用 Doctrine 自己的关系模式:你的EquipmentUK 已经通过属性$schemaInfo 声明了它与SchemaInfo 的关系,它还通过$standardDescribed 声明了它与StandardDescribedUK 的关系。所以:

    $qb->select('e')
        ->from('\Lookup\Entity\Fullhistory\EquipmentUK', 'e')
        ->leftJoin(
            'e.schemaInfo',
            'si'
            //we don't need that third param as the relation is already known
        )
        ->leftJoin(
            'e.standardDescribed',
            'sd'
            //same here, no need for explicit relation
        )
        ->where($qb->expr()->eq('e.vehicle_id', ':vehicle'))
        //this here is a restriction, should be in WHERE instead of ON
        ->andWhere($qb->expr()->neq('si.parent_schema_id', 'si.schema_id'))
        ->setParameter('vehicle', $jatoid);
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2014-03-24
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      相关资源
      最近更新 更多