'Admin_sysQuestionOption', -6ren">
gpt4 book ai didi

kohana - 删除相关记录,有has_many关系

转载 作者:行者123 更新时间:2023-12-02 05:41:49 26 4
gpt4 key购买 nike

我在两个表之间有 has_many 关系like : "questions"-< "options"[一个问题有很多选项]我的类(class)结构是:提问

class Model_Admin_sysQuestion extends ORM {

protected $_table_name = 'questions';

protected $_has_many = array(
'options' => array(
'model' => 'Admin_sysQuestionOption',
'foreign_key' => 'question_id',
),
);... .

选项

Class Model_Admin_sysQuestionOption extends ORM {

protected $_table_name = 'questions_options';
protected $_belongs_to = array(
'question' => array(
'model' => 'Admin_sysSection',
'foreign_key' => 'question_id',
),
); .... .

我正在尝试使用以下代码删除所有选项的问题:

 $question = ORM::factory('Admin_sysQuestion', 30);
$question->options->delete($question->id);
$question->delete();

但是报错

error":"Cannot delete admin_sysquestionoption model because it is not loaded."

有什么想法吗?怎么做?

最佳答案

加载多个关系时必须调用find_all:

foreach($question->options->find_all() as $option)
{
$option->delete();
}

或者使用DB QBuilder进行多次删除:

DB::delete('questions_options')
->where('question_id', '=', 30)
->execute($this->_db);

关于kohana - 删除相关记录,有has_many关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10845751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com