Model class inside a prestashop module - php

I am developing a PrestaShop module, which will have it's own database tables. Let's say database table name is 'cat'. So I wanted to have a model class named Cat to keep track of it's properties and related operations. So where should it?
For example, there are prestashop core model classes inside classes directory. Is it ok to create a classes directory inside my module directory for that purpose? will it work?

The standard used is to place the model class in /module/model/YourModelClass.php, you can see this module and in your installation module class you should call it
require_once(_PS_MODULE_DIR_ . 'example/models/YourModelClass.php');
you have not a strict naming standard to your class model, like it does it the controller class and installation class.
Hope that it helps.
Cordially.

PrestaShop model structure is pretty free-flowing. You can decide what structure you want to use.
The only few constraints imposed on you are
having your module class which extends the PrestaShop module class;
registering the appropriate hook and their respective handlers;

My question was about where to place ObjectModel subclasses in prestashop. Above accepted answer is answering that question. But that's not enough to work the module correctly. You will have to include your model class where ever you want to use inside the module.
for example
include_once(_PS_MODULE_DIR_.'mymodule/classes/Cat.php');
class mymoduledisplayModuleFrontController extends ModuleFrontController {
// Other code goes here
}
If you are overriding existing model class, you can put your class inside /modules/your_module/override/classes directory. I have noticed while installing module, your overridden classes will be copied to the prestashop_root/override/classes directory.
http://doc.prestashop.com/display/PS16/Overriding+default+behaviors#Overridingdefaultbehaviors-Overridingaclass

Related

Limit which classes can call another class by namespace or interface

Is it possible to programmatically limit access to a class methods from another class?
I am working with an MVC framework which is made up of modules. Modules exist in their own namespace and namespace exactly mimics folder structure. A module contains, at least, a class that is an instance of the module interface but usually a controller and a model. Most of them also contain lib classes unique to that module.
For what we need this is quite a sweet setup. A module obviously needs to access the public methods of its own namespaced classes but that means other modules can do that too and they should only be calling the module class public methods.
Can a class method's scope be limited to a single namespace (or by interface) or is this a step too much for PHP's current level of object oriented implementation?

Multiple definitions (versions) of a class without conflicts

So recently I came up with this idea to store some very common functions needed for PrestaShop modules in my own extended version of a class:
class MyModule extends Module { ...
There are lot of functions / common code lines / missing functionality that can be stored in an extended class. What I would like to do is to keep this class, add and refine it with the functionality that I need to speed up module development. So later I can just pop it in (the newest version of this class) and dramatically reduce the amount of code lines:
require_once('core/classes/MyModule.php');
class MyModuleName extends MyModule { ...
However, I also realized that I won't be able to do this if the application (PrestaShop) has two or more modules that use this class, because there will be a class definition conflict. PrestaShop does have override functionality, but it is not an option, because if it detects a method that is already overriden, it throws an error (does not allow conflicts). We make a lot different modules, some of them we update, some of them we don't so we don't know how many of our modules the client PrestaShop system will have and which versions they will. Each module should be standalone, but should not conflict with other modules (if the exists and use the extended class).
PHP Fatal error: Cannot redeclare class
Is there any way I can make this happen (using namespaces or something else)?
Is it bad practice to have multiple definitions of a class?
Would it affect performance by much ?
In MyModule case, I need to have access to Module object ($this) to define most of my extended functions.
I also would like to have MyTools class with static methods, but naturally it faces the same problem. Do you think that defining helper functions like this would be the way to go ? :
if(!function_exists(myFunctionName)){
function myFunctionName(){
}
}
In my opinion, the best way to achieve this is to duplicate your class in every module you use, for this reason : your module have a dependency with an external class who will change in the time, so if you change something in your helper class for Module A, there will be no impact on Module B since Module B use its own helper. This is already how PrestaShop do with some modules to keep a compatibility between 1.4 and 1.5 version, with a backward_compatibility/ folder on each module.

Codeigniter bootstrap like Zend

Is there any way to create a function that works for all controllers in Codeigniter at init?
In Zend there is a application/Bootstrap.php, i need some solution like that.
You could extend the native CI_Controller class and create a MY_Controller class that all of your application's controllers would extend. Methods in the MY_Controller class would then be available to every controller that extends it. You could also put code in the MY_Controller constructor that would be executed each time a child controller was constructed.
I don't remember exactly how the Bootstrap file works in Zend, but if this sounds like a viable solution the Creating Core System Classes section of the documentation explains how to extend the native controller.
You can extend your New_controller to CI_Controller. In New_controller you can write common function which you want. For use about new extended controller you can see this link:
The subject of extending core controllers is discussed briefly in a few places in the manual - specifically in the Core Classes and Creating Libraries pages.
The intent of extending the core Controller is to provide methods and attributes to all your normal Controllers. There are other ways of providing this kind of pervasive functionality - including Hooks, Libraries and Helpers. You should familiarise yourself with the methods and benefits of those alternatives before assuming the following is the answer to your question.
Finally, it’s assumed that you have an application that does something - it doesn’t matter what, merely that you have an existing Controller that we can work with here.
-extend_the_CI_Controller

Class extending Zend_Controller_Action, not found by Zend Tool

I created an class extension of Zend_Controller_Action and added some user defined methods, which will be accessed from any controller so forth.
Every thing is working fine, until I use Zend Tool to create a new Action, as this time The Zend tool will not find out my extended class.
Error Message:
Fatal error: Class 'CMS_Zend_Controller_Action' not found in....
That is the class which extends Zend_Controller_Action and the one extended by other controllers like indexController.
How to make the class discoverable. Do I have to include each and every folders, like my classes are? Does zend does that? I dont think so. How does it do it?
Simple. :-p If it can find your core controllers, then you just need to include the path to your extended controllers.
http://php.net/manual/en/function.set-include-path.php
set_include_path(path_to_your_extended_classes) in your index.php, aka routes file.
I think what you are trying here is not what Zend_Tool is about.
As much as I understand your question and setup you have created a class in your library. Of course, you can extend Zend_Controller_Action with lots of your own classes in your own library/libraries (I do that, too). Adding an action to such a class is maybe unusual but a problem for Zend_Tool for one specific reason.
Zend_Tool I believe is only about the well known structures like /application and same for what is inside /modules. If you create a Controller Class Zend_Tool will do some work for you like adding required folder structure to your /application or /modules folder. Same with action method which require view files. Having a Controller Class in your library does not (should not) need all that and hence is not build into Zend_Tool. I think whatever class you create in your library is not supported in Zend_Tool.

Calling Models in Module in Zend Framework

I have a structure that is
application
application/modules
application/modules/default
application/modules/default/models
application/modules/admin
application/modules/admin/models
When calling controllers in admin I understand they must be named like Admin_TestController. This works fine, but my models in my admin module don't seem to be able to load. I have tried naming them and the files in all kinds of ways but it just doesn't seem to want to be able to load them. How should I name the file and model class in a module to be able to use it? I use autoloading.
What do your bootstrap files look like? That is the most import part of problem.
It is very important (in order for the namespaces to autoload) that you you have a bootstrap in each module (located # application/modules/admin/bootstrap.php) that should contain, at the very least:
class Reports_Bootstrap extends Zend_Application_Module_Bootstrap
Notice that it extends Zend_Application_Module_Bootstrap. This does the heavy lifting of registering the namespaces for the MVC of the module.
As Fatmuemoo states the bootstrap for the module should extend Zend_Application_Module_Bootstrap also you should include
resources.modules[] =
In your config. This is in the docs for Zend_Application_Resource_Modules
It seems you need to include a bootstrap class that extends Zend_Application_Module_Bootstrap for the modules you want to use. Check this forum post about a similar issue to see if it helps point you in the right way. Seems you may need more than one to load separate modules.

Categories