0

I have relation between two tables through third with extra row (s) like in this 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;
}

}

Join table with third param s (something like priority)

/**
* @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;
  }

}

Last entity with symptoms:

/**
* @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;
}

}

But

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

return PersistentCollection oject.

How I can get list of symptoms with s param from join table?

Community
  • 1
  • 1
Yuri Taratorkin
  • 607
  • 6
  • 19

1 Answers1

1

A PersistentCollection is a Collection (of Symptom objects, in this case). It already is a list of symptoms.

You can iterate it (just like an array), access its elements (which are Symptom objects in this case), or convert it to an array:

$illness->getSymptAssoc()->toArray();
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • symptom propertye of **Illness_Symptom_Association** inside symptAssoc collection return **DoctrineORMModule\Proxy\__CG__\Application\Entity\Symptom** instead of **Symptom** mapper. What could be the reason for this? – Yuri Taratorkin May 27 '13 at 21:48
  • It's a generated sub-class of Symptom used to lazy-load entities; you can use it as if it was a Symptom object. – Arnaud Le Blanc May 28 '13 at 08:24