CakePHP model named 'Model' not working - cakephp

I have a model named Model. After some frustration with its responses, I came to the conclusion that the class file is not being used.
To test this, I changed the name of the file from model.php to model_x.php and no errors were encountered.
Can someone verify to me if Model is an invalid name for an appModel class?
I am using CakePHP 2.x
Lee

The class Model is already taken
So what you should do is create another name. It's the same when using a controller and model named layout, because the Layout folder in View is already used for the layouts. So you should either come up with a clever solution with routing, or simply give it a different name.

Related

cakephp 2.10 get data without model class

Cakephp 2.10 ?
$text = $this->PrivacyPolicy->find('all', array(
'fields' => array('id', 'title', 'description')
));
How PrivacyPolicy is becoming a model role as there is no Model class in the files or relation with the database ?
Magic and unicorn dust... otherwise known as naming conventions and auto-models.
If no concrete model class can be found, CakePHP will create an instance of the AppModel base class instead, which will lookup the database table based on the the model name that you've used. So in your case the AppModel instance will look up the data in privacy_policies (lowercased, underscored, plural variant of the model name).
Quote from the docs:
CakePHP will dynamically create a model object for you if it cannot find a corresponding file in /app/Model. This also means that if your model file isn’t named correctly (for instance, if it is named ingredient.php or Ingredients.php rather than Ingredient.php), CakePHP will use an instance of AppModel rather than your model file (which CakePHP assumes is missing). If you’re trying to use a method you’ve defined in your model, or a behavior attached to your model, and you’re getting SQL errors that are the name of the method you’re calling, it’s a sure sign that CakePHP can’t find your model and you need to check the file names, your application cache, or both.
https://book.cakephp.org/2/en/models.html#understanding-models

CakePHP 3 - How to identify the installed behaviors in a model when baking a controller

I'd like to adapt my controller baking code (vendor/cakephp/bake/src/Template/Bake/Template/Bake/Controller/controller.ctp) so when baking a controller it will automatically detect if there is a (f.e. Translate-) behavior installed in the model and add "use Cake\I18n\I18n;" to the controller while baking it.
So, can anyone tell me how to identify the installed behaviors within the controller-baking-code?
Given that the table class already exists when baking the controller, you should be able to get the required information from $modelObj that is passed to the view, it's an instance of the table class that is associated with the controller.
The behavior registry available via Table::behaviors() should have the info that you need.
$modelObj->behaviors()->has('Translate')
And of course you can get further information from the table, like the schema (Table::schema()), validation rules (Table::validator()), etc...
See also
API > \Cake\ORM\Table::behaviors()
API > \Cake\ORM\BehaviorRegistry::has()

Unable to access model in plugin

I have two Plugins in cakephp both contains User model. When I want to access the properties of User model it always call first User model. How can I access second User Model?
Not possible - rename your models
Unfortunately that's not possible. Because of the way models are defined, that means you're wanting to load two different classes with the same name in the same namespace (global) which is not possible with PHP - the only solution is to use different classnames.
A standard practice is to name plugin classes prefixed with the plugin name to avoid collisions:
So for example in the foo plugin:
<?php
//App/Plugin/Foo/Model/FooUser.php
class FooUser extends FooAppModel {
}
And in the bar plugin:
<?php
//App/Plugin/Bar/Model/BarUser.php
class BarUser extends BarAppModel {
}
This does lead to slightly more cumbersome usage, but prevents roadblock-problems when trying to access both classes at the same time.
You have to specify the plugin name when you initialize the model classes. So you could call your first one 'User' and your second one 'PluginUser', ie:
$this->User = ClassRegistry::init('PluginOne.User');
$this->PluginUser = ClassRegistry::init('PluginTwo.User');

CakePHP 1.3: extending class unable to find its parent model PHP file

In the class declaration line of the model (.php) files, I'm trying to extend off of a different class then AppModel. Let's say I have some models, including Model, ModelOrder, and ModelLastShipment, etc. And yes, "Model" is a made up name for the sake of generalization.
Model does not use a table and does not have any table association. It extends AppModel. It has some basic functions that I'd like ModelOrder and ModelLastShipment (and other Model... classes) to inherit. The problem is that while ModelOrder can be extended (i.e. class ModelOrder extends Model {...}), I'm unable to do the same with the following because of a missing file error.
model_last_shipment.php:
class ModelLastShipment extends Model {...}
This returns the error,
Fatal error: Class 'Model' not found in C:\xampp\htdocs\my_app\app\models\model_last_shipment.php on line 3
Including include_once('model.php') before the declaration solves this issue, but why does the extension work for ModelOrder without explicit inlcusion but not for ModelLastShipment?
Please let me know if there is more information needed to resolve this. There are many articles and posts regarding this sort of error, but I've yet to find out why I'm getting this error for one and not for another.
Let's say I have some models, including Model
It's impossible to create a class named "Model"
Model is a core class. If you create a class named Model, it with either not be loaded (because the class Model already exists) or cause fatal behavior - possibly "at random".
The class structure in the question is:
Overloadable (Cake)
Model (Cake)
AppModel (App)
Model (App) <- problem
That cannot work. Class names must be unique and Model is a core class.
Including include_once('model.php') before the declaration solves this issue
Are these model class names made up? As stated above that isn't possible with a class named Model.
Intermediary class models are not automatically loaded
These are the only model classes that are loaded automatically in Cake 1.3:
Model
AppModel
AppModel (when appropriate)
If the class hierarchy of your models is such that there are more intermediary classes - they need to be loaded explicitly:
<?php
App::import('Model', 'SomethingElse');
class NotNamedModel extends SomethingElse {

Dynamically passing a model name to a CakePHP Plugin

I'm having trouble wording my problem, so it's been tough to search for an answer. Hopefully you'll know how to help.
I am creating a CakePHP 2.1 Plugin that will interact with a series of its own Models:
- Friend
- Group
- User
Friend and Group are models that are created specifically for the Plugin, and they function within the plugin normally. However, the User model is really just an alias for some other table in the parent app.
So, if "My Awesome Application" decides to use "My Awesome Plugin", it will have to have its own "users" table (though it may called something else). Let's say "My Awesome Application" has a Model called MyUser. "My Awesome Plugin" wants to dynamically tell its internal User model to $useTable = "my_users".
My question is, how do I pass that data to the Plugin? How do I configure "My Awesome Plugin" to understand that User should $useTable "my_users";
As I understand you would like a Model in a PlugIn to use a table that would typically belong to a Model in your Application - by the conventions. Have you tried statically setting:
public $useTable = "my_users";
in the plugin? All plugins usually get initialized when Cake starts up, so all configurations should be loaded then. Why do you need this - it does really restrict you a lot? Will the table being used by the Plugin model change runtime?
The Model class also has some goodies you may find useful:
$this->User->table;
holds the table name for the model - the table that is currently being used that is**.
Also you can set the source table for the Model (inside a Controller) with:
$this->User->setSource('table_name);
** I am not sure if this applies when you use Model::setSource(). It would be interesting to check out what $this->User->table; holds after a Model::setSource() call.
I've figured out a way to accomplish this, but it might not work in all scenarios for all people.
I created a Component in my Plugin, and then I call the Component in my Controller. I pass the name of the users Model through the Component. This way, I can get information about the users Model, and I can set it as the useTable to my Plugin for use in the Plugin.
Of course, this method restricts me to using the Component to utilize the Plugin, but that's probably for the best.
Here's an example of how I did it:
// in the AppController
public $components = array(
'MyPlugin.MyPluginComponent' => array('userModel'=>'UserModelName')
);
// in the Plugin's Component
class MyPluginComponent extends Component {
function initialize($controller) {
//get the base user model
$this->UserModel = ClassRegistry::init($this->settings['userModel']);
//set the useTable for our plugin's user model
$this->PluginUser = ClassRegistry::init('MyPlugin.PluginUser');
//set the useTable value for this model
$this->PleaseUser->setSource($this->UserModel->useTable);
}
That seems to work for me. Hope this helps someone else.

Resources