CodeIgniter session object cannot be found - php

I'm working on a model that tracks user data and stores it in a session, where appropriate. Here's the basic structure of it:
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->library('session');
if (!is_null($this->session->userdata('user'))) {
$arrData = $this->session->userdata('user');
$this->_netID = $arrData['_netID'];
$this->_fName = $arrData['_fName'];
$this->_eventMgr = $arrData['_eventMgr'];
$this->_accessMgr = $arrData['_accessMgr'];
$this->_accessAcnt = $arrData['_accessAcnt'];
$this->_accessEvnt = $arrData['_accessEvnt'];
$this->_accessCMS = $arrData['_accessCMS'];
$this->_accessReg = $arrData['_accessReg'];
$this->_accessRep = $arrData['_accessRep'];
$this->_accessPay = $arrData['_accessPay'];
$this->_okEvents = $arrData['_okEvents'];
}
}
public function __get($name) {
switch($name) {
default:
if (function_exists('parent::__get')) {
return parent::__get($name);
} else {
return $this->$name;
}
}
}
public function __set($name,$val) {
switch($name) {
default:
if (function_exists('parent::__set')) {
return parent::__set($name,$val);
} else {
$this->$name = $val; }
}
}
The issue I'm having is right in the constructor. Whenever it hits the seventh line (checking if the user key is null) it errors out, saying:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: SessionUser::$session
Filename: models/sessionuser.php
Line Number: 83
Fatal error: Call to a member function userdata() on a non-object in /usr/cwis/data/www-data/melioraweekenddev/system/application/models/sessionuser.php on line 38
Any ideas on why?

My guess is you're trying to load a library from within a model which is not directly possible.
Try instead:
public function __construct() {
parent::__construct();
$CI =& get_instance(); //Loads the codeigniter base instance (The object your controller is extended from. & for php4 compatibility
$CI->load->database();
$CI->load->library('session');
if (!is_null($CI->session->userdata('user'))) {
...

Related

Getting error messages when trying to display images from a database while using codeigniter

I am trying do display information from my database using codeigniter, but I keep reeving an error message. The error messages is Severity: Warning Message: Invalid argument supplied for foreach().
This is my model:
class stuffModel extends CI_Model {
private $stuff_table = 'Stuff';
function __construct()
{
parent::__construct();
$this->load->database();
}
public function getItem()
{
$un = array();
$res = $this->db->get($this->stuff_table);
$results = $res->result();
return $results;
}
}
And this is my Controller:
public function displayStuff() {
$STUFF= $this->stuffModel->getItem();
$this->load->view('myView',array('stuff' => $STUFF));
}
And this is my View:
<?php
foreach ($stuff as $s) {
echo"<h1>".$s['Items']."</h1>";
}?>
And the error message is Severity: Notice Message: Undefined variable: stuff and Severity: Warning Message: Invalid argument supplied for foreach()
Nothing is jumping out at me and it looks like it should work. Here's a little trouble shooting idea.
Change the controller as follows
public function displayStuff()
{
$data['stuff'] = $this->stuffModel->getItem();
var_dump($data); //share this output here
$this->load->view('myView', $data);
}
You did not load the model into controller that's why you can't call the model from the controller .
first load the model into controller
$this-load->model('modelName');
than call
$this->modelName->methodName();
Changes in Controller
Make sure that the object of model class is declared in the constructor as given below
$this->load->model('stuffModel', 'stuffModel', TRUE);
then in the displayStuff function,
public function displayStuff() {
$data['stuff']= $this->stuffModel->getItem();
$this->load->view('myView',$data);
}
Change in the model
public function getItem()
{
$res = $this->db->get($this->stuff_table);
if ($res->num_rows() > 0) {
return $res->result_array();
} else {
return array();
}
}
Finally in the view
You can display the values stored in the array by iterating as follows
foreach($stuff as $stf){
echo $stf['field_name'];
}

Store model function return to controller function

I have a model which returns a username of the person that has logged into the website to a controller. I am trying to save the username into a variable which i can user to then insert back into another table, however i am having no luck saving the data. Below is my model and controller classes.
Model:
function is_loggedin()
{
$session_id = $this->session->userdata('session_id');
$res = $this->db->get_where('logins',array('session_id' => $session_id));
if ($res->num_rows() == 1) {
$row = $res->row_array();
return $row['name'];
}
else {
return false;
}
}
Part of my Controller:
public function index()
{
$loggedin = $this->authlib->is_loggedin();
if ($loggedin === false)
$this->load->view('login_view',array('errmsg' => ''));
else
{
$this->load->view('postquestion_view',array('username' => $loggedin));
$user = $loggedin['username'];
}
}
public function askquestion()
{
$qtitle = $this->input->post('title');
$qdetails = $this->input->post('details');
$qtags = $this->input->post('tags');
$qcategory = $this->input->post('category');
$quser = $user;
Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
Filename: controllers/postq.php
Line Number: 47
Here the error message is very clear. The variable $user in the last line of the function -action- askquestion() snippet is not defined. Basically, you have to read more about variables scope.
In your current situation, the code of index action should be in constructor and the variable user should be an object property. i.e it should defined globally in your controller's class and then takes its value from the constructor something like the following general demo:
<?php
class Blog extends CI_Controller {
public $user = false;
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function askquestion()
{
$qtitle = $this->input->post('title');
$qdetails = $this->input->post('details');
$qtags = $this->input->post('tags');
$qcategory = $this->input->post('category');
$quser = $this->user; //NOTICE THIS LINE
}
?>

Codeigniter with datamapper save error

So I was trying to make a CRUD for my app. I have setup a controller, like this:
class Clients extends CI_Controller {
public function __construct() {
parent::__construct();
session_start();
$this->c = new Client();
$this->u = new User();
}
function index() {
$data['current_view'] = 'client_view';
$data['header'] = 'Меню клиентов';
$data['clients'] = $this->getClients();
$this->load->view('main_view',$data);
}
function getClients() {
$this->u->where('username',$_SESSION['username'])->get();
$c = $this->c->where('user_id',$this->u->id)->get();
return $c;
}
function delClient() {
$this->c->where('client_id', $this->uri->segment(3))->delete();
$this->c->skip_validation()->save();
$this->index();
}
}
However, when I'm trying to perform a delete on a client, i get a db error:
You must use the "set" method to update an entry.
Filename: C:\OpenServer\domains\localhost\system\database\DB_active_rec.php
Line Number: 1174
What might be the cause of this? I found a similar question here, but I don't think that's the case.
EDIT: Client model:
class Client extends DataMapper {
public $has_one = array('user');
public function __construct($id = NULL) {
parent::__construct($id);
}
}
you have not passed the uri segment to the function delClient() and this is your model right...
function delClient($id) {
//$id is the value which you want to delete
$this->c->where('client_id', $id)->delete();
$this->c->skip_validation()->save();
$this->index();
}

Simple ORM Structure Codeigniter

This is my code:
<?php
class VortexORM {
private static $orm = null;
public function __get($name) {
return $this->$name;
}
public function __set($name, $value) {
$this->$name = $value;
}
public static function getInstance() {
if (VortexORM::$orm == null)
VortexORM::$orm = new VortexORM();
return VortexORM::$orm;
}
public static function set($name, $value) {
$orm = VortexORM::getInstance();
//echo "Setting [ <b>{$name}</b> :: <i>{$value}</i>]";
$orm->$name = $value;
}
public static function get($name) {
$orm = VortexORM::getInstance();
// echo "Getting [ <b>{$name}</b> :: <i>{$orm->$name}</i>]";
return $orm->$name;
}
}
To get data I use:
var_dump(VortexORM::get('admin_links'));
var_dump(VortexORM::get('admin'));
To set data I use:
VortexORM::set('admin_links',array(....));
However, I get the following warnings:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: VortexORM::$admin_links
Filename: Vortex/VortexORM.php
Line Number: 8
NULL
A PHP Error was encountered
Severity: Notice
Message: Undefined property: VortexORM::$admin
Filename: Vortex/VortexORM.php
Line Number: 8
Why am I getting these warnings?
I want to be able to access it like this in CodeIgniter as a static function:
$this->vortexorm->admin_links = array(....);
OK, I just tested this code:
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class VortexORM {
private static $orm = null;
public function __get($name) {
return $this->$name;
}
public function __set($name, $value) {
$this->$name = $value;
}
public static function getInstance() {
if (VortexORM::$orm == null)
VortexORM::$orm = new VortexORM();
return VortexORM::$orm;
}
public static function set($name, $value) {
$orm = VortexORM::getInstance();
//echo "Setting [ <b>{$name}</b> :: <i>{$value}</i>]";
$orm->$name = $value;
}
public static function get($name) {
$orm = VortexORM::getInstance();
// echo "Getting [ <b>{$name}</b> :: <i>{$orm->$name}</i>]";
return $orm->$name;
}
}
VortexORM::set('admin_links',"test");
var_dump(VortexORM::get('admin_links'));
It just worked fine for me. giving the following output:
string(4) "test"
So, I guess, you were might just trying to retrieve a property before its set? Or please share more details. Also, why are you trying to create new ORM, where we can use doctrine with codeigniter easily?

PHP Fatal error: Call to a member function getScore() on a non-object in

this is my first question
I Have following class
class ProDetection {
public function ProDetection ( ) { }
public function detect($word) {
............
}
public function getScore() {
return $score;
}
}
class SaveDetection {
public function SaveDetection($words) {
$proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
}
in other PHP file, i'm try to calling SaveDetection to getScore();
$konten = "xxxx xxxx xxx";
$save = new SaveDetection($konten);
print_r( $save->getScore() );
But i got an error message
Notice: Undefined property: SaveDetection::$proDetection in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22
Fatal error: Call to a member function getScore() on a non-object in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22
Please need your help
You never declare the the $proDetection member variable. Basically in your SaveDetection constructor you are declaring $proDetection as a local variable
class SaveDetection {
public function SaveDetection($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
private $proDetection;
}
EDIT:
PS. You should really use PHP's __construct() syntax instead of the old style of constructors. See here.
class SaveDetection {
public function __construct($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
Try this. Declare your variables as private (Coz if your code grows, it is hard to find what all the object or variables are used in the class.)
class SaveDetection {
private $proDetection = null;
public function __construct($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
private $proDetection;
}

Categories