简体   繁体   English

如何在CodeIgniter Datamapper中使用cascade_delete配置选项

[英]How to use the cascade_delete config option in CodeIgniter Datamapper

I use MySQL using InnoDB tables with CodeIgniter Datamapper in my PHP application. 我在PHP应用程序中将MySQL和InnoDB表与CodeIgniter Datamapper一起使用。 Often, the user is given the option of deleting a record through the app by initiating a ->delete function call. 通常,通过启动->删除功能调用,用户可以选择通过应用程序删除记录。 When a record has child records (one-to-one or one-to-many), I would also like these records to be deleted along with the parent record, if it is stated by FK constraints in the database. 当一条记录具有子记录(一对一或一对多)时,如果数据库中的FK约束说明,我也希望这些记录与父记录一起删除。

In this case, I have 2 tables, items and input_lines. 在这种情况下,我有2个表,项目和input_lines。 I have confirmed that both are using InnoDB. 我已经确认两者都在使用InnoDB。 Each item can have many input_lines, so input_lines has a field called item_id, which is set to NULL, indexed, and have FK constraints (ON CASCADE DELETE and ON CASCADE UPDATE). 每个项目可以有许多input_lines,因此input_lines具有一个称为item_id的字段,该字段设置为NULL,已索引并且具有FK约束(ON CASCADE DELETE和ON CASCADE UPDATE)。 I have set the config element in the DM config file as 我已将DM配置文件中的config元素设置为

$config['cascade_delete'] = FALSE

Because in the documentation it says you should do that if you are using ON UPDATE/DELETE CASCADE. 因为在文档中说如果使用ON UPDATE / DELETE CASCADE,则应该这样做。 However, when the user initiates the $item->delete() method, only the item is deleted, and the item_id fields on the input_line records associated with the item are set to null. 但是,当用户启动$ item-> delete()方法时,仅删除该项目,并且与该项目相关联的input_line记录上的item_id字段设置为null。

My models look like this: 我的模型如下所示:

class Item extends DataMapper {

  public $has_many = array('labour', 'item_type', 'input_line', 'custom_item_type');

  ...
}

class Input_line extends DataMapper {

  public $has_one = array('item');
  ...
}

I have tried this with cascade_delete = false and true and it won't work. 我已经尝试过使用cascade_delete = false和true来进行此操作,它将无法正常工作。 I know the constraints work because deleting the record with MySQL directly works as expected, deleting the child records. 我知道约束起作用,因为使用MySQL删除记录可直接按预期方式工作,即删除子记录。

What am I missing? 我想念什么? Why is it setting the FK fields to null instead of deleting the record? 为什么将FK字段设置为null而不是删除记录?

EDIT 1: 编辑1:

I decided against my better judgment to debug the delete function in datamapper.php (libraries directory). 我决定以更好的判断来调试datamapper.php(库目录)中的删除功能。

I noticed this code in that function: 我在该函数中注意到了以下代码:

// Delete all "has many" and "has one" relations for this object first
foreach (array('has_many', 'has_one') as $type)
{
    foreach ($this->{$type} as $model => $properties)
    {
        // do we want cascading delete's?
        if ($properties['cascade_delete'])
        {
        ....

So I var_dumped the contents of $properties, and I saw this: 所以我var_dumped $ properties的内容,我看到了:

array (size=8)
  'class' => string 'labour' (length=6)
  'other_field' => string 'item' (length=4)
  'join_self_as' => string 'item' (length=4)
  'join_other_as' => string 'labour' (length=6)
  'join_table' => string '' (length=0)
  'reciprocal' => boolean false
  'auto_populate' => null
  'cascade_delete' => boolean true

It appears the default for when the model doesn't have the property specifically initialized is overriding the config value. 当模型没有专门初始化的属性将覆盖配置值时,它将显示为默认值。 This seems like too glaring a mistake so there's definitely something I'm doing wrong somewhere...right? 这似乎太过犯错了,所以肯定某个地方我在做错什么...对吗? I really, really want to avoid hacking the DM core files... 我真的很想避免破坏DM核心文件...

EDIT 2: 编辑2:

I was thinking maybe the config file wasn't being found, but I checked the logs and there're entries stating that the Datamapper config file was successfully loaded, so that's not the issue. 我以为可能找不到配置文件,但是我检查了日志,并有条目指出Datamapper配置文件已成功加载,所以这不是问题。

Doesn't look like anyone has any answers. 看起来没有人有任何答案。

My solution was to change the property in the datamapper library $cascade_delete to false, since it's set to true right now. 我的解决方案是将datamapper库$ cascade_delete中的属性更改为false,因为现在将其设置为true。 It's unfortunate that I have to resort to hacking the core, but DM won't respect my changes in the config file for cascade_delete so I have no other choice. 不幸的是,我不得不求助于内核,但是DM不会尊重我对级联_删除的配置文件所做的更改,因此我别无选择。

If anyone comes across this question and has encountered an issue like this before, please comment. 如果有人遇到此问题,并且之前遇到过此类问题,请发表评论。

I have come across the same problem, but finally I just did like this: 我遇到了相同的问题,但最终我只是这样做:

$sql = "DELETE FROM EVENT WHERE event_id=".$event_id.";";
$this->db->query ($sql );

In case we set "ON DELETE CASCADE" for the foreign key which refers to event_id, the above SQL works fine, so I just call it directly. 如果我们为引用event_id的外键设置“ ON DELETE CASCADE”,则上述SQL可以正常工作,因此我直接调用它即可。

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

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