Skip to content

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

@amerker

Description

@amerker

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions