Extend model

2018-07-16 09:44:51
tengfei
1419
Last edited by tengfei on 2019-01-08 09:38:00

Model is mainly used to query and update various kinds of data. There are three ways to extend a model, 



  • add or override methods, 
  • use a hook, and 
  • define a new class.



1. Add or override methods

No matter it is to add a new method or to override the existing method, you have to name the corresponding file in the name of the method in the ext/model/ directory of the module. For example, add a new method to the misc model and name it foo. What you do is to create foo.php in misc/ext/model/. The code is as follows:


public function foo()
{ 
     return 'foo';
} 
Note: The definition does not contain the statement of class. It is just a statement of methods. When ZenTaoPHP is implemented, it will automatically replace the code in misc/model.php with the code in foo.php in the extension directory. If the new method is added, it will add to misc/model.php, and generate a merged model class file.


2. Use a hook

You can also do extension by a hook. All hooks are stored in the ext/model/hook directory. The naming rule is method name. extension.php. For example, extend the "hello" method in the misc module using a hook. Create the hello.abc.php file under misc/ext/model/hook/, and then implement the code.


ZenTaoPHP merges the code of all hook for one method into the final code. However, there are restrictions on this method and unexpected actions so it is not recommended.


3. Define a class

In addition to the two ways mentioned above, there is another way to extend a model, which is to put all the extensions in a class, and then load it through the loadExtension() of zentaoPHP. This is mainly to deal with the conflicts of encrypted files.


zentaoPHP will merge the corresponding code when processing the extension of a model, but this also leads to a problem. If exe/model/abc.php is encrypted, it will conflict with other extensions of the models in open source version. If it is not encrypted, the code cannot be protected. To solve this problem, the third way to extend.


3.1 Create a class under ext/model/class/. The naming rule is plug-in.class.php.

For example, the Gantt chart plug-in. It is defined as project/ext/model/class/gantt.class.php which defines various kinds of codes. The naming rule of the class is {plug-in name} {module name}, and the initials of the model should be capitalized, e.g. ganttProject.


class ganttProject extends projectModel
{
    public function createRelationOfTasks($projectID)
    {
    }
}
Note: This local class is inherited from projectModel, so the original code can be reused.



3.2 Create a program called in ext/model/. For example, project/ext/model/gantt.php


public function createRelationOfTasks($projectID)
{ $this->loadExtension('gantt')->createRelationOfTasks($projectID);
}
Through the loadExtension(), we invoke the method in the gantt.class.php invoked in 3.1. In this way, only the class extensions have to be encrypted, so the problem of the conflicting encrypted file is solved.



Write a Comment
Comment will be posted after it is reviewed.