我在收到一个关联以填充我的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