Cakephp table name error [duplicate] - cakephp

This question already has answers here:
CakePHP specify table-name
(2 answers)
Closed 7 years ago.
I created a table named as quantities_master but when i using modal name as QuantitiesMaster it is showing some errors. what would be modal name of quantities_master table name?

Table names should be plural and model names singular in CakePHP. So you should have a table named quantities_masters and a QuantitiesMaster model.
However, if you need to have a table with a singular name you can define this in the model.
In CakePHP 2:-
class QuantitiesMaster extends AppModel {
public $useTable = 'quantities_master';
}
In CakePHP 3:-
class QuantitiesMasterTable extends Table
{
public function initialize(array $config)
{
$this->table('quantities_master');
}
}
Only ever break from CakePHP's naming conventions when you have no other choice as it makes working with CakePHP significantly harder!
If you find determining the plural and singular of words for CakePHP (i.e if English is not your first language) then the CakePHP Inflector website is really handy.

The model is looking for quantities_masters table. Add this line at the beginning of the class and it will work fine:
class QuantitiesMaster extends AppModel {
public $useTable = 'quantities_master';
}

Related

Inflector rules - Table without underscore in db

I have a simple problem and I don't know how to solve it.
I work with an existing database, where tables don't match with cakePHP conventions, and I have to make cakePHP work with it.
For example, I have a table named "ItiConf" in sql db (instead of iti_confs by convention).
My model ItiConfModel.php :
class ItiConf extends AppModel {
}
My controller ItiConfsController.php :
class ItiConfsController extends AppController {
//...
}
I tried to make my own Inflector::rules in app/Config/bootstrap.php file,
but it doesn't work and I still have the following error :
*Error: Table iti_confs for model ItiConf was not found in datasource default.*
Do you please have any idea or hint about this problem and the synthax of the related inflector rule needed ?
Thanks you by advance !
Inset07.
While Cake has its conventions, it doesn't require them to be used. In this case, try the $useTable property to change the table the model uses, instead of modifying Inflector rules:
class ItiConf extends AppModel {
public $useTable = 'ItiConf';
}
More info here: http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable

cakephp changes when changing the name of the database table

I work in cakephp, I have a database table named 'movements' I want the named 'movements_bags' what changes need to be made​​:
- The model
-the controller
-folder view
To use a table with a name that is outside the CakePHP table naming convention (e.g. Movement model has table names movements, but you want to use the table named movements_bags) you simply specify which table name to use in your model with the $useTable property:
class Movement extends AppModel {
var $useTable = 'movements_bags'; // default would be movements
// ...
}
I prefer Scrowler answer, but if you are not happy with this answer you can try bellow code.
change controller name to MovementsBagsController.
And change controller class name
class MovementsBagsController extends AppController {
}
Change your model name to MovementsBag
and change model class name
class MovementsBag extends AppModel {
}
change view folder to MovementsBags
I think that will work fine now.

Getting CakePHP to work with existing "non-Cake" database

I am building a new app based on multiple databases with many tables which don't follow any Cake conventions. I want to use CakePHP but I'm not sure if it's possible without the database in a Cake format.
Problems include:
Tables not named as Cake expects
Primary keys are not necessarily named id (e.g. it might be order_id)
Foreign keys are not necessarily named like other_table_id
Changing the database tables is not an option.
Is it possible to manually configure the schema in each model so that Cake will then know how the model relationships need to work? Or should I just give up on using Cake?
yes. you can still use CakePHP in your case.
Check out various Model attributes to fit your needs
http://book.cakephp.org/2.0/en/models/model-attributes.html.
e.g.
public $useTable = 'exmp' can be used to configure what table to use.
public $primaryKey = 'example_id'; can be used to configure the primary key's name
**Try this code sample............**
More detail here http://book.cakephp.org/2.0/en/models/model-attributes.html
<?php
class Example extends AppModel {
// Name of the model. If you do not specify it in your model file it will be set to the class name by constructor.
public $name = 'Example';
// table name example
public $useTable = example;
// example_id is the field name in the database
public $primaryKey = 'example_id';
}
?>

CakePHP: can't get models to work [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
Im really new to cakePHP, and I more or less understand the controllers, and views as I worked with a MVC framework before. However, the models and naming conventions in cakePHP seem to be different from what I've used before. the one's I've used; you call various functions such as select, and insert, with the fields and such you want, and then execute the query. Essentially you build your own query. I'm trying to figure out how to use models in cakePHP, and as I understand it, if I have a controller in /controllers/users_controller.php with the contents:
<?php
class UsersController extends AppController{
var $name = 'Users';
var $helpers = array ('Html','Form');
public function login(){
$this->set('msg', 'Login PAGE!!!! YAY!!!!!!!!!');
$this->set('MT', $this->Users->find('all'));
}
}
then it should automatically have the Model, Users, in models/users.php available to it right? Therefore the variable $MT can be echoed on the page.
The contents on Users.php are:
<?php
class Users extends AppModel{
var $name = 'Users';
}
The problem I'm having though seems to be:
Undefined property: UsersController::$Users [APP\controllers\users_controller.php
and thus leads to:
Call to a member function find() on a non-object in app\controllers\users_controller.php
I'm following the tutorial at http://book.cakephp.org/1.3/en/The-Manual/Tutorials-Examples/Blog.html for cakePHP 1.3 running on a WAMP server. Any advice you can give would be appreciated! Thanks a lot!
change your model name:
class Users extends AppModel{
...
to
class User extends AppModel{
...
and:
$this->set('MT', $this->Users->find('all'));
to
$this->set('MT', $this->User->find('all'));

Cake PHP Model Associations - What am I doing wrong?

No matter how many times I read through the documentation, look at examples I cannot get my model to "associate" with another.
Here is my example:
I have a table called kits and a table called grades (read books and authors).
kits looks like this (simplified):
id (PK)
grade_id (FK)
name
grades looks likes this:
id (PK)
code
Each kit will have only 1 grade. I don't see any issue with naming conventions, it follows as prescribed in the API.
--
I have a controller called KitsController.php that looks like this:
class KitsController extends AppController {
public function index() {
//simple find that limits to one (for readability)
$pigs = $this->Kit->find("all", array(
"limit" => 1
));
$this->set("pigs", $pigs);
}
}
I have a corresponding index.ctp file that outputs the variable $pigs (not a production level variable name I know).
--
I have my model called KitModel.php
class Kit extends AppModel {
public $belongsTo = "Grade";//here is where I am confused
}
...and an empty Grade model for a placeholder
class Grade extends AppModel {
}
--
The Problem:
When the array prints out on the page it looks like this.
Array ( [0] => Array (
[Kit] => Array (
[id] => 1
[grade_id] => 1
[name] => Ferrari
) ) )
From my understanding when I print out the results it should include the Grade information along with the kit information. It certainly does for every example I have seen online. I have also tried different relationships (hasOne, etc) but to no avail. Also I have tried the long form by passing in an array with the classname and foreign key but if my schema matches cakePHP convention I shouldn't have to do that.
What am I missing?
I have my model called KitModel.php
Your model filename is incorrect. It should be Kit.php. If CakePHP doesn't find a model file named as per convention it uses an AppModel instance for the model.

Resources