atk4agiletoolkit

need simple examples for hasone and hasmany usage


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.


Solution

  • 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.