In the following code, th开发者_开发问答e "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) { } }
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)
Face same issue and fixed with the solution
add code like
protected $_primary_key = 'your tables primary key';
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.
精彩评论