What is the best way to create my model so I can call it from my controller like this?
$this->model_model->function->dbname
I have the following model but its rubbish:
Model:
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
Update:
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
Why would you want to? Why not call it like this?:
$this->model_model->functionname('dbname');
A complete skeleton of a model is like this:
<?php
class Mymodel extends Model
{
function get_some_entries($number_rows)
{
return $result = $this -> db -> query("SELECT id, name
FROM tablename
LIMIT $number_rows");
}
}
?>
Best way? You can probably read on CodeIgniter general guide and alter to your situation. But the basic idea is that, you create the model class then load from your controller. Then just call the model function accordingly. For example,
class Custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
...
...
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
}
<?php
class CustomController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('custom_model', 'fubar');
}
public function index()
{
$result = $this->fubar->systemName('dbname');
print_r ($result);
}
}
?>
Related
could ya'll help me on this one please. I'm trying to count how many are there in my table as it doesn't show up the result on my view.
Controller
public function enrollment_summary()
{
$data['title'] = 'Enrollment Summary';
$this->load->model('report_model');
$data['query'] = $this->report_model->get_students();
$this->load->view('report_enrollment_summary', $data);
}
Model
<?php
class report_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_students()
{
$query = $this->db->count_all('students');
}
}
View
<body>
<h1>Enrollment Summary</h1>
<?php echo $query; ?>
</body>
Your get_students() is not returning anything?
So you need to add in a return.
Your current method...
public function get_students()
{
$query = $this->db->count_all('students');
}
becomes
public function get_students()
{
$query = $this->db->count_all('students');
return $query;
}
OR
public function get_students()
{
return $this->db->count_all('students');
}
Can I obtain the model directly from a query in Code Igniter?
Model
<?php
class User_Model extends CI_Model {
public $user_id;
public $name;
public $team_id
public function getTeamName() {
$this->db->select('name');
$this->db->from('team');
$this->db->where('team_id',$this->team_id);
$query = $this->db->get();
$team = $query->row();
if($empresa == null){
return "";
} else {
return $team->name;
}
}
public function getUser($id) {
$query = $this->db->get_where('users', array('user_id' => $id));
$user = $query->row();
return $user;
}
I would like getUser($id) function returns a user object like 'new User_Model()' to be able to use getTeamName() function in the view. Can I do it?
Now returns an object, but not a User_Model() object.
is wrong call a method in the model from the view.
in the model use
function getUser($id){
$query = $this-db->select('*')
->from('users')
->where('user_id', $id)
->get();
return $query->result();
}
this return a object with all data, then you call the method in the controller like this
$data['user_model'] = $this->model_name->getUser($id);
$this->load->view('Your_View',$data);
then in the view $user_model is the object
in the view
print_r($user_model);
As long as the model is loaded, in your view you can access any public function or property like:
$this->user_model->getUser() or $this->user_model->team_id
View:
<?php
echo $this->user_model->getUser(1)->username;
This is against MVC in some respects but I can understand the logic behind doing so based on the following scenario where you want to say use num_rows and result without doing count:
class User_Model extends CI_Model {
public function getUsers() {
return $this->db->get('users');
}
}
class Some_Controller extends CI_Controller {
public function index() {
$this->load->model('user_model');
$data['users'] = $this->user_model->getUsers();
$this->load->view('some_view', $data);
}
}
View:
<?php
if ($users->num_rows() > 0) {
foreach ($users->result() as $row) {
echo $row->username;
}
} else {
echo 'No users';
}
Actually there is another possibility you can use
create a Team_model and an User_model
The Team_model could look like
class Team_model extends CI_Model
{
$arrTeamNames = [];
public function getTeamName($team_id)
{
if (!isset($this->arrTeamNames[$team_id]))
{
$query = $this->db
->select('name')
->from('team')
->where('team_id',$this->team_id)
->get();
if ($query->num_rows() == 1)
{
$objTeam = $query->row();
$this->arrTeamNames[$team_id] = $objTeam->name;
return $objTeam->name;
}
return '';
}
return $this->arrTeamNames[$team_id];
}
}
and the User_model
class User_Model extends CI_Model
{
public function getUser($id)
{
$query = $this->db->get_where('users', array('user_id' => $id));
if ($query->num_rows() == 1)
{
$user = $query->row(0, 'User_Object');
return $user;
}
}
}
class User_Object
{
public function getTeamName()
{
$ci = get_instance();
$ci->load->model('Team_model');
return $ci->team_model->getTeamName($this->team_id);
}
}
Note in the User_model you have an additional User_Object which
gets mapped by the row() function which is well documented
here.
Now if you call this model in one of your controller and pass it to your view you actually can access to a model function in a proper way e.g.
class User extends CI_Controller
{
public function detail($user_id)
{
$this->load->model('User_model');
$objUser = $this->User_model->getUser($user_id);
$this->load->view('user-detail', ['objUser' => $objUser]);
}
}
//user-detail view
echo $objUser->getTeamName();
I am new to codeigniter and trying to get a percentage result outputted into the view of codeigntier for my project. Once I fix this, things will be much smoother.
He is the model function:
<?php
public function _construct()
{
$this->load->database();
}
public function percent_specilation1($numstudent)
{
$query = $this->db->query("SELECT * FROM Student WHERE Student.JRNOption ='1'");
$numstudent = $query->num_rows()/25;
if($query->num_rows() > 0){
return $numstudent;
}
return null;
}
?>
Here is the controller.
<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
parent::_construct();
$this->load->model('Stats_model');
}
public function index(){
//echo "Various stats for the Journalism department:";
$data['Test'] = $this->Stats_model->percent_specilation1($numstudent);
}
public function view($page = 'Statsview')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
//$data['title'] = ucfirst($page);
$this->load->view('templates/header',$data);
$this->load->view('Statscontroller/index',$data);
$this->load->view('templates/header');
}
?>
Here is the view
<html>
<h1> Various Statistics for the Journalism department</h1>
<?php
echo "Percent of students that are undecided:";
echo $Test; ?>
</html>
What am I doing wrong? I been trying to get it right for over an hour and I just want to output the single result from the database query. Any help you can provide would be great.. please be respectful. Thanks!
Try this code, I have updated some of code of controller and model and use primary key of student table for student_id in model query.
Update Model:
<?php
public function _construct()
{
$this->load->database();
}
public function percent_specilation1()
{
$query = $this->db->query("SELECT (SELECT COUNT(s2.student_id)
FROM Student s2
WHERE s2.JRNOption='1')*100/COUNT(s1.student_id) AS percentage
FROM Student s1");
$result = $query->row_array();
return $result['percentage'];
}
?>
Updated controller:
<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
parent::_construct();
$this->load->model('Stats_model');
}
public function index(){
//echo "Various stats for the Journalism department:";
$data['Test'] = $this->Stats_model->percent_specilation1();
$this->load->view('templates/header',$data);
$this->load->view('Statscontroller/index',$data);
$this->load->view('templates/header');
}
}
?>
Hope this will help you.
I Have Defined MY_ Controller in my core folder.
Then i have two controllers:
Admin_Controller
Customer_Controller
Now i want to put a query into my Customer_Controller whose result i can access all the controller which extends to Customer_Controllers.
I have put this code in Customer_Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
Now when i have a child class something like this
<?php
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//Get Results from Customer_Controller
}
}
how do this?
you can follow this:
class Customer_Controller extends My_Controller
{
public function customer()
{
return 'costomers';
}
}
then
class User extends Customer_Controller
{
//call the function from Customer_Controller
$this->customer();
}
Let me give you a suggestion. Instead of creating a controller and extending the same, why don't you create a library class.
And then load your library inside the controller you want.
for example here is your library class file.
<?php
class users{
private $session;
public function __construct(){
$CI =& get_instance();
$this->session=$CI->session;
}
public function get_users()
{
// do the code and return the
}
}
AND in your controller
<?php
class User extends CI_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('users');
$users = new users();
$userinfo = users->get_users();
//Results from library
}
}
You Try get_instance() like this in you User Controller
public function get_users()
{
$id = $this->session->userdata('id');
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
return $result;
}
class User extends CI_controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
$CI=&get_instance();
$result=$CI->get_users();
foreach($result as $row)
{
echo $row->id;//here add you table field name for id
}
}
}
Codeigniter is MVC framework.
So You will put get_users in model part.
example: you must making custom_model.php in models folder.
<?php
class custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_users($id)
{
$this->db->select('*');
$this->db->join('tenant','tenant.id = sites.tenant_id');
$this->db->where('tenant.id',$id);
$this->db->from('sites');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
?>
next step: you are remake custom_controller.
public function get_users()
{
$id = $this->session->userdata('id');
$this->load->model('custom_model');
$result=$this->cutom_model->get_users($id);
$sitedata = json_decode(json_encode($result));
$this->session->set_userdata($sitedata);
}
and next step:
class User extends Customer_Controller
{
public function__construct()
{
parent::__construct();
}
public function index()
{
//
}
public function get_users(){
parent::get_users();
}
so I believe I have it right (im new to active records).
the problem I am having I keep getting error
Message: Call to a member function getbank() on a non-object
My code is simple
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
if($query = $this->profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
model
class Profile_model extends CI_Model {
function getbank()
{
$query = $this->db->get('bank');
return $query->results();
}
Its so simple, I cant see a error, thank you.
Try this
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('profile_model'); # Added
$query = $this->profile_model->getbank(); # Changed
if(!empty($query)) # Changed
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
Model
class Profile_model extends CI_Model {
public function getbank() # Changed
{
$query = $this->db->get('bank');
return $query->result(); # Changed
}
}
Update
Simple answer, thanks to all those who tried helping. I had the model file name with a small letter not a capital.
Eg profile_model.php
should be Profile_model.php
However, this solution is elegant and simple, so it improved my code so il accept this one.
It may be because you didn't load your model?
Try changing your controller to:
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('Profile_model');
if($query = $this->Profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}