我在收到一個關聯以填充我的Doctrine實體時遇到問題。即使設置爲急切加載,實體也會充分填充關聯的唯一例外。我有其他類似的協會工作,所以我懷疑有一些基本的理解,我在這裏失蹤。學說2關聯映射總是返回null
我在這裏要做的是在對象的實體中填充$s
,在查詢中被稱爲s
。我事先爲命名道歉,但我不得不剝奪任何可能識別的東西,因爲這是專有代碼的一部分。
這裏是我的SHealth
實體類的散裝:
// -------------------------------------------------------------------
// ENTITY FOR SHealth
// -------------------------------------------------------------------
/**
* Used for tracking the current health of shares.
* @Entity(repositoryClass="SHealthRepository")
* @Table(name="s_health")
*/
class SHealth
{
/**
* @Id
* @Column(type="integer", name="s_id")
*/
protected $sId;
/**
* @Id
* @Column(type="integer", name="l_id")
*/
protected $lId;
/**
* @Id
* @Column(type="smallint", name="s_type")
*/
protected $sType;
/**
* @Id
* @Column(type="smallint", name="s_subtype")
*/
protected $sSubtype;
/**
* @Column(type="smallint", name="health_status")
*/
protected $healthStatus;
/**
* @Column(type="datetime", name="update_time")
*/
protected $updateTime;
/**
* Scalar value
*/
protected $active;
/**
* @ManyToOne(targetEntity="S")
* @JoinColumns({
* @JoinColumn(name="l_id", referencedColumnName="l_id"),
* @JoinColumn(name="s_id", referencedColumnName="id")
* })
*/
protected $s;
// [Accessors and mutators omitted]
}
下面是相關的倉儲類的一個片段:
// -------------------------------------------------------------------
// Repository fetch function
// -------------------------------------------------------------------
$rtime_check = !$include_rtimed ? " AND s.rtime IS NULL" : "";
$limit_check = $limit > 0 ? " LIMIT " . $limit : "";
$sql = "SELECT
s.l_id,
s.id AS s_id,
COALESCE(s_health.s_type, s.type) AS s_type,
COALESCE(s_health.s_subtype, 0) AS s_subtype,
s_health.health_status,
s_health.update_time,
(s.enabled AND
COALESCE(orsr.status, orsh.status, 0) > 0) AS active
FROM s
LEFT JOIN s_health ON
s.l_id = s_health.l_id AND
s.id = s_health.s_id AND
s.type = s_health.s_type AND
s_health.s_subtype = 0
LEFT JOIN orsr ON
s.l_id = orsr.l_id AND
s.se_id = orsr.se_id AND
orsr.status IN ([omitted])
LEFT JOIN orsh ON
s.l_id = orsh.l_id AND
s.id = orsh.s_id AND
orsh.status IN ([omitted])
WHERE s.l_id IN (:d_ids)
{$rtime_check}
GROUP BY s.l_id, s.id
{$limit_check}";
// Map the SQL result columns to class properties.
$rsm = new ResultSetMappingBuilder($this->_em);
$rsm->addRootEntityFromClassMetadata(SHealth::class, 's_alias');
$rsm->addScalarResult('active', 'active');
$query = $this->_em->createNativeQuery($sql, $rsm);
$query->setParameter("d_ids", $d_ids);
$results = $query->getResult();
// Inject aggregate function results into the resulting object.
$health_objects = [];
foreach ($results as $result)
{
$health_object = $result[0];
$health_object->setActive($result['active']);
$health_objects[] = $health_object;
}
return $health_objects;
最後,這裏的S
類,有一些成員刪除:
// -------------------------------------------------------------------
// ENTITY FOR S
// -------------------------------------------------------------------
/**
* @Entity
* @Table(name="s")
*/
class S
{
/**
* @Id
* @Column(type="integer")
*/
protected $id;
/**
* @Id
* @Column(type="integer", name="l_id")
*/
protected $lId;
/**
* @Column(type="integer", name="se_id")
*/
protected $seId;
/**
* @Column(type="smallint")
*/
protected $type;
/**
* @Column(type="boolean")
*/
protected $enabled;
/**
* @Column(type="datetime")
*/
protected $rtime;
// [Accessors and mutators omitted]
}
我擁有所有必要的吸氣劑s和setter,並且所有必要的數據庫數據都存在於兩個表中,因此連接列應該沒有任何問題。
我無法使用單個連接列,因爲目標實體的主鍵是組合鍵。沒有'l_id','id'不是唯一的。 –
@ BrandonJ.Dusseau啊,我明白了,現在我錯過了那部分。我會再看一次。 – Wilt
@ BrandonJ.Dusseau我更新了,不知道這是否適用於兩個連接列雖然... – Wilt