Skip to content

Phalcon\Mvc\Model very strange setter behaviour if provided multidimensional array #945

Closed
@cottton

Description

@cottton

Questions should go to https://forum.phalconphp.com
Documentation issues should go to https://github.com/phalcon/docs/issues

Expected and Actual Behavior

Describe what you are trying to achieve and what goes wrong.
Setting up a model with a simple foreach ... $this->{$key} = $value; overwrites already set fields.
Totally messes up recursively.

Provide output if related

// paste output here
// expected:
    array (
        'id' => NULL,
        'name' => 'cottton',
    )
    array (
        'id' => NULL,
        'name' => 'Foo',
    )
// actual
    array (
        'id' => NULL,
        'name' => 'cottton',
    )
    array (
        'id' => NULL,
        'name' => 'shit!',
    )

Provide minimal script to reproduce the issue

CREATE TABLE `robots` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(70) NOT NULL,
    PRIMARY KEY (`id`)
);
class Robot extends Phalcon\Mvc\Model
{
    public function getSource()
    {
        return 'robots';
    }

    public function setData($data)
    {
        foreach ($data as $key => $value) {
            $this->{$key} = $value;
        }
        return $this;
    }
}

$robot = new \Robot();

$robot->name = 'cottton';
echo var_export($robot->toArray(), true) . PHP_EOL;  // name === cottton
 
$robot->setData(
    [
        'name'       => 'Foo', // actual name expected to be set.

        //problems:
        // if providing (on purpose or not) an array ...

        // with any offset (numeric, string, w/e)
        'any_offset' => [

            // and in here is the same offset as on parent (i.e. "name")
            // then robot will be named:
            'name'                        => 'FU',

            // except another array is given (on purpose or not)
            // with any offset EXCEPT the offset before (i.e. "any_offset")
            'any_offset_but_"any_offset"' => [
                'name' => 'FU2',
            ],

            // ... recursively
            'and_so_on ...'               => [
                'well ...' => [
                    'name' => 'this is fucked up',
                ],
            ],

            'and_so_on ... ...' => [
                'well 1' => [
                    'well 2' => [
                        'well 3' => [
                            'well 4' => [
                                'well 5' => [
                                    'well 6' => [
                                        'name' => 'shit!',
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ]
);
echo var_export($robot->toArray(), true) . PHP_EOL;// name === shit!

Details

  • System info and versions (if possible): (phalcon info)
Phalcon DevTools (3.0.3)

Environment::
  OS: Linux medev-accounting-app-1 4.4.0-53-generic #74~14.04.1-Ubuntu SMP Fri Dec 2 03:43:31 UTC 2016 x86_64
  PHP Version: 7.0.11
  PHP SAPI: cli
  PHP Bin: /usr/local/bin/php
  PHP Extension Dir: /usr/local/lib/php/extensions/no-debug-non-zts-20151012
  PHP Bin Dir: /usr/local/bin
  Loaded PHP config: /usr/local/etc/php/php.ini
Versions::
  Phalcon DevTools Version: 3.0.3
  Phalcon Version: 3.0.1
  AdminLTE Version: 2.3.6

  • PHP Version: (php -v)
    PHP 7.0.11

  • Operating System:
    Ubuntu 14.04

  • Server: Nginx | Apache | Other
    Nginx

  • Other related info (Database, table schema):
    NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions