Checking for CodeIgniter session_id to not be set - codeigniter

I'm trying to check if its set in my codeigniter application like:
elseif(!isset($this->session->userdata('session_id'))){ ## SESSION data is NOT set
// load a generic 'issue' occurred view.
}
but I keep getting this error, is !isset not valid in Codeigniter? How do I go about easily checking this?
Thanks

You must use the code like below:
$id = $this->session->userdata('session_id');
// your other code
elseif(!isset($id)){
// load a generic 'issue' occurred view.
}
The reason you can see here

You coud also do this ,
elseif($this->session->userdata('session_id') !== false)
{
// SESSION data is NOT set
// load a generic 'issue' occurred view.
}
Note: The function returns FALSE (boolean) if the item you are trying to access does not exist.

Related

Laravel Error while retrieving a Model with a custom connection

I have a Model Correo with a custom connection that changes dinamically.
The problem is that when I want to retrieve results from the database like this: Correo::on(session('conexion'))->get(), session('conexion') has the connection name, the following error appears:
Call to a member function newCollection() on null
I can get the results using this: DB::connection(session('conexion'))->table('correos')->get(), but I need the Model's methods and the previous one just returns a generic Collection.
Thanks!
You can use setConnection function
$correo = new Correo;
$correo->setConnection('yourConnectionName');
$data = $correo->find(1);
dd($data);
So based on the session ( if you don't have that many remote connections )
if (session('xyz')) {
$correo->setConnection('xyz');
} else {
$correo->setConnection('pqr');
}
`
Well, I solved it, when I created the model I wrote every property and then created every getter and setter, apparently it didn't like the new setConnection setter. I don't know why, but it stopped me from using it.

isset() on codeigniter objects

How can I achieve something that looks like:
if (isset($this->session->flashdata('user_profile'))) {}
without:
Fatal error: Call to a member function flashdata() on a non-object in ...
The issue is that it returns an error when that thing isn't set rather than continue as one might expect. If it is set, everything works out fine.
...
I have also tried:
$tmp = $this->session->flashdata('user_profile');
if ($tmp) {
but to no avail.
you don't need isset(); cause CI methods returns false or true
just do
if($this->session->flashdata()){ }
and your error is cause you are surely not loading the session library:
so do this:
$this->load->library('session');
if($this->session->flashdata()){ }
if you prefer (i preferr) you can autoload the session library changing this in your config/autoload.php file:
$autoload['libraries'] = array('session');
so now you don't have to load anytime the session library cause CI autoloads that for you
You can check the existence of $this->session first
if (isset($this->session) && isset($this->session->flashdata('user_profile'))) {
}
For proper validation you must do this method. Weather the values becomes NULL sometimes.
$sessusr=$this->session->userdata('username');
if(isset($sessusr) && trim($sessusr!=''))
{
//to do
}
Have you auto load the session library if not go to config/autoload.php open it and try to add session libarary it will look like this $autoload['libraries'] = array('session'); if you have auto loaded the library than the alternative approach is:
if(!empty($this->session->flashdata('user_profile')): echo $this->session->flashdata('user_profile'); endif;

How can I debug $Model after validation?

I want to see the content of validationErrors => array(???) of the $Model after a failed validation, but there is no "afterValidation()" method.
Does anyone know how can I see that or at least how would it look exactely?
Thank's!
On Controller, you can validate data before you trying save:
$this->ModelName->set($this->request->data);
if ($this->ModelName->validates()) {
// success
} else {
// failed
$errors = $this->ModelName->validationErrors;
}
Reference:
Validating Data from the Controller
Use $this->ModelName->invalidFields() after you have made the save/whatever you're doing:
For example:
debug($this->ModelName->invalidFields());
If you have a redirect at some point after that call, you might not see the data in your view. In this case, you can always do die(); either right after or wrapped around your call like so:
die(debug($this->ModelName->invalidFields());

Applying form errors manually

I have a situation where I'm editing a snippet of data within a larger context. The user submits this data to a specialized action for handling and redirects back to the parent page. Because it's a redirection, validation errors aren't getting automagically set, so I'm trying to work around that.
In the event of an error, I'm writing a validation_errors key to the session with a value of $model->validationErrors. In the form, though, I'd like to tell Cake to set each error so I can leverage my existing styles and not have to make a lot of changes to my $this->Form->input() methods.
Is something like this possible? Essentially, I'm looking to manually achieve the same result you'd get if a regular form was submitted and allowed to drop through with validation errors. I was hoping I could loop over each validation error and set the field error, but that's not making any change at all.
Thanks.
This can be achieved in the controller by
$this->Model->invalidate('fieldName', __('ErrorMessage', true));
If the values are available, you can also call
$this->Model->validates();
to validate all values with the validators defined in the model.
Save the data to the session and revalidate it.
function childAction() {
if(isset($this->data)) {
$this->Session->delete('invalid_data');
if($this->Test->save($this->data)) {
// ...
} else {
$this->Session->write('invalid_data', $this->data);
}
$this->redirect(array('action'=>'parentAction'));
}
}
function parentAction() {
if($this->Session->check('invalid_data')) {
// This will cause $this->Test->validationErrors to be populated
// Assuming your parent page has the form set up properly, the
// errors will be automagically filled. ie: $form->input('Test.field1')
$this->Test->set($this->Session->read('invalid_data'));
$this->Test->validates();
}
}
If you want to do the same with CakePHP 3, use the method "errors".

Error handling custom component Joomla

How to set and retrieve error inside component in model I have:
if (empty($coupon->coupon_id))
{
$this->setError( JText::_( "Invalid Coupon" ) );
return false;
}
How to retrieve this error inside controller? this->getError gives nothing :(
Thanks
Do you want to return the error to the screen?
If so, http://docs.joomla.org/Display_error_messages_and_notices
In your controller you need to invoke your model and use the $model->getError(); function instead of $this->getError();

Resources