Controller not found in cakePHP - cakephp

I have controller class EmployeeController in employee_controller.php file,and i have a model class Employee in employee.php ,database table is employees ,All the functions are working (such as findall() and read() are working fine),but i have add function which is like this
function add() {
if (!empty($this->data)) {
if ($this->Employee->save($this->data)) {
$this->Session->setFlash('Employee has been saved.');
$this->redirect(array('action' => 'index'));
}
When i tried to save ,An error EmployeesController not found will display and shows the following code
<?php
class EmployeesController extends AppController {
var $name = 'Employees';
}
?>
i am not able to solve this problem ,please help me out to get rid of this problem

The file should be called employees_controller.php, not employee_controller.php.
All controller files are named in the plural.

You can also learn about the naming of the tables and the conventions at http://cakeapp.com

I don't know if the question is still important, but the cakephp convention says that filenames for controllers must be in plural an without underscores, so your controller file should be named EmployeesController.php.

Related

use unassociate model in another model with condition in cakephp [duplicate]

Can I use another Model inside one model?
Eg.
<?php
class Form extends AppModel
{
var $name='Form';
var $helpers=array('Html','Ajax','Javascript','Form');
var $components = array( 'RequestHandler','Email');
function saveFormName($data)
{
$this->data['Form']['formname']=$data['Form']['formname'];
$this->saveField('name',$this->data['Form']['formname']);
}
function saveFieldname($data)
{
$this->data['Attribute']['fieldname']=$data['Attribute']['fieldname'];
}
}
?>
Old thread but I'm going to chime in because I believe the answers to be incomplete and lacking in "why". CakePHP has three ways to load models. Though only two methods work outside of a Controller, I'll mention all three. I'm not sure about version availability but this is core stuff so I believe they'll work.
App::import() only finds and require()s the file and you'll need to instantiate the class to use it. You can tell import() the type of class, the name and file path details.
ClassRegistry::init() loads the file, adds the instance to the object map and returns the instance. This is the better way to load something because it sets up "Cake" things as would happen if you loaded the class through normal means. You can also set an alias for the class name which I've found useful.
Controller::loadModel() uses ClassRegistry::init() as well as adds the Model as a property of the controller. It also allows $persistModel for model caching on future requests. This only works in a Controller and, if that's your situation, I'd use this method before the others.
You can create instances of other models from within any model/controller using one of these two methods.
If you're using Cake 1.2:
App::import('model','Attribute');
$attr = new Attribute();
$attr->save($dataYouWantToSavetoAttribute);
If you're using Cake 1.1:
loadModel('Attribute');
$attr = new Attribute();
$attr->save($dataYouWantToSavetoAttribute);
An obvious solution everyone missed is to create an association between two models, if appropriate. You can use it to be able to reference one model from inside another.
class Creation extends AppModel {
public $belongsTo = array(
'Inventor' => array(
'className' => 'Inventor',
'foreignKey' => 'inventor_id',
)
);
public function whoIsMyMaker() {
$this->Inventor->id = $this->field('inventor_id');
return $this->Inventor->field('name');
}
}
In CakePHP 1.2, it's better to use:
ClassRegistry::init('Attribute')->save($data);
This will do simply
<?php
class Form extends AppModel
{
//...
$another_model = ClassRegistry::init('AnotherModel');
//...
}
?>
In CakePHP 3 we may use TableRegistry::get(modelName)
use Cake\ORM\TableRegistry;
$itemsOb = TableRegistry::get('Items');
$items = $itemsOb->find("all");
debug($items);
If you want to use Model_B inside Model_A, add this line at the beginning of Model_A file:
App::uses('Model_B_ClassName', 'Model');
and then you will be able to use it inside Model_A. For example:
$Model_B = new Model_B_ClassName();
$result = $Model_B->findById($some_id);
var $uses = array('ModeloneName','ModeltwoName');
By using $uses property, you can use multiple models in controller instead of using loadModel('Model Name').
App::import('model','Attribute');
is way to use one model into other model. Best way will be to used association.

CakePHP 2.1 enters infinite loop when saving a record

The question probably sounds quite odd, and indeed it is. Here's the problem: I have a model, FollowingStationLine, and its corresponding table, following_station_lines. In the model, I am trying to save a record with $this->save(array('field' => 'value));. When I hit the corresponding page, the method seems to be calling in an infinite loop (I've put some debug()s before and after the save statement) and it ends only when the memory limit has been reached. I tried even with 2GB of memory, and still the same issue.
This happens also when calling the method from another controller, other than FollowingStationLinesController. All other models/tables work as expected, beside this one. I really can't imagine what is the problem. Here are the codes for the controller, respectively the model:
Controller:
App::uses('AppController', 'Controller');
class FollowingStationLinesController extends AppController {
public function admin_set() {
$this->FollowingStationLine->set(array(1));
}
}
Model:
App::uses('AppModel', 'Model');
class FollowingStationLine extends AppModel {
public function set($lineIds = array()){
if(!is_array($lineIds)){
return false;
}
$save = array();
$save[] = array('station_id' => 45);
debug($save[0]);
$this->save($save[0]);
}
}
Any help would be highly appreciated!
CakePHP's AppModel class has a function called 'set' that is called by the model's 'save' function. By overrriding 'set', you are causing a loop between the 2 methods.
Rename your function to something else and you should be ok.
You can inspect the AppModel source for more info: https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/Model.php
UPDATE:
#mark made a very good comment that if your PHP configuration included error reporting mode E_STRICT, you would have received an error for overriding with a mismatched signature.
More info to set your configuration: http://php.net/manual/en/migrating5.errorrep.php

displaying data using a query in a controller - cake php framework

Hy guys, I started to study cake php framework (version 2.0) and when i finished to read the blog tutorial i tried to do some experiments in particular my problem is "Is it possible to create a query in the model then execute it in the controller and in the end display the result of the query in a view in this way?
this is the file post.php(the model):
<?php
class Post extends AppModel {
public $name='Post';
}
?>
this is posts_controller(the controller):
<?php
class PostsController extends AppController {
public $helpers = array ('Html','Form');
public $name = 'Posts';
public $name = 'Articles';
function index() {
//$this->set('posts', $this->Post->find('all'));
$sql="select * from posts";
$this->set('Articles',$this->Post->query($sql));
}
}
?>
The Question is if I declare for the second time $name I obtain an error from cake, in this case, Which is the correct name to set a variable that contain a posts arrays (the databse is the same of the blog tutorial) and the second question is How can I display the data obtained from the query in the index.ctp?? in the example I iterate in this way
<?php foreach ($posts as $post): ?>
and to obtain an element I have to write
<?php echo $post['Post']['title']?>
but in my case?
And Is it possible to declare a function tha returns a result of a query in a model, then call it in a controller and display the data in a someview.ctp?
Question 1: $name should be the name of the controller itself, not sure what you're trying to do with 2 names. Just stick with "Posts".
Question 2:
You're probably just confusing matters here by referring to posts as articles..
Best thing for you to do is change this:
$this->set('Articles',$this->Post->query($sql));
to
$this->set('posts',$this->Post->query($sql));
Then the references in the template will work the same. What's happening here is when calling the $this->set method you're making the data accessible to the template in a variable called $posts. So what you currently have:
$this->set('Articles',$this->Post->query($sql));
Will make the data accessible to the template in a variable called $Articles.

CakePHP doesn't load model

I'm new to cakePhp development. I've stuck on following problem:
I've made few models, controllers and views - it works great. The problem is that after production, I have to made new table(Transactionaltemp table and corresponding model and controller ) in the db that logically is "connected" to other tables, but technically does not needs to - for ex. it holds temporary info on user_id, time, ip and similar. So, other tables doesn't need to be directly connected to that.
The problem is when I try (in some other controller than transactionaltemps_controller):
$this->loadModel('Transactionaltemp');
I get error - the model is not found (it is true because the model is missing in the cache). Interesting enough transactionaltempls_controller is in the cache (in the cake_controllers_list file).
I tried following stuff to resolve the problem:
clear cache
disable cache
tried using uses={..} code in the controller that I would like to use mymodels_controller
tried using init('Transactionaltemp')
with no success. Here is corresponding code:
The model:
<?php
class Transactionaltemp extends AppModel
{
var $name = 'Transactionaltemp';
function beforeSave() {
return true;
}
}
?>
The controller:
<?php
class TransactionaltempsController extends AppController
{
var $name = 'Transactionaltemps';
var $scaffold;
}
?>
I'll very grateful to any help!!!
App:Import('Model','Transactionaltemp');
$this->Transactionaltemp= new Transactionaltemp;
I hope this may work
If you are connecting to a table name with different name than your model, you must specify the table name in it:
<?php
class Transactionaltemp extends AppModel
{
var $uses = 'Transactional';
var $name = 'Transactionaltemp';
function beforeSave() {
return true;
}
}
Try
App::Import('Model', 'YourModelName');
in your controller (or where you want).

Can I use one model inside of a different model in CakePHP?

Can I use another Model inside one model?
Eg.
<?php
class Form extends AppModel
{
var $name='Form';
var $helpers=array('Html','Ajax','Javascript','Form');
var $components = array( 'RequestHandler','Email');
function saveFormName($data)
{
$this->data['Form']['formname']=$data['Form']['formname'];
$this->saveField('name',$this->data['Form']['formname']);
}
function saveFieldname($data)
{
$this->data['Attribute']['fieldname']=$data['Attribute']['fieldname'];
}
}
?>
Old thread but I'm going to chime in because I believe the answers to be incomplete and lacking in "why". CakePHP has three ways to load models. Though only two methods work outside of a Controller, I'll mention all three. I'm not sure about version availability but this is core stuff so I believe they'll work.
App::import() only finds and require()s the file and you'll need to instantiate the class to use it. You can tell import() the type of class, the name and file path details.
ClassRegistry::init() loads the file, adds the instance to the object map and returns the instance. This is the better way to load something because it sets up "Cake" things as would happen if you loaded the class through normal means. You can also set an alias for the class name which I've found useful.
Controller::loadModel() uses ClassRegistry::init() as well as adds the Model as a property of the controller. It also allows $persistModel for model caching on future requests. This only works in a Controller and, if that's your situation, I'd use this method before the others.
You can create instances of other models from within any model/controller using one of these two methods.
If you're using Cake 1.2:
App::import('model','Attribute');
$attr = new Attribute();
$attr->save($dataYouWantToSavetoAttribute);
If you're using Cake 1.1:
loadModel('Attribute');
$attr = new Attribute();
$attr->save($dataYouWantToSavetoAttribute);
An obvious solution everyone missed is to create an association between two models, if appropriate. You can use it to be able to reference one model from inside another.
class Creation extends AppModel {
public $belongsTo = array(
'Inventor' => array(
'className' => 'Inventor',
'foreignKey' => 'inventor_id',
)
);
public function whoIsMyMaker() {
$this->Inventor->id = $this->field('inventor_id');
return $this->Inventor->field('name');
}
}
In CakePHP 1.2, it's better to use:
ClassRegistry::init('Attribute')->save($data);
This will do simply
<?php
class Form extends AppModel
{
//...
$another_model = ClassRegistry::init('AnotherModel');
//...
}
?>
In CakePHP 3 we may use TableRegistry::get(modelName)
use Cake\ORM\TableRegistry;
$itemsOb = TableRegistry::get('Items');
$items = $itemsOb->find("all");
debug($items);
If you want to use Model_B inside Model_A, add this line at the beginning of Model_A file:
App::uses('Model_B_ClassName', 'Model');
and then you will be able to use it inside Model_A. For example:
$Model_B = new Model_B_ClassName();
$result = $Model_B->findById($some_id);
var $uses = array('ModeloneName','ModeltwoName');
By using $uses property, you can use multiple models in controller instead of using loadModel('Model Name').
App::import('model','Attribute');
is way to use one model into other model. Best way will be to used association.

Resources