Answer the question
In order to leave comments, you need to log in
How to disable lazy initialization of entities in Doctrine for the current request?
Good day to all.
Today I encountered the following very strange problem.
What is available:
PHP on Windows 7 x64, version 5.3.17, Apache version 2.4 (at work 2.2), PHP is included as a module. Symfony version 2.0.16, Doctrine version 2.1.
Three tables were created for the test - а
, b
, and a_b
, storing many-to-many relationships. Test entities Doctrine MyUsers and MyComments were created, binding was made through metadata to these tables. At this stage, everything is fine. Here is the code that is contained in the entity classes:
<?php
namespace TTCom\BillingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TTCom\BillingBundle\Entity\MyUsers
*
* @ORM\Table(name="a")
* @ORM\Entity
*/
class MyUsers
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="id", type="integer", nullable=false)
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="varchar(255)", nullable=false)
*/
private $name;
/**
* Bidirectional - Many users have Many favorite comments (OWNING SIDE)
*
* @ORM\ManyToMany(targetEntity="MyComments", inversedBy="userFavorites")
* @ORM\JoinTable(name="a_b",
* joinColumns={@ORM\JoinColumn(name="a_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="b_id", referencedColumnName="id")}
* )
*/
private $favorites;
public function getName() {
return $this->name;
}
public function getFavoriteComments() {
return $this->favorites;
}
public function addFavorite(Comment $comment) {
$this->favorites->add($comment);
$comment->addUserFavorite($this);
}
public function removeFavorite(Comment $comment) {
$this->favorites->removeElement($comment);
$comment->removeUserFavorite($this);
}
}
<?php
namespace TTCom\BillingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TTCom\BillingBundle\Entity\MyComments
*
* @ORM\Table(name="b")
* @ORM\Entity
*/
class MyComments
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="id", type="integer", nullable=false)
*/
private $id;
/**
* @var string $text
*
* @ORM\Column(name="text", type="varchar(255)", nullable=false)
*/
private $text;
/**
* Bidirectional - Many comments are favorited by many users (INVERSE SIDE)
*
* @ORM\ManyToMany(targetEntity="MyUsers", mappedBy="favorites")
*/
private $userFavorites;
public function getCommentText() {
return $this->text;
}
public function getUserFavorites() {
return $this->userFavorites;
}
}
public function testAction(Request $request) {
$em = $this->getDoctrine()->getEntityManager();
$user = $em->getRepository('TTComBillingBundle:MyUsers')->findOneById(1);
$fav = $user->getFavoriteComments();
//return new Response(print_r(\Doctrine\Common\Util\Debug::dump($fav[0]), true)); //1
//return new Response(count($fav)); //2
$a = array();
//count($fav); //3
//for ($i = 0; $i < count($fav); $i++) {
//print_r(\Doctrine\Common\Util\Debug::dump($fav[$i])); //4
//$a[] = $fav[$i]->getCommentText(); //5
//}
//array_walk($fav, function($el, $i) use ($a, $fav) { print_r(\Doctrine\Common\Util\Debug::dump($el)); $a[] = $fav[$i]->getCommentText(); }); //6
print_r($a);
//return new Response(print_r($f[0], true)); //7
//return new Response(print_r(\Doctrine\Common\Util\Debug::dump($user->getFavoriteComments()), true)); //8
return new Response($user->getName().": ".implode(", ", $a)); //9
$objects = $em->getRepository('TTComBillingBundle:AlarmObject')->getList();
$events = $em->getRepository('TTComBillingBundle:AlarmLogEntry')->getList();
return $this->render('TTComBillingBundle:Default:alarm.html.twig',array('objects' => $objects, 'events' => $events));
}
array(0) {}
, as well as an error in the log likePHP Fatal error: Call to a member function getCommentText() on a non-object
array(2) {
[0]=>
object(stdClass)#548 (4) {
["__CLASS__"]=>
string(37) "TTCom\BillingBundle\Entity\MyComments"
["id"]=>
int(1)
["text"]=>
string(2) "b1"
["userFavorites"]=>
string(8) "Array(2)"
}
[1]=>
object(stdClass)#540 (4) {
["__CLASS__"]=>
string(37) "TTCom\BillingBundle\Entity\MyComments"
["id"]=>
int(2)
["text"]=>
string(2) "b2"
["userFavorites"]=>
string(8) "Array(1)"
}
}
print_r(\Doctrine\Common\Util\Debug::dump($el));
inside the callback function, as well as a non-empty array $a containing two elements: "b1" and "b2". Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question