How obtain directly the model from a query? -> CodeIgniter - php

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();

Related

Codeigniter Model returning empty data

I have a problem with my model. It's returning empty data.
Here is the controller.
public function getReport() {
$data = array();
$id = $this->input->post('id');
$this->auth->setId($id);
$data['instant_main']=$this->auth->getReportData();
$this->load->view('auth/report-item', $data);
}
and this is my Model
class Auth_model extends CI_Model {
// Declaration of a variables
private $_id;
public function setId($id) {
$this->_id = $id;
}
function getReportData()
{
$this->db->select('*');
$this->db->from('inbound_main');
$this->db->join('inbound_item', 'inbound_main.id = inbound_item.report_id');
$this->db->where('inbound_item.report_id', $this->_id);
$query = $this->db->get();
return $query->result();
}
}
If I have removed the WHERE condition from the query, everything was fine.
Your $this_id is empty..
You have folow the code like this
Controller
$data['instant_main']=$this->auth->getReportData($id);
Model
function getReportData($id)
{
$this->db->select('*');
$this->db->from('inbound_main');
$this->db->join('inbound_item', 'inbound_main.id = inbound_item.report_id');
$this->db->where('inbound_item.report_id', $id);
$query = $this->db->get();
return $query->result();
}

How to call model in controller on constructor because I need write only one time

I create a model for fetching data then write code
model..
function get_courses(){
$this->db->from(TABLE_COURSE);
$this->db->where('name !=', '');
$query = $this->db->get();
$result = '';
if($query){
if($query->num_rows() > 0)
$result = $query->result();
}
return $result;
}
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Mastermodel','',TRUE);
}
public function index()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $data);
}
public function quiz_of_day()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_of_day";
$this->load->view('layout/dashboard/layout', $data);
}
public function quiz_edit()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_edit";
$this->load->view('layout/dashboard/layout', $data);
}
}
How to call model in controller on constructor because I need write only one time?
To achieve what you want you can do the following...
You need to make the $data array a property of the class
Reference the new $this->data throughout the class
You need to move your call to the model in the constructor.
So what you get is this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
protected $data = array(); // Old school definition of an array (instead of [])for safety
public function __construct() {
parent::__construct();
$this->load->model('Mastermodel', '', TRUE);
$this->data['courses'] = $this->Mastermodel->get_courses();
}
public function index() {
$this->data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $this->data);
}
public function quiz_of_day() {
$this->data['view_file'] = "content/quiz/quiz_of_day";
$this->load->view('layout/dashboard/layout', $this->data);
}
public function quiz_edit() {
$this->data['view_file'] = "content/quiz/quiz_edit";
$this->load->view('layout/dashboard/layout', $this->data);
}
}
Tim's method would work just find, but I tend to do it more this way.
class Quiz extends CI_Controller {
public $courses; // can also be set to private
public function __construct(){
parent::__construct();
$this->load->model('Mastermodel','',TRUE);
$this->courses = $this->Mastermodel->get_courses();
}
public function index()
{
$data['courses'] = $this->courses;
$data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $data);
}

How to pass an array to the model from the controller in codeIgniter

I am trying to pass an array to the model from the controller in codeIgnitor I know this question have some answers but I tried all of them and no solution works with me. So this my code:
Controller:
<?php
if(! defined('BASEPATH')) exit('No direct script access allowed');
class SearchController extends CI_Controller{
var $controller = "user";
public function index(){
$this->home();
}
public function home(){
$this->load->view("search_view");
}
public function search(){
if(isset($_POST['submit'])){
$data['type']=$this->input->post('type');
$data['value']=$this->input->post('value');
$this->load->model("SearchModel");
$this->load->SearchModel->getCadre($data);
}
}
}
?>
Model:
<?php
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
$this->getCadre();
}
function getCadre(){
$query = $this->db->get('cadres');
$query = $this->db->where($type,$value);//the argument witch i want to take from array
if($query->num_rows()>0){
$values = result();
return $query->result();
}
else{
return NULL;
}
}
}
?>
Try below code in your model::
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
}
function getCadre($data = array()){
$this->db->where($data);//the argument witch i want to take from array
$query = $this->db->get('cadres');
if($query->num_rows()>0){
// $values = result();
return $query->result();
}
else{
return NULL;
}
}
}
OR you can pass individual data as below and return result...Also try as below
Controller:
if(! defined('BASEPATH')) exit('No direct script access allowed');
class SearchController extends CI_Controller{
var $controller = "user";
public function index(){
$this->home();
}
public function home(){
$this->load->view("search_view");
}
public function search(){
if(isset($_POST['submit'])){
$type=$this->input->post('type');
$value=$this->input->post('value');
$this->load->model("searchmodel");
$data = $this->searchmodel->getCadre($type,$value);
if($data==NULL){
echo 'No records found!!';
}
else
{
//send data to view
$this->load->view('view_name',$data);
}
}
}
}
Model
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
$this->load->database();
}
function getCadre($type,$value){
$this->db->where('type',$type);//the argument witch i want to take from array
$this->db->where('value',$value);
$query = $this->db->get('cadres');
if($query->num_rows()>0){
// $values = result();
return $query->result();
}
else{
return NULL;
}
}
}
?>

How to call Base Controllers Method in Codeigniter?

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();
}

Codeigniter: Simple echo of data from DB

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);
}
}
?>

Categories