Having problems accessing session variables through different actions in ZF.
Here are the contents of the bootstrapper:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initSession()
{
Zend_Session::start();
$sessionUser = new Zend_Session_Namespace('sessionUser');
}
}
I assign a variable to $sessionUser in the IndexController:
$sessionUser->userId = $res->userId;
$sessionUser->organisationId = $res->organisation_organisationId;
$sessionUser->forename = $res->forename;
And attempt to access the variable in the Administrator controller:
$sessionUser->organisationId
Yet I receive the error:
Notice: Undefined variable: sessionUser in /usr/local/zend/apache2/htdocs/SA1/application/controllers/AdministratorController.php on line 17 Notice: Trying to get property of non-object in /usr/local/zend/apache2/htdocs/SA1/application/controllers/AdministratorController.php on line 17
And ideas what could be causing this?
Many thanks
To get the session variable back in your controller you also need to do:
$sessionUser = new Zend_Session_Namespace('sessionUser');
Well, the error you are getting is obvious. The $sessionUser is not defined.
You must initialize such variable before assinging values to it. Put this in your controller:
$sessionUser = new Zend_Session_Namespace('sessionUser');
Related
I want to get last inserted id, and then assign it to a global variable to use it later.
I'm use callback_after_insert as the following:
$crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));
function _callback_get_lastInsertID($post_array,$primary_key) {
$this->lastInsertedId = $primary_key;
}
Then, when use the $this->lastInsertedId to get its value, but I do not find any value stored.
function featured_ad_offer() {
echo $this->lastInsertedId;
#$data['content'] .= $this->load->view('featured_ad_offer', '', true);
$this->load->view('index', $data);
}
There is an error message say Undefined property: Content::$lastInsertedId
Now, how to do that ?
Please try declaring your variable in class (assuming both functions are in the same class) right after "declaring" class itself i.e.
class Blog extends CI_Controller {
private $lastInsertedId = NULL;
public function fcn1() {$this->lastInsertedId = 3.14;}
public function fcn2() {var_dump($this->lastIndertedId);}
}
If you really mean using it as global variable you need to declare it in CI_Controller, or better yet create file in /core/Public_Controller that extends CI_Controller and let all your controllers be extended not by CI_Controller but Public_Controller therefore you can easily declare variables that are "global".
For more information on extending use this tutorial by Phil.
If you have troubles here is a user guide link.
There is another option (not recommended!) config class,
$this->config->set_item('item_name', 'item_value'); //setting a config value
$this->config->item('item name'); //accesing config value
maybe you can use this to get the last inserted id
$this->db->insert_id();
store it in the session
$this->load->library('session');
$this>session->set_userdata('last_inserted_id');
and use this session variable in other places
$last_inserted_id = $this->session->userdata('last_inserted_id');
wish this help :D
The solution that you need it can easily be resolved with Codeigniter sessions. So for example, what you can do is:
$crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));
function _callback_get_lastInsertID($post_array,$primary_key) {
$this->session->set_userdata('last_insert_id', $primary_key);
}
Make sure that you have session class loaded first. The best way to do it is to actually have the sessions library at the autoload.
Now to actually call the last_insert_id again, you can simply do it by calling:
echo $this->session->userdata('last_insert_id');
So in your example you can simple do:
function featured_ad_offer() {
echo $this->session->userdata('last_insert_id');
#$data['content'] .= $this->load->view('featured_ad_offer', '', true);
$this->load->view('index', $data);
}
I am new to php so please excuse my lack of knowledge. I am using eclipse and have a project with 3 files inside the project. I am creating a find discount class which takes the class object to call a function from another class. The error:
Notice: Undefined variable: GetInfoClass line ..
Fatal error: Call to a member function getAge() on a non-object line ...
I tried to read about it but I cant seem to understand it. Please help. Thanks
formResponse:
include "GetInfo.php";
include "IfDiscount.php";
$IfDiscount= new IfDiscount();
echo $IfDiscount->findDiscount();
class IfDiscount:
class IfDiscount
{
public function findDiscount(){
$Age = $GetInfoClass->getAge();
echo $Age;}}
$GetInfoClass is not available to findDiscount() because it is out of scope. You should pass it to findDiscount() as a parameter to make it available to that method:
public function findDiscount($GetInfoClass){
$Age = $GetInfoClass->getAge();
return $Age;
}
echo $IfDiscount->findDiscount($GetInfoClass);
(You also want to return $Age, not echo it. You already explicitly echo it when you call that method.)
I'm trying to access the email address for a user that's signed in and I can do so successfully in all of my Controllers except for one.
Here is the error I'm getting
Notice (8): Undefined index: User [APP/View/Layouts/default.ctp, line 37]
And here is the corresponding line of code (remember, this works in all of my other controllers).
<center><font color="black"><td><?php echo $user['User']['email']; ?></td></text></center>
Here is the DemosController
<?php
// app/Controller/DemosController.php
class DemosController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add','logout');
$user = $this->Demo->read(null, $this->Auth->user('id')); //this throws the error
//$user = $this->Demo->User->read(null, $this->Auth->user('id')); //Fatal error: Call to a member function read() on a non-object in
$this->set('user', $user);
}
//display the knockout table
public function index($article = null) {
}
}
I do not need a table in the database for this Controller. It's simply to display a table for demo purposes. Can I just let it reference user?
class Demo extends AppModel {
public $name = 'User';
}
Why can I access this in all of my controllers except this one? Is the Model/table situation causing the error? If so, is there a way to disable the use of a table?
You have a couple problems going here:
First, when not using a table, you set a model's $useTable property to false.
Secondly, without a table, no database call will work (just plug in sample data to the table anyway, who would know if its a demo?)
It still bears mentioning that $this->Model->read() is for updating records. Changing the line to the following will allow the set call to function properly:
$this->Demo->find('first', array(
'conditions' => array(
'Demo.field' => $this->Auth->user('id')
)));
Getting properties of the logged-in user via AuthComponent
You don't have to pass information of the currently logged-in user to the view via a 'viewVar'.
To access properties of the currently logged-in user outside of the controller, use the 'static' interface of the AuthComponent
Inside your views or layouts, output the information like this;
<p>The current users email is: <?php echo AuthComponent::user('email') ?></p>
<p>The current users id is: <?php echo AuthComponent::user('id') ?></p>
<p>And all properties of the user are:</p>
<pre><?php print_r(AuthComponent::user());?></pre>
See: Accessing the logged in user
You can remove these lines from your beforeFilter();
$user = $this->Demo->read(null, $this->Auth->user('id')); //this throws the error
//$user = $this->Demo->User->read(null, $this->Auth->user('id')); //Fatal error: Call to a member function read() on a non-object in
$this->set('user', $user);
the class below is part of an mvc structure im building for my website. the class is there to produce a page if index.php is called without any request in the url. if that is good practice or not I do not know. Im new to php, and this is the first time i implement an mvc structure.
<?php
class Default_Model
{
private $defaultPage = array
(
'headline' => 'JOBBSÖKAREN',
'instruction' => 'logga in nedanför'
);
public function __construct()
{
}
public function getContent()
{
line 17 return $defaultPage;
}
}
?>
here is the error message:
Notice: Undefined variable: defaultPage in /home/mengus/dev/www/models/default.php on line 17
so what I dont get is why the array is undefined. I use similar code in other classes and it works just fine. is it a scope issue? I seem to have gone error blind from staring at this :)
thanks for help.
To access the fields you need to use $this and -> operator
return $this->defaultPage;
CakePHP Newbie :)
I am having trouble accessing another controller and passing that data to a view in one of my controllers:
In controllers/landings_controller.php:
var $uses = 'User';
function home() {
$userdata = $this->User->read();
$this->set(compact('userdata'));
}
In views/landings/home.ctp:
<?php
echo $this->userdata;
?>
When accessing /landings/home I get the following error:
Notice (8): Undefined property: View::$userdata [APP/views/landings/home.ctp, line 38]
I don't know what I am doing wrong. Any help?
Thanks!
$this->set('userdata', $userdata);
Compact returns a single array. $this->set expects two parameters.
http://book.cakephp.org/view/57/Controller-Methods
Correction:
set does in fact accept associative arrays (thanks Daniel Wright). Read below about using variables in views.
Also, variables are placed in scope -- not attached as members -- so you wouldn't do this in the view:
<?php echo $this->userdata ?>
but, rather:
<?php echo $userdata ?>
Assuming $userdata is a scalar, of course.
I think using compact is fine.You need know more about set().