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

Populate object with nested objects in a form

Hello.

This is my first post in this great forum.

I have two classes (I don't use ORM)

    class parent {

        public $id;

        public $name;

        public $ch;  <--- Child object

    }

    class child {

        public $id;

        public $name;

    }

Var_dump of parent object.

    object(parent)#95 (3) { 

        ["Id"]=> int(1) 

        ["name"]=> string(9) "FOO"  

        ["ch"]=> object(child)#132 (2)

        { 

            ["Id"]=> int(1) 

            ["name"]=> string(8) "BAR"

        }
    }

I have a Form with two text elements, but I cant't get the child name value inside this.

I want to send this object to edit the names in a volt view with and parent name appears ok , but I can't get child name (I have trycalling "ch.name" to the Text Element for example, but it does not works.

Thank you in advance.

Regards

Ángel



539
Accepted
answer
edited Sep '15

I have a possible solution using value option in my text element:

$chname="";
if($entity!=null){
        $chname=$entity->ch->name;
   }
$txtChildName = new Text("childName", array(
        "value" => $chname
    ));

Thank you.