cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Models setter is executed when view object

Open borisdelev opened this issue 6 years ago • 1 comments

Hi there,

Today ive upgrade my Phalcon from 4.0.6 to 4.1.0. And a error appear with setters in my models. Here example:

<?php

namespace Models;

class Testimonials extends \Phalcon\Mvc\Model
{
    public $id;
    public $position;
    public $name, $subtitle, $content;
    private $categories;

   // Somewhere in my Testimonials Model

    public function setCategories(array $list = []): Testimonials
    {
       
        return $this;
    }

Now somewhere in my views...

{% for item in testimonials %}
 {# ... #}
{% endfor %}

And here is go the new error:

Fatal error: Uncaught TypeError: Argument 1 passed to Models\Testimonials::setCategories() must be of the type array, string given in \common\models\Testimonials.php on line 23

I did not update/save/set that model/object to enter in that setter. Try that in the controller- same error:

        $this->view->testimonials = \Models\Testimonials::find([
            'order' => 'position ASC'
        ]);
        
        foreach($this->view->testimonials as $t) {
            // ERROR
        }

So... when enter in foreach error is popup. Maybe for some reason setter is executed, but why? I did not set or update nothing here.

Hope you fix that :/ Thanks!

May the force be with you!

My php version is 7.3 64x.

borisdelev avatar Nov 02 '20 08:11 borisdelev

Well... the issue was private $counters; in Model. Was private variable and works in previous version, but not in this one. Must be protected : protected $counters;

borisdelev avatar Nov 02 '20 13:11 borisdelev