Kohana ORM 模型未加载

发布于 2024-10-27 07:27:35 字数 3878 浏览 2 评论 0原文

在以下代码中,“添加”操作按预期工作,但删除操作会引发错误,指出 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) { } }

In the following code, the "add" action works as expected, but the remove action throws an error citing that Kohana "Cannot delete favorites model because it is not loaded."

Any ideas?

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();
}

A var_dump($favorites) show this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

披肩女神 2024-11-03 07:27:36

使用 ->where()->find() 加载代码后,我遇到了同样的问题
只需确保

protected $_primary_key = 'id';即可。

在模型中定义

I have the same issue after loading my codes with ->where() and ->find()
Just make sure that the

protected $_primary_key = 'id';

is defined at the model.

┼── 2024-11-03 07:27:35

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

您需要在 delete() 之前添加 find()

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 关系)

Yes, it is not loaded in your code.

You need to add find() before your delete()

UPD:

As I can see - it is just a relation tables without single PK field (id)

You could delete row with something like:

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

Or you could use remove() method (since seems like you're using kohana ORM relations)

墟烟 2024-11-03 07:27:35

面临同样的问题并通过解决方案

添加代码来修复,例如

protected $_primary_key = 'yourtablesprimarykey';

Face same issue and fixed with the solution

add code like

protected $_primary_key = 'your tables primary key';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文