In PHP Symfony project with doctrine, I am using following class with self-referencing attribute “credit” and another attribute referencing class foo as follow.
<code>/**
* Bill
*
* @ORMTable(name="bill")
* @ORMEntity(repositoryClass="BillRepository")
*/
class Bill {
/**
* @var Bill
* @ORMManyToOne(targetEntity="Bill")
* @ORMJoinColumn(name="credit_id", nullable=true)
*/
private $credit;
public function getCredit() {return $this->credit;}
/**
* @var Foo
* @ORMManyToOne(targetEntity="Foo")
* @ORMJoinColumn(name="foo_id", nullable=true)
*/
private $foo;
public function getFoo() { return $this->foo; }
public function getCreditFooDirectAccess() { return $this->getCredit()->foo; }
public function getCreditFooMethodAccess() { return $this->getCredit()->getFoo(); }
}
/**
* Foo
*
* @ORMTable(name="foo")
* @ORMEntity(repositoryClass="FooRepository")
*/
class Foo {
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
public function getId() { return $this->id; }
}
</code>
<code>/**
* Bill
*
* @ORMTable(name="bill")
* @ORMEntity(repositoryClass="BillRepository")
*/
class Bill {
/**
* @var Bill
* @ORMManyToOne(targetEntity="Bill")
* @ORMJoinColumn(name="credit_id", nullable=true)
*/
private $credit;
public function getCredit() {return $this->credit;}
/**
* @var Foo
* @ORMManyToOne(targetEntity="Foo")
* @ORMJoinColumn(name="foo_id", nullable=true)
*/
private $foo;
public function getFoo() { return $this->foo; }
public function getCreditFooDirectAccess() { return $this->getCredit()->foo; }
public function getCreditFooMethodAccess() { return $this->getCredit()->getFoo(); }
}
/**
* Foo
*
* @ORMTable(name="foo")
* @ORMEntity(repositoryClass="FooRepository")
*/
class Foo {
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
public function getId() { return $this->id; }
}
</code>
/**
* Bill
*
* @ORMTable(name="bill")
* @ORMEntity(repositoryClass="BillRepository")
*/
class Bill {
/**
* @var Bill
* @ORMManyToOne(targetEntity="Bill")
* @ORMJoinColumn(name="credit_id", nullable=true)
*/
private $credit;
public function getCredit() {return $this->credit;}
/**
* @var Foo
* @ORMManyToOne(targetEntity="Foo")
* @ORMJoinColumn(name="foo_id", nullable=true)
*/
private $foo;
public function getFoo() { return $this->foo; }
public function getCreditFooDirectAccess() { return $this->getCredit()->foo; }
public function getCreditFooMethodAccess() { return $this->getCredit()->getFoo(); }
}
/**
* Foo
*
* @ORMTable(name="foo")
* @ORMEntity(repositoryClass="FooRepository")
*/
class Foo {
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
public function getId() { return $this->id; }
}
Now the two following controllers are showing different results. The direct access on displays NULL whereas the method access one displays some ID. Any ideas ? I am wondering if this is related to the way I am fetching the data.
<code>public function testDirectAccessAction(Request $request)
{
$dql = "SELECT bill FROM Bill bill";
$bill = $this->getEm()->createQuery($sql)->getFirstResult();
var_dump($bill->getCreditFooDirectAccess() ? $bill->getId() : null);
die;
}
public function testMethodAccessAction(Request $request)
{
$dql = "SELECT bill FROM Bill bill";
$bill = $this->getEm()->createQuery($sql)->getFirstResult();
var_dump($bill->getCreditFooMethodAccess() ? $bill->getId() : null);
die;
}
</code>
<code>public function testDirectAccessAction(Request $request)
{
$dql = "SELECT bill FROM Bill bill";
$bill = $this->getEm()->createQuery($sql)->getFirstResult();
var_dump($bill->getCreditFooDirectAccess() ? $bill->getId() : null);
die;
}
public function testMethodAccessAction(Request $request)
{
$dql = "SELECT bill FROM Bill bill";
$bill = $this->getEm()->createQuery($sql)->getFirstResult();
var_dump($bill->getCreditFooMethodAccess() ? $bill->getId() : null);
die;
}
</code>
public function testDirectAccessAction(Request $request)
{
$dql = "SELECT bill FROM Bill bill";
$bill = $this->getEm()->createQuery($sql)->getFirstResult();
var_dump($bill->getCreditFooDirectAccess() ? $bill->getId() : null);
die;
}
public function testMethodAccessAction(Request $request)
{
$dql = "SELECT bill FROM Bill bill";
$bill = $this->getEm()->createQuery($sql)->getFirstResult();
var_dump($bill->getCreditFooMethodAccess() ? $bill->getId() : null);
die;
}
Still working on isolating the problem some provided code is more like pseudo-code for now.
Thanks