cakephp Appmodel access child tables - database

There are a relationship between Appmodel and child model. Is that mean it can access every database table ? However, i cannot access the child database. Is there any difference with the sytax?
App::uses('AppModel', 'Model');
class CompanyAppModel extends AppModel{
$this->Brand; it works
$this->BrandLangauge; it does not work
}
class BrandLanguage extends CompanyAppModel {
}
class Brand extends CompanyAppModel {
}

I'm not clear the question. But, you can use ClassRegistry::init needing to load a model that’s not associated with the current model.
$anotherModel = ClassRegistry::init('AnotherModel');

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.

Working with abstract models in cakephp 1.2

I'm working on a project based on cakephp 1.2.2.
And I'm having the following situation.
I have a few model classes with the following hierarchy
Human_Abstract extends AppModel {
var $table = 'livingBeing';
}
Man extends from Human_Abstract...
Woman extends from Human_Abstract...
suppose all those models need to work with the same table,
How do i do that?
because i have tried with the var $table = 'livingBeing';
but if I try
$man = new Man();
$man->findAll(...);
The findAll method arranges the query using
Man.fields
and not using
LivingBeing.id
So... how can i do that?
Thanks!
var $useTable = 'living_being';

Cakephp inheritance of model associations

If I create a Model Car with some associations:
hasOne Driver
hasMany Wheel
And then create a Model Truck extending Car with
class Truck extends Car { ... }
is it normal that the associations from Car are not inherited from Truck?
What are the possibilities to inherit model associations?
Thanks in advance!
Inheritance is PHP related; your custom classes will behave the same way regardless of anything in cakephp. Class inheritance in CakePHP is completely depended on How your classes are written. Inheritance is an important part of cakephp; the framework is very much object oriented.
Are you including the classes properly using App::uses() to load your car class into truck?
The only way your Truck class will not inherit from the Car class is if you have defined the properties in the Truck class.
class Car extends AppModel {
public $hasOne = array('Driver', ...);
}
class Truck extends Car {
public $hasOne = array(); // now you have no hasOne relations
}

CakePHP requiring a database table

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"

Resources