cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Bug in ORM 'one-to-one' relation: maximum recursion depth exceeded

Open Vastly opened this issue 12 years ago • 3 comments

Ubuntu 12.04 TLS, php 5.4.27, Phalcon 1.2.6

i have created a 'one-to-one' relation between Cat and Tail models which leads to Fatal error: Phalcon\Events\Manager::fireQueue(): Maximum recursion depth exceeded and i suppose it is a bug. Cat model http://pastebin.com/fxqrRKbA Tail model http://pastebin.com/xdHiVnK6

those two Models code was automatically generated by phalcon dev tools from the following simple schema (except 'initialize' method):

create table tail (

id int not null auto_increment primary key,
color varchar(200) not null,
cat_id int not null,
constraint fk_tail_cat foreign key tail(cat_id) references cat(id)
);

create table cat (
id int not null AUTO_INCREMENT primary key,
catname varchar(100) not null
);

insert into cat(catname) values('fluffy');
insert into tail(color, cat_id) values('blue', 1);

the code is as simple as

 /** @var Cat $cat */
        $cat = Cat::findFirst();

        /** @var Tail $tail */
        $tail = Tail::findFirst();

        // make sure that relationships work as expected
        var_dump(get_class($cat->Tail)); // outputs 'Tail'
        var_dump(get_class($tail->Cat)); // outputs 'Cat'


        // any data is not even changed here
        $catsTail = $cat->Tail;
        $catsTail->Cat = $cat;
        $catsTail->save();
        exit;

the last line produces fatal error: Fatal error: Phalcon\Events\Manager::fireQueue(): Maximum recursion depth exceeded

Note: after changing the type of the relation to has-many (instead of has-one) everything works like a charm, so it looks like a bug in 'has-one' side of things.

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

Vastly avatar Apr 24 '14 18:04 Vastly

Try changing the code to:

$catsTail = $cat->Tail;
$catsTail->cat_id = $cat->id;
$catsTail->save();

ghost avatar Apr 24 '14 18:04 ghost

It works, thank you! But it looks more like a crutch/work-around than a best practice solution. What do you think?

Vastly avatar Apr 24 '14 19:04 Vastly

I have similar issue. In above scenario my ->save() method would return false, but getMessages would return null. Object does not get saved. Workaround suggested by @phalcon works. I suggest this is addressed as it seems to be a bug.

Thanks for all the hard work.

MatejBalantic avatar Jul 22 '14 18:07 MatejBalantic