I want to get my session variables in my model. Im not using Auth component. Is there any alternate way ?
Thanks
Binoy
You would need to use the Session helper to do,
$this->Session->write('key','value');
But as the comment states, you'll be wanting to set a variable in your model and then use the session to write the same value into that variable in the model, rather than accessing the session actually in the model.
Class MyModel Extends AppModel{
var $username;
var $password;
}
Then in your controller you could use something along the lines of,
$this->MyModel->username = $this->Session->read('User.id');
$this->MyModel->password = $this->Session->read('User.password');
Controller File : use Session helper
class AppController extends Controller {
var $helpers = array('Session');
function index()
{
$this->Session->write('answer', '10'); //$this->Session->write('key','value');
}
....
}
Now you can access this varibale in your model :
class AppModel extends Model {
function validateanswer(){
CakeSession::read('answer');/*Here u can get value of varibale answer which you set through controller*/
}
}
Related
How do i access another model within a model in cakephp4.2? The docs on this issue isnt clear to me and i can then run a query on this ? TableRegistry is deprecated now.
error Unknown method "getTableLocator" called on App\Model\Table\LessonsTable
//none of these no longer work
in model {
use Cake\ORM\Locator\LocatorAwareTrait;
class LessonsTable extends Table
{
..
private function getlessonRevenue(){
//$clients = $this->getTableLocator()->get('Clients');
// $cleints = TableRegistry::get('Clients');
// $this->Table = TableRegistry::get('Clients');
$clients = $this->getTableLocator()->get('Clients');
https://api.cakephp.org/4.0/class-Cake.ORM.TableRegistry.html
Try:
<?php
use Cake\ORM\Locator\LocatorAwareTrait; //<------------ add here
class ArchivesTable extends Table
{
use LocatorAwareTrait; // <--------------------------- and add here
public function myMethod()
{
$clients = $this->getTableLocator()->get('Clients');
}
and read https://book.cakephp.org/4/en/orm/table-objects.html#using-the-tablelocator
and learn how to use php trait https://www.phptutorial.net/php-tutorial/php-traits/
I am trying to access the find() on another model in a controller. I am getting a
Call to a member function find() on a non-object error
PollsController.php (controller):
class PollsController extends AppController{
public function add(){
$this->set('appNames', $this->App->find('list'));
}
}
Poll.php (model):
class Poll extends AppModel{
public $belongsTo = array ('App');
}
App.php (model):
class App extends AppModel{
public $hasMany = array('Poll');
}
Note: I have also tried $this->Poll->App->find('list') and still get the same error.
App is reserved keyword. When I change the class to SomethingController.php and Something.php, everything works again.
If you want access another model functions you need load model. There is few ways to do it.
This one is only useful for controllers
$this->loadModel('App');
$this->App->find('first');
This one is good for models and controllers
Read more: CakeAPI: Class ClassRegistry
$appModel = ClassRegistry::init('App');
$appModel->find('first');
This one is good for controllers
Read more: CakeAPI: $uses variable in controller
//in Post Controller
public $use=array('Post','App');
Please load modal in your controller
public $uses = array("Poll");
Im trying to load one appmodel to many controllers. For example i have appmodel called Item and i want to get some items in HomeController and ContactController. When i execute this code:
class HomeController extends AppController {
public function index() {
$items = $this->Item->find('all');
$this->set('items', $items);
}
}
I got this error:
Call to a member function find() on a non-object
How can i get thing from database in many views in CakePHP?
you need to declare which model you want to use in your controller.
class HomesController extends AppController {
var $uses = array('Item', 'AnotherModel');
...
}
var $uses=array('Item');
If using this model in multiple controllers, put this in app controller.
Currently i am using something like this:
//at bootstrap.php file
Configure::write('from', 'mymail#mydomain.com')
//at controllers or models files
$var = Configure::read('from')
The thing is, i would like to manage that variable through the database to be able to modify it in a simpler way.
I was thinking about doing it with AppModel but then it would only be accessible for Models and not controllers.
What should I do in this case?
Thanks.
You can create a separate model / plugin which will be mapped to a configuration table in your database. Then load it through $uses statement for controllers and App::import() for models.
class SystemSetting extends AppModel {
/**
* Return a list of all settings
*
* #access public
* #return array
*/
public function getSettings() {
return $this->find('all');
}
}
Then, in your controller:
class SomeController extends AppController {
var $uses = array('SystemSetting');
public function displaySettings() {
$settings = $this->SystemSetting->getSettings();
// .. your code
}
}
In model:
App::import('Model', 'SystemSettings.SystemSetting');
$settings = new SystemSetting();
$mySettings = $settings->getSettings();
This works just fine. Of course, you might also load settings in both AppController and AppModel to follow the DRY rule.
create the getSettings in your AppModel
in AppController you can write this method:
public function getSettings() {
return $this->{$this->modelClass}->getSettings();
}
this way the getSettings() method is available in any model and any controller
any model call:
$mysettings = $this->getSettings();
any controller call:
$mysettings = $this->MODELNAME->getSettings();
I need to know in a helper in a CakePHP application if the device is mobile, I would love to use $this->RequestHandler->isMobile(), but the request handler component is not available in helpers. Any ideas?
Thanks!
You can import the class and use it anywhere in the framework like so:
App::import('Component', 'RequestHandler'); // import class
$requestHandler = new RequestHandlerComponent(); // instantiate class
$isMobile = $requestHandler->isMobile(); // call method
var_dump($isMobile); // output: bool(true) or bool(false)
(Tested from helper and gives correct results for Firefox and iPhone)
Also, any options you set in the Controller::helpers property will be passed to the helper:
class AppController extends Controller {
public $components = array(/*...*/, 'RequestHandler');
public $helpers = array(/*...*/, 'MyHelper');
public function beforeFilter() {
$this->helpers['MyHelper']['mobile'] = $this->RequestHandler->isMobile();
}
}
You can catch the options array in your helper's constructor:
class MyHelper extends AppHelper {
protected $_defaultOptions = array('mobile' => false);
public function __construct($options) {
$this->options = array_merge($this->_defaultOptions, $options);
}
}
The accepted answer suggests using a component inside a helper which should be avoided as components are for use solely in controllers and will result in errors as mentioned by Anupal.
The simple solution is to use the CakeRequest class that RequestHandlerComponent uses. So in your helper you can do:-
App::uses('CakeRequest', 'Utility');
$isMobile = (new CakeRequest())->is('mobile');