Can I somehow omit the first line? i.e. $this->load->model('Model_name'); and autoload it when necessary?
To load a model you will use the following function:
$this->load->model('Model_name');
Once loaded, you will access your model functions using an object with the same name as your class:
$this->Model_name->function();
Lazy loading is not supported in Codeiginiter 2.1
There are a couple of options available to you
Use the 'autoload' in the config. This will always make the model available throughout the application. See here for more info
Load the model in the __construct() of your controller (if it is specific to that controller)
You don't understand the logic .
$this->load->model('Model_name') means you load it when it necessary.
or
If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the model to the autoload array.
Related
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()
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');
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.
I have a multi-domain site. Depending on the domain the site needs to behave accordingly.
I created a helper called CompanyInfo it has methods such as name(), phone(), email(), etc.
Depending on what domain you are on it returns the correct information.
So for example if I need to display the phone number for a user to call I would use $this->CompanyInfo->phone() and it will display the correct phone number for the user depending on the domain.
Ok, this is all good, but not really relevant. The real issue is, I need this information in more than just the view. Helpers are just for views though. If I want to access this information from a controller I need to create a component to do it.
I really don't want to have a Helper and a Component doing the same thing. I would rather have one class handle it rather than copy and paste logic.
So whats the best way to have a class with methods that can be accessed from the controller or view or even model?
Is it just this kind of static information (name, phonenumber, email, etc) what you need to display? Why not just add them to your configuration in core.php?
Something like
# in core.php
Configuration::write('Company.name', 'Acme Corp.');
Configuration::write('Company.email', 'joe#acme.com');
You can then get this info anywhere you need using
Configuration::read('Company.name');
You can define this variable in your app_controller and then use these variable easily in any of your controller as set these variables from there only.
Call this function in your construct class.
i think that will solve your problem.
You can access model classes from any place this way :
$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();
a) you can use libs in cake1.3 for that
b) static model methods which you can pass the content to and which will return the expected value
echo Model::phone($data)
If you want to reuse code in views Symfony has two basic mechanisms: partials and slots. Partials are nice because you can define global partials (you can use them in any module) and module partials (they are only available in a certain module).
However, in CakePHP you only have regular templates and elements, the latter being available in every view, no matter which model/controller you are in.
Does CakePHP have anything like Symfony's partials? It would nice for example to avoid duplicating forms code for a model. You can have two templates (add and edit) that "include" a common form.
I know you can still use elements, but having a "local" elements directory for a module seems to keep things more organized. Can you suggest a workaround to simulate this?
Thanks!
Why not create a view (module_partial.ctp) inside the controller specific view directory. This will keep the code specific to the controller you want it to pertain to. So lets say you have a books controller. You want to add a BooksController specific form to some of your books views.
Create a view in the views/books/ directory called: search_partial.ctp
The search_partial.ctp will contain the HTML code you want.
Then, in any view, just call:
<?php echo $this->render('search_partial'); ?>
This will not prevent other controllers views from loading it, but it keeps the code base readable and segregated as you expect.
ALL of the globals would go into views/elements.
You can put elements in plugins.
You can do something like $this->element('something'); in the layout and have the element in a plugin and/or the main app views folder like such...
App/plugins/a_plugin/views/elements/something.ctp //only called when a controller from 'a_plugin' is called.
App/views/elements/something.ctp // called if the current plugin does not have 'something.ctp' in the elements folder
For not duplicating views like add/edit look at this https://github.com/infinitas/infinitas/blob/beta/app_controller.php#L389