dana
dana

Reputation: 5208

Kohana simple read from database

i am trying to retrieve some simple data from the database, and i don't know where i am wrong.

i want to get all the inactive users from my database. For that, i have in my controller:

    public function action_useremails()
{   
    $users = Model::factory('user')->where('user_status', '=', 2);  
    $this->view->users = $users;
}

and in the view:

 <? foreach ($users as $user): ?>
    <tr>
        <td><span><?= $user; ?></span></td>

    </tr>
<? endforeach; ?>

butm even though i have inactive users, i don't see anything in the view. I wonder where am i wrong? }

Upvotes: 0

Views: 313

Answers (3)

for v3.0 add the find_all() method at the end:

$users = Model::factory('user')->where('user_status', '=', 2)->find_all();

It executes the query you built before with the other methods.

Upvotes: 0

Sai Prasad
Sai Prasad

Reputation: 735

If you are talking about Kohana 2, your query must be:

$users = Model::factory('user')->where('user_status', 2)->find();

Note the args in where() block & the find() at last. Without find(), you are just building the query, but not running it.

Docs: http://docs.kohanaphp.com/libraries/orm#find

If it is KO3, it has to be similar to the above.

Upvotes: 0

Matteo Riva
Matteo Riva

Reputation: 25060

You're building the query, but not executing it. Try:

$this->view->users = $users->execute();

also consider that the $users variable in the view will be an array of arrays, you'll have to echo the right element.

Upvotes: 1

Related Questions