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.

2 Answers 2

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.

2
  • Thank u very much. @vadym could you please give an example for hasmany too?
    – webelizer
    Sep 19, 2013 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, 2013 at 16:37
1

Read Agile Toolkit low-level documentation here:

http://agiletoolkit.org/book.pdf

should help you understand models fully.

1
  • roman is this the last version? I have read this book about three or four monthes ago.
    – webelizer
    Sep 19, 2013 at 11:09

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