0

I am migrating from Doctrine 1.2 to Doctrine 2.1 and am having problems with what should be a simple join

This is my first table:

/**
 * Model_WBS
 *
 * @Table(name="WBS")
 * @Entity
 */
 class Model_Wbs 
   /**
    * @var integer $id
    *
    * @Column(name="id", type="integer", length=8)
    * @Id
    * @GeneratedValue(strategy="SEQUENCE")
    * @SequenceGenerator(sequenceName="wbs_seq_seq", allocationSize=1, initialValue=1)
    */
    public $id;
  .
  .
  .
   /**
    * @var \Doctrine\Common\Collections\ArrayCollection
    *
    * @OneToMany(targetEntity="Model_WbsFund", mappedBy="id")
    * @JoinColumn(name="id", referencedColumnName="wbs_id")
   */
   public $wbsfunds;

And this is my second table

/**
 * Model_WbsFund
 *
 * @Table(name="WBS_FUNDS")
 * @Entity
 */
class Model_WbsFund    
 /**
  * @var integer $id
  *
  * @Column(name="id", type="integer", length=8)
  * @Id
  * @GeneratedValue(strategy="SEQUENCE")
  * @SequenceGenerator(sequenceName="wbs_funds_seq_seq", allocationSize=1, initialValue=1)
  */
public $id;

/**
 * @var integer $wbs_id
 *
 * @Column(name="wbs_id", type="integer", length=8)
 */
public $wbs_id;
 .
 .
 .
  /**
   * @var \Doctrine\Common\Collections\ArrayCollection
   *
   * @ManyToOne(targetEntity="Model_Wbs", inversedBy="wbs_id")
   * @JoinColumn(name="wbs_id", referencedColumnName="id")
  */
  public $WBS;

And here is my simple test

$testEntity = Model_Wbs::find(7185);
foreach($testEntity->getwbsfunds() as $wbsfundobj){
  print("<br/>");
  print($wbsfundobj->FUND->getFundCode()." -- ".$wbsfundobj->WBS->getWbs());
}

So I would expect to see the fund code and the wbs from the wbsfunds table that are associated with the wbs I first searched. (I put in the ->WBS->getWbs() to see if I was actually getting just the funds related to wbs id 7185).

Instead, I get all wbs_funds records. I printed out the query statement doctrine generated and it's not correct

SELECT t0.id AS ID1, t0.wbs_id AS WBS_ID2, t0.fund_id AS FUND_ID3, t0.wbs_id AS WBS_ID4, t0.fund_id AS FUND_ID5 FROM WBS_FUNDS t0

The interesting thing is that the link between wbsfundobj->FUND->geFundCode() works great, as does the wbsfundobj->WBS->getWBS(), but the link from WBS to WBS_FUNDS seems wrong.

  • Prefer protected attributes instead of public ones. http://stackoverflow.com/questions/4090609/how-can-public-fields-break-lazy-loading-in-doctrine-2 – Florian Klein Feb 02 '12 at 23:29

1 Answers1

0

Typically, you call find() on the repository for a entity. Something like:

// $em is your Entity Manager instance
$testEntity = $em->getRepository('\Model_Wbs')->find(7185);

// or a shorcut
$testEntity = $em->find('\Model_Wbs', 7185);

How you get a reference to the EntityManager depends upon how you bootstrapped Doctrine. If you used the Bisna application resource plugin, then it would be something like (in controller):

$em = $this->getInvokeArg('bootstrap')->getResource('doctrine')->getEntityManager();

Typically, I put this into the controller's init() method or write an action helper that encapsulates the steps above so that I could call it using something like:

$em = $this->_helper->doctrine->getEntityManager();
David Weinraub
  • 14,144
  • 4
  • 42
  • 64