2

如何在 atk4 中实现多表更新/删除?

4

2 回答 2

1

You can use functions beforeInsert, afterInsert, beforeDelete, afterDelete and beforeUpdate, afterUpdate to carry out additional processing in the database. For example using a unzipped install of ATK 4.1.3 in your webroot creates a folder called agiletoolkit which is referred to below as ATKHOME.

Create a simple table TASKTYPE in mysql with three fields (id, tasktype_desc and budget_code) and another table TASKTYPE_BUDGET with just id and budget_code.

Create two models for the tables in ATKHOME/lib/Model as follows (you may need to create the Model directory if it doesnt exist)

class Model_TaskType extends Model_Table {
     public $entity_code='tasktype';
     public $table_alias='ty';

     function defineFields(){
           parent::defineFields();

           $this->newField('id')
               ->mandatory(true);;

           $this->newField('tasktype_desc')
                ->mandatory(true);

           $this->newField('budget_code')
                ->mandatory(true);
      }

      public function afterInsert($new_id){
        $ttb=$this->add('Model_TaskTypeBudget');
        $ttb->set('id',$new_id)
            ->set('budget_code',$this->get('budget_code'));
        $ttb->insert();
        return $this;
      }

      public function beforeUpdate(&$data){
        $ttb=$this->add('Model_TaskTypeBudget')->loadData($data['id']);
        $ttb->set('budget_code', $data['budget_code']);
        $ttb->update();
        return $this;
      }

      public function beforeDelete(&$data){
        $ttb=$this->add('Model_TaskTypeBudget')->loadData($data['id']);
        $ttb->delete();
        return $this;
      }
}

Note if you are using innoDB and have foreign keys, you have to do the insert and delete in the right order e.g. if there was a foreign key from TaskTypeBudget to TaskType on ID, then it should use beforeDelete and afterInsert to prevent constraint violations.

class Model_TaskTypeBudget extends Model_Table {
    public $entity_code='tasktype_budget';
    public $table_alias='tyb';

    function defineFields(){
            parent::defineFields();

            $this->newField('id')
                ->mandatory(true);

            $this->newField('budget_code')
                    ->mandatory(true);

    }
}

And a page in ATKHOME/page like this

class page_tasktype extends Page {

    function init(){
        parent::init();
        $p=$this;

        $tt=$this->add('Model_TaskType');
        $crud=$p->add('CRUD');
        $crud->setModel($tt, array('id','tasktype_desc', 'budget_code'));

        if($crud->grid)
          $crud->grid->addPaginator(10);
        }
}

Note also be careful to include an opening < ? php tag before each class line but DO NOT include a closing ? > as this may cause errors in the Ajax.

In ATKHOME/config-default.php, modify the mysql connection username and password from root/root to the user and password of your mysql database.

$config['dsn']='mysql://atktest:atktest@localhost/atktest';

and modify ATKHOME/lib/Frontend.php to uncomment line 8 which allows all pages to connect to the database (you can also just add the $this->dbConnect(); line to the page.

class Frontend extends ApiFrontend {
function init(){
    parent::init();
    $this->dbConnect();  //uncommented

In the same Frontend.php, insert the following around line 50 to add a button to the default menu to add our new page.

->addMenuItem('CRUD Test', 'tasktype')

Now go to your webbrowser and enter http://localhost/agiletoolkit and on the first page, click CRUD Test. Adding rows will result in a row being added to TASKTYPE and a row with the same id and budget_code to TASKTYPE_BUDGET. Editing the budget_code will be reflected in both tables and deleting the row will delete it from both tables.

How neat and simple is that once you know the functions are provided by ATk4 ?

于 2011-12-28T00:04:51.550 回答
1

您可以在模型级别添加相关实体:

https://stackoverflow.com/a/7466839/204819

如果这不是一个选项,您始终可以为“编辑”和“删除”按钮创建自定义处理程序,并使用内部 ORM 甚至在模型级别执行操作。

于 2011-12-26T21:28:51.427 回答