We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon 2.0 - "Trying to get property of non-object" with findFirst

Hi all,

I have a problem with a simple thing and I don't understand why I have this error :

Trying to get property of non-object

My Menus Model

class Menus extends \Phalcon\Mvc\Model
{    
    public $id;

    public $parentId;        

    public $route; 

    public $createdAt;

    public $updatedAt;

    public function getSource()
    {
        return 'menus';
    }    

    public function beforeValidationOnCreate()
    {        
        $this->createdAt = $this->updatedAt = date('Y-m-d H:i:s');
    }

    public function beforeValidationOnUpdate()
    {
        $this->updatedAt = date('Y-m-d H:i:s');
    }         
}

And a simple private function in my controller to re-order my menu after deleting one.

private function reOrder($menuId)
    {  
        $menu = Menus::findFirst($menuId); 
        $position = 1;

        foreach (Menus::find(array( 'parentId = :parentId:', 'bind' => array( 'parentId' => $menu->parentId), 'order' => 'position ASC')) as $m)
        {
            $nm = Menus::findFirst($m->id);
            $nm->position = $position;
            $nm->update();
            $position++;
        }       
    }

I do not understand, why I have this error with when I use : $menu->parentId (off course is not null)

Thx.

Your modelclass has no attribute "position" and did you checked if Menu::findFirst($menuId) returns something?



7.1k

I miss "position" when I post this example :) but is present in my model.

I check many times findFirst output and return this :

Model\Menus Object
(
    [id] => 5
    [parentId] => 2
    [route] => -1
    [active] => 1
    [position] => 3
    [createdAt] => 2015-04-27 20:34:23
    [updatedAt] => 2015-04-27 20:34:23
    [_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object
        (
            [_services:protected] => Array
                (
                ......