Find() on a non-object? - cakephp

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

Related

Cakephp3.0 I am calling Postcategories controller into Appcontroller and it is error " Call to undefined method Cake\Core\App::import() "

My Code tries to fetch all Main categories of the posts into Appcontroller to show on the homepage:
namespace App\Controller;
use Cake\Core\App;
use Cake\Controller\Controller;
class AppController extends Controller
{
public $helpers = ['Html', 'Form', 'Session','Time','Tree'];
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->maincategories();
}
function maincategories(){
App::import('Controller','Postcategories');
$postcates = new PostcategoriesController;
$postcates = $postcategory->find('threaded');
}
}
Your maincategories() method is wrong. You need the model, not the controller to retrieve the data from. You need to use TableRegistry::get('Postcategories') to get the Postcategories model and then call the find on that:-
public function maincategories()
{
$Postcategories = TableRegistry::get('Postcategories');
$this->set('postcategories', $Postcategories->find('threaded'));
}
$this->set() is setting the categories as a view variable ($postcategories). You will need to make sure you include use Cake\ORM\TableRegistry; at the top of your AppController file.
Make sure you've fully read the docs on retrieving data.

One model to many controllers - 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.

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 call a function of another model error

I have a site develop in cakephp 2.x
I want into my controller call a function of another controller like this:
class ProductsController extends AppController {
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction(){
$this->loadModel('Unit');
$this->Unit->test();
}
}
The function test into UintController.php is this:
public function test(){
echo("test");
}
My model name are Product and Unit.
When I call the function test give me this error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'prova' at line 1
In the function now is empty but give me this error.
I have tried with:
public $uses = array('Unit');
and to cancel the line with $uses.
How can I solve it?
To call a function from another controller you can use the requestAction:
Definition
"This function calls a controller’s action from any location and returns data from the action. The $url passed is a CakePHP-relative URL (/controllername/actionname/params). To pass extra data to the receiving controller action add to the $options array".
Usage
This is what your code would looks like:
class ProductsController extends AppController
{
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction() {
// Calls the action from another controller
echo $this->requestAction('/unit/test');
}
}
And then in the UnitController:
class UnitController extends AppController
{
public function test()
{
return 'Hello, I came from another controller.';
}
}
Warning
As said in the CakePHP Cookbook:
"If used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model".
Best solution for you
But, the best solution for you, would be to create a function inside a model and then call from your controller, like this:
class ProductsController extends AppController {
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction() {
echo $this->Unit->test();
}
}
And in the Unit model:
class Unit extends AppModel
{
public function test(){
return 'Hello, I came from a model!';
}
}

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