简体   繁体   English

插入CodeIgniter中的联接表

[英]Inserting into joined tables in CodeIgniter

I just got a joined table set up and running in CodeIgniter; 我刚刚在CodeIgniter中建立并运行了一个联接表; I am retrieving data from the DB very easily using ActiveRecord. 我可以使用ActiveRecord轻松地从数据库中检索数据。 The next obstacle though, seems like the hardest: I am new to joins and I have no idea how I'm going to insert data into my joined tables. 但是,下一个障碍似乎是最困难的:我是联接的新手,我也不知道如何将数据插入联接的表中。 The data should go in looking the same as it comes out (meaning actual data, not row ids) but short of hard-coding arrays of associations and transforming the data before save, I don't know how I'm going to do that. 数据的外观应与输出的外观相同(意味着实际数据,而不是行ID),但缺少硬编码的关联数组并在保存之前转换数据,我不知道该怎么做。 But there has to be a way, right? 但是必须要有办法吧?

Right? 对?

I actually don't know CodeIgniter, but I can imagine how it should work based on other PHP ORMs. 我实际上并不了解CodeIgniter,但是我可以想象它应该如何基于其他PHP ORM起作用。

$something = new ModelObject();
$something->setSomeProperty($someVal);
$something->save();

$related = new RelatedObject();
$related->setModelId($something->getId());
$related->save();

In other words, once you create the object represented by one table, that object's property corresponding to the generated identifier is populated for you. 换句话说,一旦创建了一个表所代表的对象,便会为您填充与所生成的标识符相对应的对象属性。 Now you can use it when you create an object in the related ("join") table to set the foreign key property before saving. 现在,您可以在相关(“联接”)表中创建对象时使用它,以在保存之前设置外键属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM