Kohana ORM型号未加载

时间:2011-03-28 00:14:46

标签: kohana kohana-orm

在下面的代码中,“添加”操作按预期工作,但删除操作会引发错误,引用Kohana“无法删除收藏夹模型,因为它未加载。”

有什么想法吗?

if ($_GET['action'] == 'add')
{
    $favorites = ORM::factory('favorites');
    $favorites->question_id = $_GET['question_id'];
    $favorites->user_id     = Kohana_Facebook::instance()->user_id();
    $favorites->save();
}
elseif ($_GET['action'] == 'remove')
{
$favorites = ORM::factory('favorites')
    ->where('user_id', '=', $facebook->user_id())
    ->and_where('question_id', '=', $_GET['question_id'])
    ->find();
$favorites->delete();
}

var_dump($favorites)显示:

object(Model_Favorites)#24 (31) { 
["_table_name:protected"]=> string(9) "favorites" 
["_has_one:protected"]=> array(0) { } 
["_belongs_to:protected"]=> array(0) { } 
["_has_many:protected"]=> array(0) { } 
["_load_with:protected"]=> array(0) { } 
["_validation:protected"]=> NULL 
["_object:protected"]=> array(2) { ["user_id"]=> string(8) "60717257" ["question_id"]=> string(1) "2" } 
["_changed:protected"]=> array(0) { } 
["_related:protected"]=> array(0) { } 
["_valid:protected"]=> bool(false) 
["_loaded:protected"]=> bool(false) 
["_saved:protected"]=> bool(false) 
["_sorting:protected"]=> NULL 
["_foreign_key_suffix:protected"]=> string(3) "_id" 
["_object_name:protected"]=> string(9) "favorites" 
["_object_plural:protected"]=> string(11) "favoriteses" 
["_table_columns:protected"]=> array(2) { ["user_id"]=> array(13) { ["type"]=> string(3) "int" ["min"]=> string(11) "-2147483648" ["max"]=> string(10) "2147483647" ["column_name"]=> string(7) "user_id" 
["column_default"]=> NULL 
["data_type"]=> string(3) "int" 
["is_nullable"]=> bool(false) 
["ordinal_position"]=> int(1) 
["display"]=> string(2) "11" 
["comment"]=> string(0) "" 
["extra"]=> string(0) "" 
["key"]=> string(3) "PRI" 
["privileges"]=> string(31) "select,insert,update,references" } 
["question_id"]=> array(13) { ["type"]=> string(3) "int" ["min"]=> string(11) "-2147483648" ["max"]=> string(10) "2147483647" ["column_name"]=> string(11) "question_id" ["column_default"]=> NULL ["data_type"]=> string(3) "int" ["is_nullable"]=> bool(false) ["ordinal_position"]=> int(2) ["display"]=> string(2) "11" ["comment"]=> string(0) "" ["extra"]=> string(0) "" ["key"]=> string(3) "PRI" ["privileges"]=> string(31) "select,insert,update,references" } } ["_updated_column:protected"]=> NULL ["_created_column:protected"]=> NULL ["_primary_key:protected"]=> string(2) "id" ["_primary_key_value:protected"]=> NULL ["_table_names_plural:protected"]=> bool(true) ["_reload_on_wakeup:protected"]=> bool(true) ["_db:protected"]=> object(Database_MySQL)#23 (6) { ["_connection_id:protected"]=> string(40) "f9eb0f07846bef120d6d8414616f81f993f5306a" ["_identifier:protected"]=> string(1) "`" ["last_query"]=> string(98) "SELECT `favorites`.* FROM `favorites` WHERE `user_id` = '60717257' AND `question_id` = '2' LIMIT 1" ["_instance:protected"]=> string(7) "default" ["_connection:protected"]=> resource(73) of type (mysql link) ["_config:protected"]=> array(6) { ["type"]=> string(5) "mysql" ["connection"]=> array(3) { ["hostname"]=> string(9) "localhost" ["database"]=> string(17) "davekiss_dumbpoll" ["persistent"]=> bool(false) } ["table_prefix"]=> string(0) "" ["charset"]=> string(4) "utf8" ["caching"]=> bool(false) ["profiling"]=> bool(true) } } 
["_db_group:protected"]=> NULL 
["_db_applied:protected"]=> array(0) { } 
["_db_pending:protected"]=> array(0) { } 
["_db_reset:protected"]=> bool(true) 
["_db_builder:protected"]=> NULL 
["_with_applied:protected"]=> array(0) { } 
["_cast_data:protected"]=> array(0) { } }

3 个答案:

答案 0 :(得分:2)

是的,它没有加载到您的代码中。

您需要在find()

之前添加delete()

<强> UPD

正如我所看到的 - 它只是一个没有单个PK字段(id

的关系表

你可以用以下内容删除行:

DB::delete('favorites')->where('user_id', '=', $facebook->user_id())
                       ->and_where('question_id', '=', $_GET['question_id'])
                       ->execute(Database::instance());

或者您可以使用remove()方法(因为您似乎正在使用kohana ORM关系)

答案 1 :(得分:0)

面对同样的问题并修复解决方案

添加类似

的代码

protected $ _primary_key ='你的表主键';

答案 2 :(得分:0)

我在使用->where()->find()加载代码后遇到了同样的问题

确保

protected $_primary_key = 'id';

在模型中定义。