j4mie / paris

A lightweight Active Record implementation for PHP5, built on top of Idiorm.
http://j4mie.github.com/idiormandparis/
996 stars 131 forks source link

Infinite recursion when using Twig accessors on fields that are NULL #78

Closed amerker closed 10 years ago

amerker commented 10 years ago

In Paris 1.4.1 and Idiorm 1.4.0, when trying to access DB fields with NULL as value from Twig (via {{ object.field }}), I get infinite recursions.

This is when building the object using find_one(), or find_many() with a Twig loop, while find_array() works as expected.

Here's a barebones system that should demonstrate the problem:

DB (MySQL):

CREATE TABLE IF NOT EXISTS `twig_test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `requiredField` varchar(128) NOT NULL,
  `nullableField` varchar(128),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
INSERT INTO `twig_test` (`requiredField`, `nullableField`) VALUES ('first set', 'optional first data');
INSERT INTO `twig_test` (`requiredField`) VALUES ('second set');
INSERT INTO `twig_test` (`requiredField`, `nullableField`) VALUES ('third set', 'optional third data');

Model and Slim Route:

class TwigTest extends Model {}
$app->get(
    '/twigtest',
    function() use ($app) {
        $testdata = Model::factory('TwigTest')->find_many();
        $app->render('twigtest.html.twig', ['testdata' => $testdata]);
    }
);

Twig:

{% for row in testdata %}
    {{ row.requiredField }} // {{ row.nullableField }}<br/>
{% endfor %}

This always throws an infinite recursion to which Xdebug predictably reacts with:

Fatal error:  Maximum function nesting level of '100' reached, aborting! in /[...]/paris.php on line 522

Twig does not render the third output line (corresponding to the third db row) after that. As soon as I replace find_many() with find_array() in the Slim Route, it all works.

Also, the infinite recursion occurs when using Twig's {% if row.nullableField %}, but not when using {% if row.nullableField is defined %}. The latter always returns true - interestingly enough on ANY field name, even nonexistent ones, which is something I would maybe regard as a bug as well.

Using j4mie/paris 1.4.1, j4mie/idiorm 1.4.0, slim/slim 2.3.5, slim/views 0.1.0, twig/twig 1.14.2.

gwn commented 10 years ago

I believe that this problem is addressed in issue #74 and related pull request #75.

gwn commented 10 years ago

Hey i just saw that you had already posted a comment to #75. Sorry

DavidePastore commented 10 years ago

I think that this is associated to https://github.com/j4mie/idiorm/issues/181 .

petersmithca commented 10 years ago

Hi there,

i've had this infinite recursion issue as well.

This says its in the development branch, and there is text for a release 1.5, but I don't see it as actually being released yet. Wondering when it will be? Using composer to pull and not sure how to get a non release branch (am new to a lot of this, sorry)

treffynnon commented 10 years ago

@petersmithca you can change your composer requirements to point to dev-develop so the line would look like:

"j4mie/idiorm": "dev-develop"

I am working to get the 1.5.0 release out soon, but I am very short on time with paid work taking priority at the moment. It would be great if you could test your issue against the develop branch mentioned above for me though.

treffynnon commented 10 years ago

This issue has been fixed in Idiorm.