Saving a hasMany relationship, after the Model is put in a session

$activity = new Activity();

$activity->location_id  = $location->id;
$activity->booked_start = $dateFrom->format(DbConfig::SQL_DATETIME_FORMAT);
$activity->booked_end   = $dateTo->format(DbConfig::SQL_DATETIME_FORMAT);
$activity->name         = $name;
$activity->description  = $description;

$activityResources = [];

foreach ($resources as $resourceId => $amount) {
    $activityResource = new ActivityResource();

    $activityResource->resource_id = $resourceId;
    $activityResource->amount      = $amount;

    $activityResources[] = $activityResource;
}

$activity->activityResources = $activityResources;

$this->session->activity = $activity;

And now at another pageload in some controller:

$this->session->activity->save();

Only the activity itself is saved in the DB. The ActivityResources are not. If I save right away, they do get saved. Is this intended behaviour or is this a bug? Could there be a workaround?

$activity = new Activity();

$activity->location_id  = $location->id;
$activity->booked_start = $dateFrom->format(DbConfig::SQL_DATETIME_FORMAT);
$activity->booked_end   = $dateTo->format(DbConfig::SQL_DATETIME_FORMAT);
$activity->name         = $name;
$activity->description  = $description;

echo serialize($activity), PHP_EOL;

$activityResources = [];

foreach ($resources as $resourceId => $amount) {
    $activityResource = new ActivityResource();

    $activityResource->resource_id = $resourceId;
    $activityResource->amount      = $amount;

    $activityResources[] = $activityResource;
}

$activity->activityResources = $activityResources;

echo serialize($activity), PHP_EOL;

The result will be the same for both serializations, which means the related models does not get stored in the session. Storing models in session is not a good ide anyway, since it's a complex class and would hog memory.

You should probably save the data as arrays in session, and only instantiate the model when you actually save it



294
edited Jan '18

It's an extremely bad idea to store a Model instance in session, since the object itself has a lot of references to the DI and whatnot. My guess is that's the problem with the related items.

References are never stored in a session, only the object's contents. Which made me wonder, what would happen if i'd serialize the object. I found out that that's where the problem is. The activityResources are not found in the serialized object, so they are probably stored somewhere outside of the object.

You should only save the ID of the record(s), and then fetch it from db on demand.

There are no ID's, because they haven't been stored yet. The objects are stored in session, because they are in a basket which can be ordered at a later point. Once the order is accepted, the basket's contents are stored in the DB.



294

Storing models in session is not a good ide anyway, since it's a complex class and would hog memory.

It's not so bad:

C:23:"Website\Models\Activity":193:{a:7:{s:2:"id";N;s:11:"location_id";s:1:"1";s:8:"order_id";N;s:4:"name";s:8:"test";s:12:"booked_start";s:19:"2018-01-31 16:00:00";s:10:"booked_end";s:19:"2018-01-31 17:00:00";s:5:"notes";N;}}