CakePHP requiring a database table - cakephp

I intended to create a controller that handles a contact page. I created the controller ContactsController. The problem is that it is asking for a table with the same name:
Missing Database Table
Error: Database table
username_contacts for model Contact
was not found.
Notice: If you want to customize this
error message, create
app/views/errors/missing_table.ctp
Do I really need to create a table with no data for this?
This is my controller code:
<?php
class ContactsController extends AppController {
var $name = 'Contacts';
function index($id = null)
{
$this->set('page', ClassRegistry::init('Page')->findByShortname($id));
}
}

var $name = 'Contacts';
var $uses = array();
not to be that guy, but this is documented well.
http://book.cakephp.org

You might want to create the model anyway as you'll almost certainly find you need to do some database type stuff. It doesn't need to use a db_table:
class ModelWithoutTable extends AppModel
{
var $useTable = false;
}
Think "fat model - thin controller"

Related

CakePHP: create temporary table and use it

I'm trying to create a temporary table within CakePHP 2.x, but I always receive the message "Error: Call to a member function query() on a non-object".
I did some research and found a solution, but this one is not working for me: Create temporary table in CakePHP and load it as a Model
EDIT:
The following code now produces a different error: "Error: Table devices_cls for model DevicesCl was not found in datasource default."
Here is my code:
class DevicesController extends AppController {
public $uses = array('Device','Client');
public function index(){
$conditions = array();
$tmpModel = 'DevicesCl';
$tmpTable = 'devices_cls';
$this->loadModel('DevicesCl');
$this->DevicesCl->query("CREATE TEMPORARY TABLE IF NOT EXISTS devices_cls AS (SELECT uuid,ownerUuid,status,os,imei,updatedOn,msisdn,model FROM device)" );
As I'm quite new to CakePHP, do I need to add an additional model class? I don't think so, as this should be handled by the temporary table - right?
Thanks for your support!
class DevicesController extends AppController {
public $uses = array('Device','Client');
public function index(){
$conditions = array();
$tmpModel = 'DevicesCl';
$tmpTable = 'devices_cls';
$this->loadModel('DevicesCl');
$this->DevicesCl->useTable=false;
$this->DevicesCl->query("CREATE TEMPORARY TABLE IF NOT EXISTS devices_cls AS (SELECT uuid,ownerUuid,status,os,imei,updatedOn,msisdn,model FROM device)" );
$this->DevicesCl->useTable = $tmpTable;
The problem is that the Model "DevicesCl" does not exist yet.
in your example on the link : the Model exist
so if you change
$this->DevicesCl->query(...)
to
$this->Model->query(...)
But i don t see why you need a functionnality like that... i think there are surely best solutions no ?
In Cakephp 3, you can do the following:
Model/Table/MyTempTable.php
<?php
// ......
class MyTempTable extends Table{
public function initDownload(){
$connection = ConnectionManager::get('default');
$connection->execute('CREATE TEMPORARY TABLE temptemp as (select * from refernece_db)');
// Do your thing
}
}
// When use the temp table, first should do:
$this->MyTempTable->initDownload();
// Then, can use Cakephp3 Table syntax normally as if it is a normal table

Cakephp: How to get table data without creating Models/Controllers?

My doubt is, I had few tables named client1,client2,client3 etc., I need to get the data of each client in single controller without creating any model/controller for each table. Can any one explain how to get those values.
You can use dynamic models
app/client_model.php
<?php
class ClientModel extends Model {
var $name = 'Client';
var $alias = 'Client';
function __construct($table) {
$this->useTable = $table;
parent::__construct();
}
}
?>
And Use like this for client1 table
App::import('model','Client');
$client = new ClientModel('client1');
$client->find('all');
You Can do it by firing normal mysql queries selecting table and fetching values
in controller as we do in core php.

define model association without the second model

Is there a way in CakePHP 1.3 to define a model association without having a model for the associated table? For example:
<?php
class SomeModel extends AppModel
{
var $useTable = 'some_table';
var $belongsTo = array(
'AnotherModel' => array(
// association data here
)
);
}
?>
Where AnotherModel doesn't actually have a model file. I just want to define the table that model would use and the association details. Is this possible?
Quick answer is: It should be fine. Have you tried it? Worse case scenario is it won't work without the file, so just add the model file. It takes all of two seconds:
<?php
class AnotherModel extends AppModel {
var $name = 'AnotherModel';
}
?>
Done!
UPDATE
If you follow cake convention on the naming of tables, you should be able to reference the table using the appropriate name without the model file. For example:
my_models = MyModel
your_models = YourModel
model_tables = TableModel
However, if you have a table that does not follow convention, you must create a model file that defines $useTable to indicate which table it relates to:
some_table = model file: SomeTable where $useTable = 'some_table';
another_model = model file: CustomModel where $userTable = 'anotherModel';
There is no other way around it. CakePHP is not magic. It needs to know what table is being referenced. Unless you are doing joins. Then in the join you can reference the table.

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