difference between $this->data and $this->request->data? - cakephp-2.1

In cakephp 2.1 whats the difference between $this->data and $this->request->data?

$this->data was used till cakephp 1.3
$this->request->data has replaced the $this->data array from cakephp 2.x onwards.
For backward compatibility $this->data is also still supported.
$this->request represents CakeRequest object and is accessible in Controller, Views and Helpers.
For more info : http://book.cakephp.org/2.0/en/controllers/request-response.html

Related

CodeIgniter: Undefined property when calling the model

I use CodeIgniter 3 with HMVC.
Does anyone know why do I get the following error
Severity: Notice
Message: Undefined property: Login::$login_model
On the second line below. I clearly call the model, but for some odd reason it is not linked properly
$this->load->model('auth/login_model');
$response = $this->login_model->attempt($username, sha1($password));
Then the model is pretty basic :
<?php
class Login_model extends CI_Model
{
public function attempt($user, $pass)
{
...
Now if I use an object it works, but I have the same issue in many places, including the place where I have
$this->db->select("....
where is crashing as there is no "db". What is the new solution for CodeIgniter 3? I've seen older posts, but none seem to help me out
Thanks!
just try this code put in controller:
public function __construct() {
parent::__construct();
$this->load->model('Login_model'); // load model
}
Problem is resolved, the issues were caused by the fact that my controller extended CI_Controller instead of MX_Controller. So changing
class Login extends CI_Controller
to
class Login extends MX_Controller
resolved the issue.
It took me a while to figure it out by debugging the thirdparty/MX/Loader.php, but once I saw it was looking for MX_Controller type I did the change and it worked perfectly.
This issue is one in many related to migration from CI 2 to CI 3 and also using the HMVC from Wiredesignz. Another big one is uppercase of file names and uppercase on the calls, so strictly referring to this issue I had to uppercase the calls in my controller (changed "login" to "Login"):
$this->load->model('auth/Login_model');
$response = $this->Login_model->attempt($username, sha1($password));
I did the above change already, so this was no longer a blocker, still I wanted to put it here just in case someone hits the exact same issue
Thanks all for your help

Difference between pluck() and lists() in laravel?

This is so confusing to me. I don't see any difference between these two methods. If I var_dump() the object returned by these methods, they are exactly the same but the book by Dayle Rees says that pluck() returns a single value from the given column (the first one) while the lists() method returns all the values from the given column. I can't even figure out why two different methods exist to do the same thing.
Example
Route::get('getalbum', function() {
$data = \App\Album::pluck('artist');
var_dump($data); // a lot of text, let's call it 'object'
$data = \App\Album::lists('artist');
var_dump($data); // exact , exact, exact same 'object'
});
From the docs, Deprecations section
The following features are deprecated in 5.2 and will be removed in the 5.3 release in June 2016
The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature remains the same.
So yes they are same. It's just there for backward compatibility.
Source code

Joomla 2.5 getUserStateFromRequest load error

I was following the example to implement custom filters in Joomla 2.5 admin component.
But I am getting error at models populateState method:
Call to undefined method
somecompModelsomecomp::getUserStateFromRequest().
$app = JFactory::getApplication('administrator');
// Load the filter state.
$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
Error disappears if I call getUserStateFromRequest using $app:
$app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
So whats the problem? In default Joomla components I've seen that it use the same approach and it works. Maybe I miss something in my model class?
Any ideas?
This is happened because $app is an object of your application class. As you defined it in your code.
$app = JFactory::getApplication('administrator');
and getUserStateFromRequest method is defind in that Application class.so you have to use it like this if you want to access this method.
$app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
And for your information $this variable is your local object.

CodeIgniter 2 and usage of $this->

I'm using CodeIgniter 2 and have installed Ion Auth and also the News tutorial that comes with CodeIgniter.
In the News Controller, the element for the page title is written like this...
$data['title'] = 'Page Title';
However, in the Ion Auth Controller, the element for the page title is written like this...
$this->data['title'] = 'Page Title';
They both seem to work equally well, so can anyone explain the difference(s)? Maybe Ion Auth was written for an older version of CodeIgniter? Is there any practical reason why I'd want to use one over the other? Please link to sources as needed.
I guess it's the author's preference. He likes to use a class property to store the view's data. It allows him to share it across methods. If you look at the author's other projects (Source 1, 2, 3), you can see two examples (source 1 & 2 goes together).
On a side note, for your project, this could allow you to extend the Auth controller with more view data.
class MY_Auth extends Auth {
function __construct()
{
parent::__construct();
}
function index()
{
$this->data['foo'] = 'bar';
parent::index();
}
}
That would allow you to use the $foo variable to your authentication view. (/auth/index in this case.)
In my own projects, I like to use a protected property for my view's data. It does give you much more freedom than a local variable. You don't need to pass the view's data as an argument all the time and you can easily extend your controllers afterward.
Hope this helps!
if you are going to use this $this->data it means you can access $this->data through out the class methods. On the other hand if you are using $data it is only available for the current scope or method and if you need data some where else then you will have to pass it as parameters to the other methods.
Adding $this on the data variable, makes it to be accessible through the class.
I believe the $data or $this->data is only used for "View". It will be passed from the "Controller" to the "View", so we can access that variable through the "View".
So, there will be no differences on the "View" side.

Reading a session variable inside a behavior in cakephp 2

I have a behavior which enables segregation of user data based on the user id stored in the session. In CakePHP 1.3 you could do this:
App::import('Component', 'Session');
$session = new SessionComponent();
$session->read('Auth.User.id');
But in CakePHP 2, you can't instantiate a component like that in a behavior because the Component __construct requires the Controller's ComponentCollection as a parameter.
Is it possible to access a session variable inside a behavior in CakePHP 2? What's the best way to do it?
If you look at the SessionComponent code, you will see that it is only a wrapper for the CakeSession class.
So you can do the following:
App::uses('CakeSession', 'Model/Datasource');
$user_id = CakeSession::read('Auth.User.id');
In CakePHP 2.0 you can also simply call the Session-methods via the static CakeSession::method() without having to load anything... ;-)

Resources