Welcome Guest, Not a member yet? Register   Sign In
Model Find function Changed?
#1

In the past when I used the find() function on a model while passing the primary key, I would get a single result as the documentation states. It seems now in v. 4.6.0 it acts like it is going to return multiple results. This requires me to call $returnedArray[0]['name'] instead of just $returnedArray[0]['name']

My Relevant Code:
PHP Code:
$transID $this->request->getPost('trans_id',FILTER_SANITIZE_NUMBER_INT);
$transModel = new \App\Models\TransactionModel();
$prevTran $transModel->find($transID);

//Make sure this payment intent isn't already paid
if($prevTran['status'] == 'paid') {
                    
    
//This transaction is already paid
    
echo json_encode(array('status'=>'alread_paid','message'=> 'This transaction is already marked as paid.'));
    exit();
                    



This results in error:
Quote:message 'Undefined array key "status"'

Upon inspecting the return from the database I can see the issue is that it's returning the row within a multidimensional array.

Expected Return:
Code:
$transModel = [
    'id'         => 1,
    'name'    => 'Registration',
    'status'    => 'paid',
]

Actual Return
Code:
$transModel = [
    [0]    =>    [
        'id'         => 1,
        'name'    => 'Registration',
        'status'    => 'paid',
    ],
]

I can do a work around by just overwriting the response. But am I wrong in believing this should already be the default response.

PHP Code:
$transID $this->request->getPost('trans_id',FILTER_SANITIZE_NUMBER_INT);
$transModel = new \App\Models\TransactionModel();
$prevTran $transModel->find($transID);

//Overwrite the return here
$prevTran $prevTran[0];

//Make sure this payment intent isn't already paid
if($prevTran['status'] == 'paid') {
                    
    
//This transaction is already paid
    
echo json_encode(array('status'=>'alread_paid','message'=> 'This transaction is already marked as paid.'));
    exit();
                    

Reply
#2

Never mind...... I made a dumb mistake ....... Disregard
Reply




Theme © iAndrew 2016 - Forum software by © MyBB