Circular reference with OneToMany/ManyToOne

dear all,

i’m trying to build up a NeosFlow-application with serveral ManyToOne/OneToMany relations. i think i succeeded in using the correct parameters now, but php throws me now Memory size errors, so i do have the feeling to have circular references within my setup, but i have no idea where :((((

the database model is that way that i have a Library-Model, which has Topics as children, and these topics have questions, and and questions have answers, so
Library (1:n) Topic (1:n) Question (1:n) Answer

i’ve setup the following relations:

class Topic {
/**
* @var integer
* @ORM\Id
* @ORM\GeneratedValue(strategy=“AUTO”)
* @ORM\Column(name=“id”, type=“integer”, nullable=false)
*/
protected $id;

/**
 * Library for this topic
 * @var Library
 * @ORM\ManyToOne(inversedBy="topics", cascade={"persist"}, fetch="EXTRA_LAZY")
 * @ORM\JoinColumn(name="library_id", referencedColumnName="id", nullable=false)
 */
protected $library;

}

and

class Library
{
/**
* @var integer
* @ORM\Id
* @ORM\GeneratedValue(strategy=“AUTO”)
* @ORM\Column(name=“id”, type=“integer”, nullable=false)
*/
protected $id;

/**
 * Topics for this library
 * @var Collection<Topic>
 * @ORM\OneToMany(mappedBy="library", cascade={"persist"}, fetch="EXTRA_LAZY")
 */
protected $topics;

}

and similar to for all other relations.

  • if i’m running the action
    public function showtopicAction($id) {
    $t = $this->topicRepository->findById($id)->getFirst();
    $this->view->assign(“topic”, $t);
    }
    i get the error “PHP Fatal error: Allowed memory size of 536870912 bytes exhausted”

  • if i try to output the topic using the JsonView everything seems to be fine, i get the topic information

so my questions are:

  • how to find out if there are circular references?
  • is there some similar function to JsonView::setConfiguration() (to limit the output just to some fields and relations)???

ciao
H.

How are you rendering the topic variable in the frontend view?

dear Søren,

like this:

                    <td>{topic.id}</td>
                    <td>{topic.type}</td>
                    <td>{topic.name}</td>

the strange this i noticed some minutes ago is, that even a

function testAction() {
$n = new Topic()
$this->view->assign(‘topic’, $t);
}

results in a memory error ?!?!

ciao
H.

What if you leave out the $this->view->assign code - does it still give you a memory error?

servas,

finally i’ve found out that i made a very stupid error :(((((((((

since i’m a beginner in NeosFlow and i struggled around with the ORM\OneToMany-statements i changed some lines in the code to printout variables ‘print_r($subject)’ - and forgot this on the next day. well, and i think a print_r on a model will iterate through all cascaded models, and thus will result in circular references …

i’m very sorry for this stupid error and to bother your time!

ciao
H.

Don’t be sorry, we’ve all been there.
We’re glad that you got it sorted!

I have done the exact same thing many many times :smiley: It’s a awesome debugging method, untill it’s not :stuck_out_tongue: Glad you got it solved :partying_face: