Form entity for multiple models

Hi there,

I have a question concerning the entities of the phalcon-forms. I have a form-model, which is used to manipulate data of two models simultaneously. One user-model with basic user-information and one corresponding profile-model (profile belongs to user) . Based on the user-type, there are different profile models, which is why I have to split both models up.

My question is how to bind the entity-data of both models to my single-form?

$user = User::findFirst($id);
$profile = $user->getProfile();

/** With this, only fields corresponding to the user-model are filled out in the form*/
 $this->view->form = new UserForm($user);

/** With this, only fields corresponding to the profile-model are filled out in the form*/
 $this->view->form = new UserForm($profile);

Is there a way to combine both models in one entitity or something?

edited Sep '16

Hey, I'm using QueryBuilder with Joins for this when needed. It's just important that the "Entity" object has the fields in the form.

Here is an example of old project of mine:

// Controller
$user = $obj->getEditUserProfile($this->session->user->id);   
$form = new UsersEditForm($user); 

// Model
public function getEditUserProfile($id)
{
    return $this->modelsManager->createBuilder() 
        ->columns(array(
            'Users.id AS user_id', 
            'name', 
            'email',
            'about', 
            'location'
        ))
        ->from(array('Users' => 'Modules\Frontend\Models\Users'))
        ->leftJoin('Modules\Frontend\Models\UserDetails', 'UserDetails.user_id = Users.id', 'UserDetails')
        ->where('Users.id = :id:', ['id' => $id])
        ->getQuery()
        ->getSingleResult();
}

Please note it's old code, but you get the idea ;)

Thanks Nikolay, but I think I don't understand your example. Are you making a new Database-Query with the query-builder? Isn't it possible to build an entity object based on the existing data of my models? Because I already have all the data stored in $user and $profile ...

edited Sep '16

I'm not using model mehods at all, but as far as i know entity must be one object that has the properties/inputs of the form class.

You can try to "merge" the two objects from models::findFirst, something hacky like this: http://stackoverflow.com/questions/455700/what-is-the-best-method-to-merge-two-php-objects

$merged = (object) array_merge(
    $obj1->toArray(), 
    $obj2->toArray()
);

That's my best guess at the moment, if I get some better idea after some sleep I'll let you know! Also let's hope someone else has better idea and faced the same issue.

edited Sep '16

I already tried that, but I guess the data-fields must be stored as class properties in the entity, because the form-model can't process array-data. Anyway, your post helped my find a solution by simply adding the profile-properties dynamically to the user-model before sending it to the edit-form:

$user = User::findFirst($id);
if($user) {
  $profile = $user->getProfile();
  $user->vorname = $profile->vorname;
  $user->nachname = $profile->nachname;
  $this->view->form = new UserForm($user, ["edit" => true]);
}

This is not nice, but it works.

Since the profile has a relation to the user model, it should be possible to define their relation in the form-model already, so the form automatically searches for the profile-fields in the related profile-model. I don't know if thats possible though but it would be nice.