简体   繁体   中英

Get logged in user by searching cakephp 2.x session table

I wanted to use the session table to get a list of all logged in users. To do this I attempted to use the following code to query the cake_sessions table:

$this->loadModel('CakeSession');
$sessions = $this->CakeSession->find('all', array('fields' => 'data'));
// next I process session data to find logged in users

But running this code I get the following error:

Call to undefined method CakeSession::find()

If I use the following code to directly access the table it works just fine:

$db = ConnectionManager::getDataSource("default");
$sessions = $db->fetchAll("SELECT data from cake_sessions");
// next I process session data to find logged in users

Is accessing the session table restricted? My current solution works just fine but I am confused why I cant use the the CakeSession->find().

Because CakeSession is not a model class. See http://api.cakephp.org/2.8/class-CakeSession.html

The class you refer to is a datasource as the "namespace" tells you as well: Cake\\Model\\Datasource . In Cake2 session access was implemented as datasource. "Model" is a layer not just a single type of files.

Create a proper session model class in your apps Model folder. See http://book.cakephp.org/2.0/en/models.html

You might have to name it different to avoid conflicts because Cake2 is not using real php namespaces unlike Cake3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM