One model to many controllers - CakePHP - cakephp

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.

Related

How to get variable from AppController beforeFilter in Other Controllers action in cakePHP 2.0

I have queried on the User table inside the AppController as below
<?php
class AppController extends Controller {
public function beforeFilter() {
function beforeFilter() {
parent::beforeFilter();
if ($this->Session->read('Auth.User.id')) {
$userLoginInfo = $this->User->findByUserId($this->Session->read('Auth.User.id'));
$this->set('userLoginInfo', !empty($userLoginInfo) ? $userLoginInfo : NULL);
}
}
}
}
?>
The $userLoginInfo is available in all ctp files, but I want to access it in all other controller actions as well.
now you put this code in AppController
function beforeFilter(){
$this->set(‘accesstest’ , ‘abc’);
}
And We have to use it in Other controller file say anotherController.php
then we will use $this->viewVars.
here we will be used
$test = $this->viewVars[‘accesstest’];
$this->set('test',$test);
Your own answer is applicable if wanting to set a variable for all Views, but this was not what you were asking in your question.
If you are extending AppController correctly then you can create a property of the AppController class that would then be accessible from any controller that extends it:-
class AppController extends Controller {
public $accesstest = 'abc';
}
Then in any controller that extends AppController you can use:-
$test = $this->accesstest;
echo $test; // 'abc'
However, if you want to share a variable that you want accessible from all controllers that can be changed and you want the change remembering then use the Session:-
$this->Session->write('accesstest', 'abc');
$test = $this->Session->read('accesstest');
$userLoginInfo = $this->viewVars['userLoginInfo'];

Find() on a non-object?

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");

Paginate component fetches all data not paged data

I have a model and i want to paginate its data. all i do is, creating a model, something like this,
class MyModel extends AppModel {}
and include Paginator component in my controller and use it, something like this,
class MyControllers extends AppController
{
public $components = array('Paginator');
public function index()
{
$this->paginate['MyModel'] = array('limit'=>5);
print_r($this->paginate('MyModel'));
}
}
And I have a table with my_controllers name and it has 10 records. if everything was fine, the paginator component must give me 5 records for page one and 5 records for page 2. but it gives me 10 records without any paging.
What is the problem? :(
Thank you
Try this in your Controller:
public function index() {
$this->paginate = array('limit'=>5);
$this->set('yourVariableNameHere', $this->paginate());
}

CakePHP: Connection between model and controller failed

I've got a problem overhere with my newest CakePHP application.
I've made a model and controller for Categories.
Model: Categorie.php
class Categorie extends AppModel{
public $name = 'Categorie';
}
Controller: CategoriesController.php
class CategoriesController extends AppController {
public function index(){
$this->set('list', $this->Categorie->find('all'));
}
}
But when I visit the page I get the error:
Fatal error: Call to a member function find() on a non-object
When I add
$this->loadModel('Categorie');
to the index-function in my controller it will work.
So I guess the connection between the model and controller is not working but I'm not sure and I don't know how to fix this.
Please help me out.
thnx
CakePHP uses the controller names singular variant for matching the model, and the singular of categories is category, not categorie.
As #ndm said, you should use CakePHP conventions.
Your table should be called categories, then your model should look like this:
class Category extends AppModel{
}
And then you can include it with:
$this->loadModel('Category');
On CategoriesController you can use it like this:
class CategoriesController extends AppController {
public function index(){
$this->set('list', $this->Category->find('all'));
}
}
If you want to name your table otherwise, you can make use of $useTable variable in controller to specify the name of the table the model will work with.
You could also make use of the controller's $uses property:
public $uses = array('Categorie');

Cakephp getting session variable in model without using Auth component

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*/
}
}

Resources