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\Mvc\Model assign vs Phalcon\Forms\Form bind

It seems that both methods are used to do the same thing, binding the submitted post's data to the entity. Am I missing something?

Phalcon\Mvc\Model assign() Assigns values to a model from an array

Phalcon\Forms\Form bind() Binds data to the entity

$myEntity->assign($_POST);

$form->bind($_POST, $myEntity);

When should one be chosen over the other? Thanks for your help.



26.3k
edited Oct '14

both methods are used to do the same thing

It is true, but only when your definition of model is equal to the definition of entity. The problem is that they are not the same.

You may create a class, let's call it AwesomeMovieDomain and this class will not be a model. You will use it as an entity. You can bind an entity of AwesomeMovieDomain class to the form. AwesomeMovieDomain can have protected property $_starring and it will be an array where each element will be an entity of a model Actor.

This is a problem that I am now struggling on too. Some keywords that are connected to this topic and that I am trying to get familiar with are: DDD - domain driven development, entities, models, domains, services, application architecture, design patterns etc. AFAIK models in Phalcon are following a design pattern called "Active Record".