CakePHP returning missing database table - cakephp-2.0

I tried all the suggested solution for this problem which is clearing your cache folders. I also disable the cache Configure::write('Cache.disable', true); My debug level is set to 2. But I always get a same error. The table that is missing is only new added table. By the way, I'm only running in my localhost.
this is my model named Department.php
<?php
class Department extends AppModel {
public $name = 'Department';
}
?>
this is my controller name DepartmentsController.php
<?php
class DepartmentsController extends AppController {
public $name = 'Departments';
public $helpers = array('Html', 'Form','Session');
public $components = array('RequestHandler','Session');
function index() {
$this->Department->recursive = 0;
$this->set('departments', $this->paginate());
}
}
?>
Advance thank you for the answer! :D

Assuming you are actually using a database I would try the following things:
If your database table isn't named using the cakephp conventions, manually set it:
public $useTable = 'table_name';
Make sure your database config is using the right database in app/config/database.php
If that model is in a plugin, use the plugin syntax when including it in the controller:
public $uses=array('PluginName.Department');

Related

CakePHP 2.x: call virtual fields over associated model

i have the Model ExternalShare, which hasOne SfileVersion. And a third Model Sfile, which hasMany SfileVersion.
In SfileVersion i have in $virtualFields the following code:
class SfileVersion extends AppModel {
public $virtualFields = array(
'active' => 'SfileVersion.is_active',
'externalShare' => 'ExternalShare.id'
);
[...]
}
AppModel looks like the following code, so, Containable is available everywhere:
class AppModel extends Model {
public $recursive = -1;
public $actsAs = array('Containable');
}
Now i try to call the find('all') method in SfileController, to get data from Sfile and SfileVersion:
class SfilesController extends AppController {
public function index() {
$this->Sfile->recursive = 0;
$this->Sfile->contain('SfileVersion');
$allSfiles = $this->Sfile->find('all');
}
[...]
}
Now i get the follwing error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ExternalShare.id' in 'field list'
In the CookBook (http://book.cakephp.org/2.0/en/models/virtual-fields.html#limitations-of-virtualfields), here is
A common workaround for this implementation issue is to copy
virtualFields from one model to another at runtime when you need to
access them
, which i tried, by adding the following lines in front of the find('all'):
$this->Sfile->virtualFields['externalShare'] = $this->Sfile->SfileVersion->virtualFields['externalShare'];
But i get still the same error. I didnt directly understand, what the workaround is doing. Also, my virtual fields may not the problem (active seems to work), just the special one, which points to an other (third) Model association (ExternalShare.id). Does someone have an idea or solution? Thank you very much!

CakePHP update public $uses = false; in action from same Controller

I'm writing an installation script for my CakePHP web application. I have a InstallController with 6 actions: step1, step2, step3, etc.
At step1 I'm handling Config/database.php creation. Because this file is empty and no datasource is available I have to set public $uses = false; in the InstallController.
At step2 the Config/database.php file is set so I should be able to make a connection to the datasource. This is also necessary because I want to update some database fields in the following steps.
Is it possible to update the public $uses = false; in every following steps after step1?
I'm using CakePHP version 2.3.5
Have you considered loading the model within the actions? So, something like:
<?php
App::uses('AppController', 'Controller');
class InstallController extends AppController {
public $uses = false;
public function step1() {
}
public function step2() {
$this->loadModel("Install");
$this->Install->callMethod();
}
}
In CakePHP 2.x models are lazy loaded, so as long as your step1 action doesn't try to make use of a model, you can safely declare the models in your controllers $uses property, they are not being constructed until your code actually makes use of them.
However, if for some reason you'd actually need to modify $uses, well then just do it, as mentioned models are lazy loaded, so you can modify $uses whenever you want and then access the models afterwards via magic properties on the controller.

CakePHP 2.0 can't access Model from its own Controller

I'm still trying to migrate from CakePHP 1.3 to 2.0
I have a Controller UsersController and its Model User.
The class User has some constants which I could easily access from the UsersController using User::constant. But for CakePHP 2.0 it doesn't work: I get an error saying the User class is not found. It works if I App::Import('Model', 'User');.
It sure has to do with their built-in lazy loading in 2.0!
you simple need to tell this file that it has other dependencies
do that at the very top of your UserController file:
<?php
App::uses('User', 'Model');
then everything works fine
You can try setting the controller name (in UsersController):
var $name = 'Users';
Or using the "uses" var (in other controllers:
public $uses = array('User');
Does that not work?
Did you declare the name of the model like that ?
class User extends AppModel {
public $name = 'User';
}
Do you have others variables in your UsersController ?
I set variables in the model like this:
//Person model
public $genders = array('m' => 'male', 'f' => 'female');
Then get them from the controller like this:
//People controller
$genders = $this->Person->genders;
No special code needed. (Is this what you're talking about?)
Also, I don't believe you need to set the $name variable anywhere anymore - I think that was just for PHP 4. (not 100% on that part, but... I don't ever set that anymore, and my Cake2 apps run fine)
You have to load the model like this:
$this->loadModel('User');

CakePHP 2.0.3 Fatal Error flash() on object

I've get this message when I tried to use a wrong controller and I figured it out that I'm not getting the right Error from cakephp I've got in 2.0.0 the right one:
Now when I try a wrong controller I get only this message:
Fatal error: Call to a member function Flash() on a non-object in
/srv/www/htdocs/web843/HTML/schaetzmal/lib/Cake/View/Layouts/default.ctp
on line 44
Does cakephp 2.0.3 have an bug or do I miss something to install to let work this or something else what I can do?
Make sure you've added the Session helper to your public $helpers array.
class SomethingsController extends AppController {
public $helpers = array('Session');
}
Or you could add it to a global AppController so that the Session helper is available to all controllers.
class AppController extends Controller {
public $helpers = array('Session');
}
I found some problems why my AppController didn´t work.
Like mensch says i have to use Session in my AppController for global but that´s not the solution because the book of cakephp says in "a global AppController"
NOTE
CakePHP merges the following variables from the AppController to your application’s controllers:
$components
$helpers
$uses
but it´s not happening. Because i overwrite it in the public variable $helpers.
therfore i take the parent given one and merge it with it:
<?
class AppController extends Controller {
public $viewClass = 'Theme';
public $theme;
public function beforeFilter() {
parent::beforeFilter();
$this->theme = 'SM';
$this->helpers = array('Form','Html','Js');
}
}
?>
the $this->helpers = array('Form','Html','Js'); do the merge and it works fine.
thank you guys
thanks for helping mensch that was the hack i needed

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).

Resources