Cphalcon: [BUG] Models: setter did not accept array

Created on 13 Dec 2018  路  7Comments  路  Source: phalcon/cphalcon

Hello :)
Consider this (if we follow phalcon setter docs):

# MODEL
namespace Models;

class Lessons extends \Phalcon\Mvc\Model
{
    public $id;
    protected $title;

    public function setBobi($test)
    {
        echo $test;
        exit;

        $this->title = $test;
    }
# Somewhere
$model = new MyModel();

$model->bobi = 'soheil'; // Will print 'soheil' (see model exit)

So this will exit on 'soheil'. But if we make setter that mus accept array- it is not possible (without logick). Example:

# MODEL
namespace Models;
class Lessons extends \Phalcon\Mvc\Model
{
    public $id;
    protected $title;

    public function setBobi(array $test)
    {
        var_dump($test);
        exit;

       $this->title = implode(',', $test);
    }

end execute:

# Somewhere
$model = new MyModel();

$model->bobi = ['some', 'other']; // Will do nothing

...this will not var_dump. Why?

Will work if i use setter like function $model->setBobi(['some']); but why? If i make $model->save($post) and in post i have multiple values on some property will not work with setter.

Expected behavior

# MODEL
namespace Models;
class Lessons extends \Phalcon\Mvc\Model
{
    public $id;
    protected $title;

    public function setTitle(array $test)
    {
       $this->title = implode(',', $test);
    }
$model = new MyModel();
$model->title = ['some', 'other'];
$model->save(); // Save `title` in database == 'some,other'

Details

  • Phalcon version: all
  • PHP Version: 7.2

Thanks. I hope u understand me and sorry for my English skills.

bug medium

All 7 comments

I find the issue in Model (Zephir code)- if we see code from https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model.zep and go to the line 4506 - this is the solution, but it is placed in wrong place and never work, must be before line 4475.

So, can someone fix that? Thank u!!!!

Or other solution is to move content from line 4503 to line after 4500. Is it better?

@borisdelev makes sense. Would line 4454 make more sense though before the object handling?

@niden is there any specific reason for the order of operations? Handle object, array then check if it has a setter?

I think- i can do it, but confirm which way is better? (and which branch- 4?)

That'd be awesome! Yes, v4 branch please :)

Well... i am not sure if i change the order is good idea, so i think this is better way: https://github.com/borisdelev/cphalcon/commit/d8540a4d328a31f1e8afaa2b98eb648035f1c6a4#diff-95da5fc282ff73cf517e37ee54f1613b - what uve thinking?

Honestly I always hated the magic methods. As we see here since we have them, we have to try and find whether this is a relationship/model or a property or a setter.... Big mess.

At some point in the future this will need to be cleaned up (the whole ORM).

Was this page helpful?
0 / 5 - 0 ratings