How use more models in one controller? - El Forum - 12-27-2007
[eluser]Pitrsonek[/eluser]
Hello,
I use CakePHP but today I start test CI.
I have one question, how i can use more models in one controller and call function which use db object.
I traid this code:
In autoload.php i have setting $autoload['libraries'] = array('database');
User Controller
Code: <?php
class User extends Controller {
function __construct(){
parent::Controller();
$this->load->library('layout', 'layout_client');
}
function index(){
$query = $this->db->get('users');
$data['query'] = $query->result();
$query2 = $this->db->query('SELECT * FROM POSTS');
$data['query2'] = $query2->result();
$this->layout->view('welcome_message', $data);
}
}
?>
When i show data from $data['query'] and $data['query2'] with var_dump it show same data. Data in $data['query'] == $data['query2']; But it is bad because Im sure dada must be differential.
I dont know how solve this problem.
THX Pitrsonek Czech Republic
How use more models in one controller? - El Forum - 12-27-2007
[eluser]Pitrsonek[/eluser]
I use ORACLE 10
How use more models in one controller? - El Forum - 12-27-2007
[eluser]tonanbarbarian[/eluser]
i would try the following change
Code: <?php
class User extends Controller {
function __construct(){
parent::Controller();
$this->load->library('layout', 'layout_client');
}
function index(){
$query = $this->db->get('users');
$data['query'] = $query->result();
$query->free_result();
unset($query);
$query2 = $this->db->query('SELECT * FROM POSTS');
$data['query2'] = $query2->result();
$query2->free_result();
unset($query2)
$this->layout->view('welcome_message', $data);
}
}
This will free up the results from the query.
As for loading a model that is simple
Code: $this->load->model('users');
You can call it is many times as needed to load as many models as you need.
So in your example you could do something like the following
Code: <?php
class User extends Controller {
function __construct(){
parent::Controller();
$this->load->library('layout', 'layout_client');
}
function index(){
$this->load->model('users');
$data['query'] = $this->users->getAll();
$this->load->model('posts');
$data['query2'] = $this->posts->getAll();
$this->layout->view('welcome_message', $data);
}
}
application/models/users.php
Code: <?php
class Users extends Model {
function Users() {
parent::Model();
} // Users
function getAll() {
$query = $this->db->get('users');
$result = $query->result();
$query->free_result();
unset($query);
return $result;
} // getAll()
} // class Users
application/models/posts.php
Code: <?php
class Posts extends Model {
function Posts() {
parent::Model();
} // Posts
function getAll() {
$query = $this->db->get('posts');
$result = $query->result();
$query->free_result();
unset($query);
return $result;
} // getAll()
} // class Posts
How use more models in one controller? - El Forum - 12-27-2007
[eluser]Pitrsonek[/eluser]
THX for your answer but this code doesnt fix the problem.
Code: <?php
class User extends Controller {
function __construct(){
parent::Controller();
$this->load->library('layout', 'layout_client');
}
function index(){
$query = $this->db->query('SELECT * FROM POSTS');
var_dump($query); echo '<br /><br />';
$data['query2'] = $query->result();
$query->free_result();
unset($query);
$query = $this->db->query('SELECT * FROM USERS');
var_dump($query);
$data['query'] = $query->result();
$this->layout->view('welcome_message', $data);
}
}
?>
Content vardump query:
Code: object(CI_DB_oci8_result)#13 (9) { ["stmt_id"]=> resource(28) of type (oci8 statement) ["curs_id"]=> NULL ["limit_used"]=> NULL ["conn_id"]=> resource(26) of type (oci8 persistent connection) ["result_id"]=> bool(true) ["result_array"]=> array(1) { [0]=> array(7) { ["id"]=> string(1) "1" ["created"]=> string(9) "23-PRO-07" ["title"]=> string(20) "Prvni pokusny clanek" ["seo_url"]=> string(20) "prvni-pokusny-clanek" ["content"]=> string(12) "asgasgasgasg" ["CATEGORY_ID"]=> string(1) "1" ["user_id"]=> string(1) "1" } } ["result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(1) }
object(CI_DB_oci8_result)#13 (9) { ["stmt_id"]=> resource(28) of type (oci8 statement) ["curs_id"]=> NULL ["limit_used"]=> NULL ["conn_id"]=> resource(26) of type (oci8 persistent connection) ["result_id"]=> bool(true) ["result_array"]=> array(1) { [0]=> array(7) { ["id"]=> string(1) "1" ["created"]=> string(9) "23-PRO-07" ["title"]=> string(20) "Prvni pokusny clanek" ["seo_url"]=> string(20) "prvni-pokusny-clanek" ["content"]=> string(12) "asgasgasgasg" ["CATEGORY_ID"]=> string(1) "1" ["user_id"]=> string(1) "1" } } ["result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(1) }
Both query are same .-(
How use more models in one controller? - El Forum - 12-27-2007
[eluser]tonanbarbarian[/eluser]
try putting the code into models then as I showed
I have not used oracle in years and never with CI so I cannot say if the issue is something to do with the CI implementation of oracle or not
are you using the CI database cache? if so try turning it off.
How use more models in one controller? - El Forum - 12-28-2007
[eluser]eggshape[/eluser]
Hi,
I load and use multiple models in an app I recently built. I can not show you the exact code (not allowed), but I can explain how I did it. Here is something similar:
Code: class Root_Controller extends Controller {
//every controller extends Root_Controller and inherits and overwrites these properties
protected $models = array(); // load models here
//The following is called in the constructor or save function of each controller to autoload the models
protected function set_models()
{
$models = $this->models;
if (count($models) > 0)
{
while ($model = array_shift($models))
{
$this->load->model($model);
}
}
}
// This function is called on form submission to save data to all the relevant models
protected function save_form()
{
$validate = $this->set_all_validation();
if ($validate and $this->validation->run() === false)
{
return 1;
}
$models = $this->models;
while ($model = array_shift($models))
{
if (! $this->$model->save())
{
log_message('error', 'Could not save model data');
return 1;
}
}
return 0;
}
}
As you may have noted that all the models share a save() function; you can copy this into every model, but I chose to add/replace the default Model class, so all my models inherit the same function. So in your Model class or model classes, you can then retrieve the field names appropriate to each model and save to the database.
Code: class Model {
// table name
protected $table = '';
// array to hold data for each model
protected $data = array();
// I do something like this:
// This retrieves existing data;
protected function init_data()
{
$this->db->where(array('id' => $id));
$query = $this->db->get($this->table);
if ($query->num_rows() === 1)
{
$this->data = $query->row_array();
}
}
// this function sets the keys for the data array using table field names
protected function set_fields()
{
$this->data = array();
// uses CI DB utility function
$fields = $this->db->list_fields($this->table);
// set keys to field names
while ($field = array_shift($fields))
{
$this->data[$field] = '';
}
}
// And in the save function you would iterate the $data array and assign any new
// $_POST data to the keys
protected function save()
{
foreach ($this->data as $field => $value)
{
if (isset($_POST[$field]))
{
$this->data[$field] = $this->input->post($field);
}
}
}
}
I hope this gives you enough clues to implement your own version.
|