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.
Related
I have created a Model named "Usermgmt" and I am using table for this model is "users" in following manner.
public $useTable = "users";
Now I am loading this Model into different controller named "MembersController" in following manner
public $this->loadModel('Usermgmt');
But it is showing error when I load this model into "MembersController" controller- table "usermgmts" missing for "Usermgmt" model.
And it is working fine in "UsermgmtController" without showing table "usermgmts" missing error and saving data correctly into the "Users" table.
NOTE- I do not have any table with named - "usermgmts" as I am using "users" table for "Usermgmt" Model
Remove the public portion and call it from inside a function:
So if you are trying to add this is a function in MembersController, you would the add it like this:
App::uses('AppController', 'Controller');
class MembersController extends AppController {
public function myFunction() {
$this->loadModel('Usermgmt');
//do stuff
}
}
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
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';
}
?>
I'm using the following code in my bootstrap.php (as explained here) to load models also from another folder:
App::build(array('Model' => array('/my/path/to/models')));
This seems to work. I have a model MyModel inside that folder, which I include in the controller I want to use it like usually:
var $uses = array('MyModel');
If I print App::objects('Model'), the model MyModel is shown in the list, so I assume it's loaded correctly. However, when I try to use the model (i.e. $this->MyModel->find() it never finds anything, it always returns an empty array.
Note that if I put the same exact model (MyModel) in the typical models folder (app/Model/) then it all works fine.
What am I missing to make this work?
EDIT
Ok, so it seems that the problem is in the connection to the database when the model is placed in that folder outside app. With the code shown above, Cake finds the model. However, when I do a find(), I get a missing table error for the datasource (default in this case).
Is it possible that the model isn't loading the correct database configuration because that configuration is inside the app/Config folder? How can I make that model load that configuration? If I have to put that configuration somewehre else (maybe in the same outside folder?) I can do that, but how do I tell the model to find it?
EDIT 2
I can see better what the problem is now. If I put a model in a different folder (other than app/Model) and use App::build() to set the path of that new folder, Cake finds it, there's no doubt (I use App::objects('Model') and the model is listed with all the other models from app/Model).
However, it's like Cake is not actually reading what's inside that model class, or at least not everything. It seems to read the $useDbConfig variable, but it ignores $useTable and any function I have defined in that model. Example of my model:
class Usuario extends AppModel {
var $name = 'Usuario';
var $primaryKey = 'id_usuario';
var $useDbConfig = 'BD_ControlAcceso';
function createTempPassword($len) {
//some code
}
}
If I do a $this->Usuario->find('all'), it returns all the records correctly. However, if I call $this->Usuario->createTempPassword(7) I get a Database Error.
I have another model (MyModel) in that same folder with a $useTable = 'mytable'. If I don a find() on it, I get an error saying that mytable table could not be found. However, if I do $this->MyModel->useTable = 'mytable' then it works fine.
How is this possible? What's going on here?
EDIT 3
I just want to add that I've done extensive testing and the issue is clear: Cake "knows" that the model is in the external folder (confirmed by printing App::objects('Model'), the model is listed there, and if I remove it from that folder then it's not listed). But even though it knows it's there, it ignores whatever is inside the model file. I've tried all the methods below to load the model but none of them worked. Is this a bug in CakePHP? If not, what am I doing wrong?
You should use App::uses('MyModel', 'Model') and is should go before the class declaration like so:
<?php
App::uses('MyModel', 'Model');
App::uses('AppController','Controller');
class UsersController extends AppController {
// controller class
}
Another thing to try is loading the model where you need it:
$this->loadModel('MyModel');
The other thing you can try is the Model instantiation in the top of your model class. Try updating your model to:
App::uses('AppModel','Model');
class Usuario extends AppModel {
var $name = 'Usuario';
var $primaryKey = 'id_usuario';
var $useDbConfig = 'BD_ControlAcceso';
function createTempPassword($len) {
//some code
}
}
I'm using CakePHP 2.0.5 (but this isn't necessarily a cakephp specific question). I have a Coupon and a User model. Each time a user prints a coupon (proccessed by: Coupon Controller):
class CouponsController extends AppController {
public function printcoupon($id = null) {
// code
}
}
I want to save the information to a "coupons_printed" table (id/coupon_id/user_id/created). Should I create a new model for this, or should I just create a function inside of the Coupon model similar to (and call it in the controller each time that page is viewed)?:
class Coupon extends AppModel {
function insertIntoPrinted($id) {
$this->query("UPDATE coupons_printed SET .....");
}
}
Whatever you do, a raw SQL query is not the best way to go. Always use CakePHP methods if at all possible (and almost always it is possible).
You should put the insertIntoPrinted() function in the CouponsPrinted model (although, as a side note, PrintedCoupon would be a more natural way to name the model...) You can then add a HasMany relationship to the Coupon model ($hasMany = array( 'CouponsPrinted' )) and call the function in the CouponsController:
public function printcoupon($id = null) {
$this->Coupon->CouponsPrinted->insertIntoPrinted( $id );
}
CakePHP's model has a thing call association.
In your case, Coupon has a hasMany association with coupons_printed.
You can create a new model, or query using the association in the Coupon model, the generated queries will be the same, I believe.
Your CouponsController already depend on Coupon Model, so not creating another model is a better solution.