Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

HasMany doesn't work correctly - "Maximum function nesting level" error

Can't figure out why this simple code doesn't work:

$pack = new Pack();
$pack->name = 'A pack';

$packItems = array();

$packItem = new PackItem();
$packItem->pack = $pack;
$packItem->name = 'First item in the pack';
$packItems[] = $packItem;

$packItem = new PackItem();
$packItem->pack = $pack;
$packItem->name = 'Second item in the pack';
$packItems[] = $packItem;

$pack->packItems = $packItems;

The Pack model:

$this->hasMany('id', 'PackItem', 'packItemId', array(
    'alias' => 'packItems'

And the PackItem model:

$this->belongsTo('packId', 'Pack', 'id', array(
    'alias' => 'pack'

After I try to run this code it ends on error:

PHP Fatal error: Maximum function nesting level of '256' reached, aborting!

The log file is full of code like this:

... PHP 39. Phalcon\Mvc\Model->save() PHP 40. Phalcon\Mvc\Model->_postSaveRelatedRecords() PHP 41. Phalcon\Mvc\Model->save() PHP 42. Phalcon\Mvc\Model->_preSaveRelatedRecords() ...

I don't have more ideas how it doesn't work. I use Phalcon 3.0 and It seems to me it is a bug. What do you think?



there's a recursion in your models data:

A pack object has many packItems, but each packItem has a reference to the pack object itself. Maybe this is the source of your hassle.

See what happens if you comment the line

$packItem->pack = $pack;

You're right! I removed this line and everything works fine now! You've saved lot of my time, thank you :)