• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 18
  • Last Modified:

Query not executing correctly

I have this code:
  $userid = Input::post('user');
    $productid = Input::post('product');
    $record = Model_Favorite::find('all',array('where'=>array(array('userId'=>$userid),array('productId'=>$productid))));
    $record = $record[1];
    if($record){
       $record->delete();
    }

Open in new window


It suppose to get the userid and productid then find the whole record and delete it, but it isn't deleting anything from the database. it gets the whole record i tried that out but then
$record[1] 

Open in new window

is null every time, I also tried to remove it but I got an error where i cant call to function. I tried to do

$record = null;

Open in new window

before and i also tried to add
->asObject()->exectue();

Open in new window

but it also didnt work! Can anyone help?

I get this when i var dump $record, i think i have a mistake there:
array(1) { [2]=> object(Model_Favorite)#26 (9) { ["_is_new":protected]=> bool(false) ["_frozen":protected]=> bool(false) ["_data":protected]=> array(3) { ["id"]=> string(1) "2" ["userId"]=> string(2) "99" ["productId"]=> string(3) "908" } ["_original":protected]=> array(3) { ["id"]=> string(1) "2" ["userId"]=> string(2) "99" ["productId"]=> string(3) "908" } ["_data_relations":protected]=> array(0) { } ["_original_relations":protected]=> array(0) { } ["_reset_relations":protected]=> array(0) { } ["_view":protected]=> NULL ["_iterable":protected]=> array(0) { } } }

Open in new window

0
Jazzy 1012
Asked:
Jazzy 1012
  • 3
  • 2
1 Solution
 
Chris StanyonCommented:
Arrays are zero-based, so the first record will have an index of 0

You probably want:

$record[0]
0
 
Jazzy 1012Author Commented:
I did this:
              $record = Model_Favorite::find('all',array('where'=>array(array('userId'=>$userid),array('productId'=>$productid))));
              $record = $record[0];
              var_dump ($record);die;

I got "NULL"
0
 
Chris StanyonCommented:
OK. Without knowing what your static find() method is doing, it's a little tricky to figure out.

Couple of tips. Turn on error reporting while you're debugging. And don't name you single record the same as your find() result. Gets confusing when you say you var_dumped $record - are you referring to the result of find or the variable assignment. After turning on error reporting, try the following:

$favorites = Model_Favorite::find('all',array('where'=>array(array('userId'=>$userid),array('productId'=>$productid))));
var_dump($favorites);
$record = $favorites[1];
var_dump($record);

Open in new window


In your original code, you say you var_dumped $record - the result of that var_dump shows that it's an array and as I said - not sure whether that is the result or the assignment. Either way, it's not an object so you can't call methods on it. It looks like your array contains an object, so you may have to get to the object as well, which does look to be the zero index of the var_dumped value.
0
 
Jazzy 1012Author Commented:
I did your exact code and i got this for both vardumps, i put two line breaks between.
Also when I print_r the find, i get the right query and when I execute it in the database, i have data returned.

array(1) { [2]=> object(Model_Favorite)#26 (9) { ["_is_new":protected]=> bool(false) ["_frozen":protected]=> bool(false) ["_data":protected]=> array(3) { ["id"]=> string(1) "2" ["userId"]=> string(2) "99" ["productId"]=> string(3) "908" } ["_original":protected]=> array(3) { ["id"]=> string(1) "2" ["userId"]=> string(2) "99" ["productId"]=> string(3) "908" } ["_data_relations":protected]=> array(0) { } ["_original_relations":protected]=> array(0) { } ["_reset_relations":protected]=> array(0) { } ["_view":protected]=> NULL ["_iterable":protected]=> array(0) { } } } 

NULL

Open in new window

0
 
Chris StanyonCommented:
Hmmm. Looking at that, the index of your record in the array is 2 - array(1) { [2]=> object.

As I said without know your find() method, not sure what's going on behind the scenes, but it does look like you need to use $favourites[2]
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now