2011-03-28 36 views
0

在以下代碼中,「add」操作按預期工作,但remove操作會引發Kohana引發錯誤「無法刪除收藏夾模型,因爲它未加載」。Kohana ORM Model not loading

任何想法?

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) { } } 

回答

2

是的,這是不是在你的代碼加載。

你需要你的delete()

UPD之前添加find()

我所看到的 - 它只是沒有單一的PK場關係表(id

你可以刪除行類似於:

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

或者你可以使用remove()方法(因爲好像你正在使用Kohana的ORM關係)

+0

我試過這個無濟於事。我已經更新了上面的代碼以反映這種變化。同樣的錯誤。 – 2011-03-28 00:23:22

+0

@Dave Kiss:'var_dump($ favorites);' – zerkms 2011-03-28 00:24:02

+0

@zerkms問題已更新 – 2011-03-28 00:28:37

0

面對同樣的問題,固定的解決方案

添加如下代碼

保護$ _primary_key =「你的表中的主鍵';

0

我有我的加載代碼與->where()->find() 後,同樣的問題,只是要確保

protected $_primary_key = 'id';

在模型中定義。