Вопрос

models.php

<?php defined('SYSPATH') or die('No direct script access.');

class Model_manage_feeds extends ORM  {
    protected $_table_name = 'User';
     public function manage_user(){

     $query = DB::select()->from('User');
     return $query;
     }
}

controller/user_details.php

public function action_manage_user() {
    $this->template->title = "Manage User";
    this->template->content = View::factory('manage_user');
}    

See my database structure,

ID Username Password Email is_active

  1. John xxxxxx john@gmail.com 1

  2. George xxxxxx george@aol.com 1

I want to display the username & emailid from the table.I am newly learning php and kohana,i know how to insert,edit & delete from kohana tutorial now i am trying to show the data from database.I don't know how to do.

Это было полезно?

Решение

Are you sure, this is the structure you want to be using? Usually when using ORM, you want one class to represent a specific party - in this case it would be users. Words like "manage" don't belong in the title of a model, since it should be universal.

Since your users don't need any validation/specific methods at this moment, your class could look like this (/models/User.php):

<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_User extends ORM {
    protected $_table_name = 'User';
}

It would probably be better practice to use Kohana's convention, the table name in the database would be users (since the table holds multiple users but an object of class Model_User only holds one user).

You don't need any more methods, especially none working with the DB. Because of the extends ORM, PHP knows how to handle this object and what database queries to run when, etc.

Now in your controller you can use the methods Kohana provided via ORM and just do something like the following:

$users = ORM::factory('User')->find_all();
foreach ($users as $user) {
    // access one user's data via $user->id, $user->username, etc.
}

So, how use this with Views? You basically need two templates, one holding the outer structure (your table, all that is beyond that) and one for the inner (one row of the table), this could be done like the following:

/views/main.php

<h1><?php echo $title; ?></h1>
<table>
<?php echo $rows; ?>
</table>

/views/userdetail.php

<tr>
  <td><?php echo $user->username; ?></td>
  <td><?php echo $user->email; ?></td>
</tr>

Your part of the controller managing this could be done like this

$view = View::factory('main');
$view->title = "Hello, this is the list of users";

$userlist = "";
$users = ORM::factory('User')->find_all();
foreach ($users as $user) {
    $detail_view = View::factory('userdetail');
    $detail_view->user = $user;
    $userlist .= $detail_view; // .= appends $detail_view to $userlist
}

$view->rows = $userlist;

I hope this gives you at least an overview over ORM and views in Kohana. If you have further questions, try the Kohana userguide, it usually offers great examples, e.g. for ORM and views.

Kohana is very flexible, so you can easily change the system, like setting non standard table name for a model, as you did. However, the conventions usually are quite sophisticated and especially when you are starting to learn PHP and the use of Kohana, I'd rather suggest going alongside them - even if it takes an extra hour or two to figure out how to do it - instead of doing your own thing.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top