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

LVL 1
Jazzy 1012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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 StanyonWebDevCommented:
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 StanyonWebDevCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.