Doctrine inversedBy字段包含PersistentCollection

时间:2013-05-27 15:50:51

标签: mysql join doctrine

我有两个表之间的关系,第三个是额外的行,如question

/** @Entity 
*/
class Illness {


/** @Id @GeneratedValue @Column(type="integer") */
private $illness_id;

/** @Column(type="string") */
private $name;

/** @OneToMany(targetEntity="Illness_Symptom_Association", mappedBy="Illness") */
private $symptAssoc;

public function __construct()
{
    $this->symptAssoc = new \Doctrine\Common\Collections\ArrayCollection();

}

public function setName($val)
{
    $this->name = $val;
}

public function getName()
{
    return $this->name;
}



public function getSymptAssoc()
{
    return $this->symptAssoc;
}

}

使用第三个参数(类似优先级)加入表

/**
* @Entity 
*/
class Illness_Symptom_Association {

protected $illness_id;

/**
 * @Id()
 * @ManyToOne(targetEntity="Illness", inversedBy="symptAssoc") 
 * @JoinColumn(name="illness_id", referencedColumnName="illness_id", nullable=false) 
 */
protected $illness;

/**
 * @Id()
 * @ManyToOne(targetEntity="Symptom", inversedBy="symptAssoc")
 * @JoinColumn(name="symptom_id", referencedColumnName="symptom_id")
 * */
protected $symptom;

/** @Column(type="integer") */
protected $s;

public function setIllness(Illness $illness)
{
    $this->illness = $illness;
}

/**
 * 
 * @return Illness
 */
public function getIllness()
{
    return $this->illness;
}

public function setSymptom(Symptom $sympt)
{
    return $this->symptom = $sympt;
}

public function getSymptom()
{
    return $this->symptom;
}

public function setS($s)
{
    $this->s = $s;
}

public function getS()
{
    return $this->s;
  }

}

有症状的最后一个实体:

/**
* @Entity 
*/
class Symptom {

/** @Id @GeneratedValue @Column(type="integer") */
protected $symptom_id;

/** @Column(type="string", unique=true) */
protected $name;

/** @OneToMany(targetEntity="Illness_Symptom_Association", mappedBy="symptom") */
protected $symptAssoc;

public function setName($name)
{
    $this->name = $name;
}

public function getName($name)
{
    return $this->name;
}

}

但是

 $illness = $em->getRepository('Entity\Illness')->find($id);
 $illness->getSymptAssoc();

返回 PersistentCollection 对象。

如何通过连接表中的 s 参数获取症状列表?

1 个答案:

答案 0 :(得分:1)

PersistentCollection是Collection(在这种情况下是症状对象)。它已经是一系列症状。

您可以迭代它(就像数组一样),访问它的元素(在这种情况下是Symptom对象),或者将其转换为数组:

$illness->getSymptAssoc()->toArray();