3

When writing a view which either edits creates a record, the model extends admin model which requires getTable() and getForm().

Now the confusion is how the loadForm() function knows the number of fields in the table associated with the model and what the role of the bind() function is.

It's not clear to me how the table class and form talk to each other and where

2 Answers 2

8

Your form is defined by your XML form (typically located in models/forms). You choose to have the same names as those in your database as those field names but actually it isn't compulsory.

You can think of the table as being like a wrapper around a row to insert. You go through a 3 stage process.

bind takes the array of data that comes from your form and then stores any data from it into the table class.

check then runs sanity checks on the data that you have binded into the table class

store then stores the binded data into the database table.

However what's important is the data that is stored in the class. This means that the names of the fields in your form are to an extent irrelevent. For example. You could have a field in your form called foobar and then in your bind method do:

public function bind($src, $ignore = array())
{
    $this->tableVar = $src['foobar'];
    parent::bind($src, $ignore);
}

And this will try and store the foobar variable from your form as a column called tableVar in your database

EDIT:

This is because the parent bind method will bind data to the class as a class var

public function bind($src, $ignore = array())
{
    foreach ($src as $field => $value)
    {
        $this->$field = $value;
    }
}

and then will attempt to match $field to a database column storing the value if it exists. So the importance of setting $this->tablevar above is that is the column in your database was tablevar and you didn't have a column called foobar then now it gets stored into the database.

2
  • how this ($this->tableVar = $src['foobar'];) will affect the bind function as we are passing $src without changing the name of 'foobar' instead we are passing foobar value to tableVar.? and yes thanks also Commented Feb 23, 2015 at 7:01
  • added an edit to try and help you understand :) Commented Feb 25, 2015 at 22:06
3

The Form does not know anything about the fields in your database. It's getting its fields by your XML definition. The bind() function just adds the data to the object, while store() will post the binded data to your database.

In other words: You are responsible for correlating fields/properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.