0

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

4

2 回答 2

1

看起来您的逻辑模型实际上位于两个表中。首先,决定哪个表是“主要的”,它可以是任何一种方式。我将通过创建以下模型使“标题”表成为主要表:

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

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

接下来,您需要将它与您的第二个表连接起来并从中添加字段。当您调用 $model->join() 时,它会将“SQL_Relation”对象返回给您,该对象可用于添加其他字段并创建更多连接。您可以创建新对象或扩展现有对象。

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

因为两个表都定义了相同的字段并且模型必须有唯一的字段,所以我为 detail.field1 定义了一个新名称。我还明确指定了用于加入的字段(idheader)。接下来,您将像使用任何其他模型一样使用新模型:

$form=$this->add('Form');
$form->setModel('Model_Record');
$form->onSubmit(function($form){
    $form->update()->js()->successMessage('success!')->execute();
});
于 2012-05-27T13:33:21.453 回答
0

传统的方法是使用带有“header”的网格并添加列扩展器,它显示带有“detail”的网格并且模型“detail”具有 setMasterField("idheader", $_GET["header_id"]);

例如

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

    }
}

未测试。

于 2012-05-27T10:51:06.353 回答