CodeIgniter Forums
Call to model returning boolean instead of array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Call to model returning boolean instead of array (/showthread.php?tid=85904)



Call to model returning boolean instead of array - bassmandc - 12-19-2022

I have four tables that each have a virtually identical model.  Three of them work perfectly, but one returns boolean instead of an array of results.  I'm racking my brain trying to figure this out.  I can replace the model calls in the controller line-by-line with the equivalent calls from any other model, and I get the expected array.  I put the fourth one in and I get a boolean.  The only difference I can see / find is that the table that is returning boolean only has a single line of data, where the others have multiple lines of data.  What am I missing here?
TeamsModel.php:
Code:
<?php

namespace App\Models;

use CodeIgniter\Model;

class TeamsModel extends Model
{
    protected $table = 'tiv_teams';

    protected $allowedFields = ['team_name', 'team_owner', 'team_members', 'added_on', 'added_by',];


    public function my_teams()
    {
        $conditions = [
            'team_owner' => session()->get('id'),
        ];
        $rawTeams = $this->where($conditions)->findAll();
        $teams = array_sort_by_multiple_keys($rawTeams, [
            'team_name' => SORT_ASC,
        ]);

        return $teams;
    }
}

TestController.php:
Code:
<?php

namespace App\Controllers;

use App\Models\TeamsModel;

class TestController extends BaseController
{
    public function index()
    {

        $teamsmodel = new TeamsModel();

        $test1 = $teamsmodel->my_teams();

        $data = [
            'title' => 'test page',
            'test1' => $test1,
        ];

        return view('test', $data);
    }
}

test.php (View)

Code:
<?= $this->extend('layouts/default') ?>
<?= $this->section('content') ?>
    <?php if (isset($test1)): ?>
        <p>TEST 1: <?php var_dump($test1) ?></p>
    <?php endif; ?>

<?= $this->endSection() ?>

Result:
TEST 1: bool(true)


RE: Call to model returning boolean instead of array - kenjis - 12-19-2022

array_sort_by_multiple_keys() returns bool.
See https://codeigniter.com/user_guide/helpers/array_helper.html#array_sort_by_multiple_keys


RE: Call to model returning boolean instead of array - bassmandc - 12-19-2022

Thanks a bunch for the solution.  I'm a dummy - I just realized the difference between where I was getting the boolean was that I assigned it to a variable in this one, where I simply ran the function in the others.
Returned boolean:
$var = array_sort_by_multiple_keys($teams, [
           'team_name' => SORT_ASC,
        ]);

Returns sorted array:
array_sort_by_multiple_keys($teams, [
          'team_name' => SORT_ASC,
        ]);
Time to put down my mouse and pick up a whiskey.  Thanks again.