cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG] Saving related records - "Fatal error: Maximum recursion depth exceeded"

Open integry opened this issue 12 years ago • 4 comments

If I assign a parent record (has many) instance to child (belongs to) and then attempt to save the child record it fails with Fatal error: Maximum recursion depth exceeded.

I'm fully aware that it's not how it's intended to be used, but the same issue also comes up in more complex scenarios where setting the related instance prior to saving cannot be avoided, because it has to do some calculations inside model and access the related record instances to achieve that.

I'm using 1.3.1. Here's the complete code to reproduce the problem.

<?php

$di = new Phalcon\DI\FactoryDefault();
$di->set('db', function() {
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => 'localhost',
        "username" => 'root',
        "password" => '',
        "dbname" => 'testing'
    ));
    });

class TheParent extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->hasMany('ID', 'Child', 'parentID', array('alias' => 'Children'));
    }
}

class Child extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->belongsTo('parentID', 'TheParent', 'ID', array('alias' => 'Parent'));
    }
}

Phalcon\Mvc\Model::setup(['exceptionOnFailedSave' => true]);

$parent = new TheParent();
$parent->name = 'The parent';

$child = new Child();
$child->name = 'test';

// it works if the following line is removed and $parent->save() is called instead
$child->parent = $parent;

$parent->children = [$child];

$child->save();

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

integry avatar Apr 09 '14 23:04 integry

Another test case that produces the same fatal error, and this time I'm not even assigning the parent directly.

$parent = new TheParent();
$parent->name = 'The parent';
$parent->save();

$other = new TheParent();
$other->name = 'Other parent';
$other->save();

$child = new Child();
$child->name = 'test';
$parent->children = [$child];

$parent->save();

$other->children = [$child];
$other->save();

How am I supposed to change the parent? Attempt to assign it directly to child fails with the same error.

$child->parent = $other;
$child->save();

integry avatar Apr 10 '14 01:04 integry

This way:

$child->parentID = $other->ID;
$child->save();

ghost avatar Apr 10 '14 01:04 ghost

Thank you for the suggestion, it indeed works in the test case. However for some strange reason it does not work in the production code. After setting the new ID, for some reason it is back to the old ID in the beforeSave() method. This is how I solve it...

public function beforeSave()
{
    if (isset($this->changedChoice))
    {
        $this->choiceID = $this->changedChoice;
    }
}

public function loadData($data)
{
    $id = is_array($data) ? $data['choiceID'] : $data;
    $this->choice = ProductOptionChoice::getInstanceByID($id);
    $this->choiceID = $id;
    $this->changedChoice = $id;
    $this->save();
}

integry avatar Apr 10 '14 02:04 integry

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

stale[bot] avatar Apr 17 '18 07:04 stale[bot]