2

I have watched the movies and read the documents but I can't understand the usage of hasone and hasmany in agile toolkit! can anybody give me some simple examples for these?

Thanks.

webelizer
  • 418
  • 2
  • 11

2 Answers2

3

Two models with hasOne relation:

class Model_User extends Model_Table {
    public $table = 'user';
    function init() {
        parent::init();
        $this->addField('name'); 
        $this->addField('email');
        $this->hasOne('role');    // field role_id in database
    }
}    
class Model_Role extends Model_Table {
    public $table = 'role';
    function init() {
        parent::init();
        $this->addField('name'); 
    }
}

Usage of these two models:

$cr = $this->add('CRUD');
$cr->setModel('Model_User',
    array('name','email','role_id'),
    array('name','email','role')
);

In Grid you will see field name from role table.
In Form you will see dropdown with all roles in it. You can select one role per user.

Vadym
  • 749
  • 3
  • 15
  • Thank u very much. @vadym could you please give an example for hasmany too? – webelizer Sep 19 '13 at 11:03
  • 1
    actually you just need to add $this->hasMany('User'); in Model_Role. Because every role can be assigned to many users. – Vadym Sep 20 '13 at 16:37
1

Read Agile Toolkit low-level documentation here:

http://agiletoolkit.org/book.pdf

should help you understand models fully.

romaninsh
  • 10,606
  • 4
  • 50
  • 70