问题描述
任何人都可以为CodeIgniter指出一个很好的多对多数据库教程。
Can anyone point out a good many-to-many database tutorial for CodeIgniter.
只是试图找出创建过程,多对多关系。我的示例使用多选值,想知道如何照顾更新等监视更改。
Just trying to work out the process of creating, and then updating a many-to-many relationship. My example uses a multi-select of values, wondering how you take care of monitoring changes on update etc.
推荐答案
喜欢分享我在我的应用程序做什么。这与我在。
I'd like to share what I do in my application. This is basically same with my answer in this question.
- 在用户提交之后,在进入数据库之前,我将数据库中的现有数据提取到数组中。示例:
$ collection = array('111','112','113','114');
(这只是一个例子,从数据库然后把值放到数组) - 我将在两个步骤中检查新用户输入。第一步是查看它是否已经在数据库中。如果没有,则插入。否则忽略:
- After user submit, and before entering to database, I will fetch the existing data in the database into an array. Example:
$collection = array('111', '112', '113', '114');
(This is just for example. In real, it should fetch from database then put the value to array) - I will check the new user input in two step. First step is to see if it already in the database or not. If it not, then insert. Otherwise ignore:
foreach ( $inputs as $input )
{
if ( ! in_array($input, $collection) )
{
//do insert here
}
}
foreach ( $collection as $data )
{
if ( ! in_array($data, $inputs) )
{
//do delete here
}
}
在你的情况下,你可能需要或可能不需要第二个循环。我需要这个,因为我把输入作为复选框,用户可以选择激活/停用,因此我翻译它作为插入和删除。
In your case, you might or might not need the second loop. I needed this since I make the input as checkboxes, that the user can choose to activate / deactivate, thus I translate it as insert and delete.
因为你将实现它使用多重选择,则基本上与我的复选框相同。
Since you will implement it using multi-select, then basically it's same with my checkboxes.
如果您有结构或代码示例,请随时分享,我会帮助您微调(当然有我的风格,可能或可能不会优化)。
If you have structure or code example, feel free to share it, and I will help you fine tune it (of course with my style, that might or might not optimized yet).
这篇关于CodeIgniter多对多关系管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!