Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
to_array() shows Warning on belongs_to with null value
  • FuelPHP 1.8.1

    Models:

    class Model_Employee extends Orm\Model {
        protected static $_belongs_to = array('position');
    }

    class Model_Position extends Orm\Model {}

    Controller:
    class Controller_Employee extends Controller_Rest {
        public function get_index()
        {
            $employees = Model_Employee::find('all', array('related' => array('position')));
            $result = array_map(function ($row) {
                $row = $row->to_array();

                $row['fixed_earnings'] = array_values($row['employeefixedearnings']);
                //...

                return $row;
            });

            // code to return the result to the client
        }
    }

    With the above code, the to_array() fails if employees->position_id is null.

    The warning message:

    Fuel\Core\PhpErrorException [ Warning ]:

    get_class() expects parameter 1 to be object, null given

    c:/test/fuel/packages/orm/classes/model.php @ line 2195

    line 2195:

    if ( ! in_array(get_class($rel), static::$to_array_references))
    The fix I made:

    if (is_null($rel))
    {
    $array[$name] = null;
    }
    else
    {
    if ( ! in_array(get_class($rel), static::$to_array_references))
    static::$to_array_references[] = get_class($rel);
    $array[$name] = $rel->to_array($custom, true, $eav);
    array_pop(static::$to_array_references);
    }
    }
  • Harro VertonHarro Verton
    Accepted Answer
    Thanks for reporting it.

    Has been fixed, you can use "composer update" to install the fix.
  • Thanks!
  • Now it fails on has_many with empty array value:

    Warning!



    Fuel\Core\PhpErrorException [ Warning ]:
    get_class() expects parameter 1 to be object, boolean given



    C:/test/fuel/packages/orm/classes/model.php @ line 2184



    2179                $array[$name] = null;
    2180            }
    2181            elseif (is_array($rel))
    2182            {
    2183                $array[$name] = array();
    2184                if ( ! in_array(get_class(reset($rel)), static::$to_array_references))
    2185                {
    2186                    static::$to_array_references[] = get_class(reset($rel));
    2187                    foreach ($rel as $id => $r)
    2188                    {
    2189                        $array[$name][$id] = $r->to_array($customtrue$eav);

    Backtrace



    1. C:/test/fuel/core/bootstrap.php @ line 103


    2. C:/test/fuel/packages/orm/classes/model.php @ line 2184


    3. C:/test/fuel/app/classes/controller/employees.php @ line 76


    4. C:/test/fuel/core/base56.php @ line 37


    5. C:/test/fuel/core/classes/controller/rest.php @ line 153


    6. C:/test/fuel/core/classes/request.php @ line 454


    7. C:/test/public/index.php @ line 71


    8. C:/test/public/index.php @ line 92

    I think line 2184 should be:

    if ( ! empty($rel) && ! in_array(get_class(reset($rel)), static::$to_array_references))
  • Sjees, does nobody test anymore? This code has been in 1.9/dev for months! :-(

    It's probably better to change the null test to

    if (empty($rel))
    {
        $array[$name] = null;
    }

    which would capture both.
  • Harro VertonHarro Verton
    Accepted Answer
    Hotfix 1.8.1.2 for Orm pushed.
  • Should it be that the value assigned to $array[$name] is array() for has_many?

    the find() for belongs_to returns null if no relation has been found.
    find() for has many returns array() if no relation has been found.
  • Harro VertonHarro Verton
    Accepted Answer
    I don't think that matters, but you might want to test that, the Orm isn't my strongest point.
  • ok then i just need to test my code for null has_many relationship.

    Thanks.
  • Harro VertonHarro Verton
    Accepted Answer
    I've looked around in the code, everything seem to use empty() to test for the presence of a relation, so that should be ok.

    Anyway, I've made a further change to return null on singular relations, and array() on plural ones. but I haven't tagged it with a new hotfix version, let's see if that is needed.
  • Thanks.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion