Object is too big when load from database symfony

268 Views Asked by At

I need load and object from the database and need to store it in session .

The problem is that the loaded object is too big that when i print it my browse crushes .

How can i load just the pure object from the database ?

And here is the code :

if ($session->get('record')->getId()) {
    $record = $this->container->get('myweb.record_repository')->findOneById($session->get('record')->getId());
    $session->set('record', $record);
    print_r($session->get('record'));
    die;
}
2

There are 2 best solutions below

8
Alex Pab On

Try to hydrate as array... in symfony there is the option to Query::HYDRATE_ARRAY and then normalize it to an object

0
Preciel On

You're not dumping your code correctly.

With Symfony, there is the dump() function.

You can either use it in controller, or in twig.

In controller:

//Check if parameter exists first, else you might trigger an error
if($session->has('record') && $session->get('record')->getId() !== null) {
    $record = $this->container->get('myweb.record_repository')->findOneById($session->get('record')->getId());
    $session->set('record', $record);
    dump($session->get('record'));
    exit();
}

In Twig (you can pass a variable name)

{{ dump() }}

Then again, like I said in comment, it's most likely pointless to store your object in session.
Doctrine will query for the object most of the time.

Lets say you do this:

$relatedEntity->getRecord();

Doctrine won't look into the session for the object, it will query database.