Form with head and detail

2019-09-01 06:57发布

问题:

some time ago I've asked for something similar, but now I'm picking up with this, so asking again.

Maybe this question can sound simple but it's not for me.

I have two tables:

header (id,date,field1,field2) // This has One entry on the table
detail (id,idheader,field1,field2) // This can have multiple entries on the table

so header (1) ---> detail (N)

Which can be the best method to make a form and upate on the same $f->update() these two tables ?

Imagine that this can be a lot of things: an invoice, a budget, etc, etc

Very thanks

回答1:

It looks like your logical model is physically located in two tables. First, decide which table is "primary", it can be either way. I'll make "header" table primary by creating the following model:

class Model_Header extends Model_Table {
    public $table='header';
    function init(){
        parent::init();

        $this->addField('date')->type('date');
        $this->addField('field1');
        $this->addField('field2');
    }
}

Next you need to join it with your second table and add fields from it. When you call $model->join() it returns "SQL_Relation" object back to you which can be used to add additional fields and creating more joins. You can create either a new object or extend your existing.

class Model_Record extends Model_Table {
    public $table='header';
    function init(){
        parent::init();

        $this->addField('date')->type('date');
        $this->addField('field1');
        $this->addField('field2');

        $detail = $this->join('detail.idheader');
        $detail->addField('body');


        $details->addField('body_field1','field1');
    }
}

Because both tables define same field and model must have unique field, I defined a new name for detail.field1. I also explicitly specified field used for joining (idheader). Next, you are using new model just like any other model:

$form=$this->add('Form');
$form->setModel('Model_Record');
$form->onSubmit(function($form){
    $form->update()->js()->successMessage('success!')->execute();
});


回答2:

traditional approach would be to have grid with "header" and add column expander, which shows grid with "detail" and model "detail" has setMasterField("idheader", $_GET["header_id"]);

e.g.

class page_test extends Page {
    function initMainPage(){
        $g = $this->add("Grid"); // or CRUD
        $g->setModel("header");
        $g->addColumn("expander", "details");
    }
    function page_details(){
        $this->api->stikcyGET("header_id");
        $g = $this->add("Grid"); // or CRUD
        $m = $this->add("Model_detail")->setMasterField("idheader", $_GET["header_id"]);
        $g->setModel($m);

    }
}

not tested.



标签: atk4