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

How to print Phalcon\Forms\Form elements in volt in a one-to-many relationship

I have a one-to-many relationship that looks like:

<?php
    use Phalcon\Mvc\Model;

    class Robots extends Model
    {
        public $id;
        public $name;
        public function initialize()
        {
            $this->hasMany('id', 'Parts', 'part_id'); 
        }
    }

    class Parts extends Model
    {
        public $id;
        public $part_id;
        public $part_name;
        public function initialize()
        {
            $this->belongsTo('part_id', 'Robots', 'id');
        }
    }

and the form looks like:

    class RobotsForm extends Form
    {
        public function initialize($entity = null, $options = array())
        {
            $name = new Text("name")
            $name->setLabel("Robot Name");
            $this->add($name);

            foreach ($entity->parts as $part) {
                // how to add the part name?
                // $this->add(new Text("part_name[]"));
            }  
        } 
    }

Using volt for rendering, how can I iterate over all the robot parts and add a text field for each part name? e.g.,

    {% for element in form.get('parts') %}
        {{ element.render("part_name") }}
    {% endfor %}

Hi,

I'm new with Phalcon and I have the same problem. I know this is an old post, but can anyone explained how to display the data from a one-to-many- relationship?