插入到关联表中


Inserting into an associative table

假设我有表1和表2,每个表都有一个带有动态输入字段的表单,用于将数据插入这些表。通常我只会直接获取最后一个插入 ID 以将其插入单个表的外键中,但现在我正在处理两个表,加上动态输入字段,并且不知道如何将它们插入到多对多关系的关联表中。我正在考虑将最后一个插入 ID 存储在数组中,但我的想法停在那里,我什至不确定。

table1
------
t1_id (pk)
desc
assoctable
------
status
t1_id (fk)
t2_id (fk)

table2
------
t2_id (pk)
attrb

更新:我没有包含我的代码,但就是这样。到目前为止,它们确实没有问题,并且可以将用户输入插入数据库。

查看

<input type="text" class="form-control" name="desc[]" id="row1">
<input type="text" class="form-control" name="attrib[]" id="row2">
<button type="button" class="btn btn-default" onclick="addRow1()">Add Row</button>
<button type="button" class="btn btn-default" onclick="addRow2()">Add Row</button>

控制器:

$descs = $this->input->post('desc');
$attrbs = $this->input->post('attrb');
foreach($descs as $key => $val){
    $fields = array(
        'desc' => $val
    );
    $table1_result = $this->model_test->insert_table1($fields);
}
foreach($attrbs as $key => $val){
    $fields = array(
        'attrb' => $val
    );
    $table2_result = $this->model_test->insert_table2($fields);
}

型:

function insert_table1($data) {
    $this->db->insert('table1', $data);
}
function insert_table2($data) {
    $this->db->insert('table2', $data);
}

您正在使用某种 ORM,其中每个对象表示特定表中的一个实体。要执行您尝试执行的操作,您需要实现一个事务,该事务通过提交所有查询或不提交任何查询来确保数据一致性。以下是一些通用代码,可帮助您走上正轨:

// HUGE CAVEAT! This assumes that $desc and $attr arrays are aligned
try {
    $this->db->startTransaction();
    foreach ($descriptions as $key => $description) {
        $table1 = new Table1;
        $table2 = new Table2;
        $linkingTable = new LinkingTable;
        $table1->setDescription($description);
        $table1->save();
        $table2->setAttribute($attributes[$key]);
        $table2->save();
        // Here is where the magic happens, notice that are making a call to getId on both objects, so even though the transaction hasn't committed yet, the database has allocated us the ids
        $linkingTable->setTable1Id($table1->getId());
        $linkingTable->setTable2Id($table2->getId());
        $linkingTable->save();
    }
    $this->db->commit();
} catch (Exception $e) {
    error_log(sprintf('Exception: %s', $e->getMessage()));
    $this->db->rollback();
}
--

参考评论 --

不知道表单是如何设置的,但通常表单应该表示一个主要对象,能够通过联结(或我喜欢称之为链接表)将其他对象链接到它。

以书店为例。您创建一个表单,您可以在其中将一本书保存到商店,作为表单的一部分,您可能有多个作者共同创作了该书。当你保存表单时,这本书只是一个对象,所以你不必担心数组对齐b/c,你将只有一个数组,作者。

如果您无法按照书籍/作者示例设置表单,那么理论上,您可以通过关联每个表单元素(如descs[1])来对齐您的键,并确保assocs[1]对齐,因此当您提交表单时,我给出的前面的示例应该有效。

这就是我想出的。感谢迈克先生启发我。每次插入后,我分别存储了两者的最后一个 id。

$table1_id = array();
foreach($descs as $key => $val){
   $fields = array(
       'desc' => $val
);
   $table1_result = $this->model_test->insert_table1($fields);
   $table1_id[] = $this->model_test->getlastid();
}

表 2 具有相同的类似代码。然后这就是我将它们插入连接表的方式。

foreach($table1_id as $key => $val) {
    foreach($table2_id as $key2 => $val2) {
        $field = array(
            'status' => '0',
            't1_id' => $val2,
            't2_id' => $val
        );
        $result = $this->model_admin->insert_assoctable($field);
    }
}