【问题标题】:ATK CRUD with one:many relationshipATK CRUD 与 one:many 关系
【发布时间】:2014-01-26 22:30:03
【问题描述】:

我是初学者并搜索过文档,但找不到如何执行此操作:

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

管理员:

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');

但是没有任何一对多的提示。我希望它在添加按钮表单上,但也没有什么?

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

【问题讨论】:

    标签: atk4


    【解决方案1】:

    在模型_应用程序中

      $this->hasOne('Admin');
    

    在页面上

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

    在 CRUD 的编辑表单中,您将看到所有管理员的下拉菜单,并且您可以为每个应用程序设置管理员

    【讨论】:

    • 您好,感谢您的回答,但这仅适用于 1:1 解决方案。我在这里创建了关于 M:M 的新问题:stackoverflow.com/questions/21045491/…
    • 不,这是一对多的连接,因为一个管理员可以拥有多个应用程序。对于多对多连接,您需要使用代理表
    猜你喜欢
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多