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

Form binding subobjects

Hi
suppose i have a form that has an entity, User, bound to it. This user can have many Phones. I want to be able to data bind this user to a form in such a way, that when edit action is called on the user, the user's phones are also seen. With simple models its as easy as

<?php

public function editAction($id)   
   {   
        $user = \User::findFirstById($id);    
        if (!$user) {     
       $this->flash->error('Cant find user');    
        return $this->forward('user/index');    
        }    
        $this->view->form = new CreateUserForm($user, array('edit' => true));    
   }   

And it binds the user details nicely to the form. But i want to be able to add related entities and edit them in the same form. What is the best approach? Thanks in advance



33.8k

Inside CreateUserForm's constructor:

// If a related entity exists. '$params' is optional.
if ($user->getRelatedEntity($params)) {
    // Add stuff for related entity...
}
// or...
if ($user->relatedEntity) {
    // Add stuff for related entity...
}