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');
Related
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");
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());
}
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.
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!';
}
}
Hye. I want to make a controller and model for a table which is named in uppercase and abbreviation.
For example, the table name is PS_DEPT_TBL
1) What would be the controller file name? Is it PSDEPTTBL_controller.php? The code below seems to be not working for the controller.
class PSDEPTTBLController extends AppController {
var $uses = 'PS_DEPT_TBL';
var $scaffold ;
}
2) I name the model file as PSDEPTTBL.php and code it as below.
class PSDEPTTBL extends AppModel {
var $useTable = 'PS_DEPT_TBL';
}
But the error shows that there isn't any controller of the table. I'm new to cakephp. Help me.
There is no need for your controller to be called the same as your table. The same goes for your model.
How about something this:
// ThingsController.php
class ThingsController extends AppController
{
var $uses = array('Thing');
}
// Thing.php
class Thing extends AppModel
{
var $useTable = 'PS_DEPT_TBL';
}
// Config/routes.php
Router::connect(
'/PSDEPTTBL/:action/*', array('controller' => 'things')
);
The key idea here is to make your code readable. If you have a legacy database, the best thing you can do for yourself is to hide it all in all the models.