2014-01-08 50 views
0

即时begginer和搜索文件,但无法找到这个如何做到这一点:ATK CRUD与一个:很多的关系

我有两个表,管理员和应用程序。管理员可以有很多应用程序。

ADMIN:

class Model_Admin extends Model_Table { 
    public $entity_code='admin'; 
    function init(){ 
     parent::init(); 

     $this->addField('name'); 
     $this->addField('email'); 
     $this->addField('password')->type('password'); 
     $this->addField('active')->type('boolean')->system(true); 
     $this->addField('super')->type('boolean')->system(true); 
     $this->addField('created')->type('timestamp')->defaultValue($this->dsql()->expr('now()'))->system(true); 
     $this->addField('updated')->type('timestamp')->system(true); 

     $this->hasMany('Application','admin_id'); 
     //$this->hasOne('Application'); 

     $this->addHook('beforeSave',function($m){ 
       $m['updated']=$m->dsql()->expr('now()'); 
       }); 
    } 
} 

应用:

class Model_Application extends Model_Table { 
    public $entity_code='application'; 
    function init(){ 
     parent::init(); 

     $this->addField('name'); 
     $this->addField('fbid'); 
     $this->addField('fbsecret'); 
     $this->addField('active')->type('boolean')->system(true); 
     $this->addField('created')->type('timestamp')->system(true); 
     $this->addField('updated')->type('timestamp')->system(true); 
    } 
} 

第一个问题,当我生成SQL代码(/generate.html)它不会产生一个什么一对多的关系。 其次,在页面上我添加CRUD:

$this->add('CRUD')->setModel('Admin'); 

但没有暗示任何一对多。我希望它在添加按钮的形式,但也没有什么?

我想要的是,我可以添加管理员,并选择最重要的应用程序属于他?

谢谢你的帮忙!

回答

2
在Model_Application

$this->hasOne('Admin'); 

上页

$this->add('CRUD')->setModel('Application'); 

在CRUD的编辑形式,你会看到下拉列表的所有管理员,你就可以管理设置为每个应用程序

+0

喜,谢谢你的回答,但这只适用于1:1解决方案。我在这里创建了M:M的新问题:http://stackoverflow.com/questions/21045491/how-to-implement-crud-with-manymany-relationship – Peter

+0

不,这是一对多的连接,因为一个管理员可以拥有多种应用。对于多对多连接,您需要使用proxi表格 – Vadym