Extension Of Model

2022-05-09 15:53:02
Kelsea
1901
Last edited by Hongyan on 2022-05-10 16:46:52
Share links

Extension Of Model

Model is mainly used to search and update various data. There are three ways to expand the model, one is to directly add or override methods, the other is to implement it through hooks, and the third is to completely define a new class. The following describes the mechanism of each implementation.

1. Directly add or override methods

Whether adding a method or overwriting an existing method, the module directory is created under the extension/custom directory, and a corresponding file named method is created under the ext/model/ directory. For example, if we plan to add a new method to misc's model, called foo, we only need to create foo.php under extension/custom/misc/ext/model/, and the code is as follows:

public function foo()
{ 
     return 'foo';
} 

It should be noted that the definition here does not contain the declaration of the class, it is just a declaration of a method. When ZenTao framework is executed, it will automatically replace the code in foo.php in the extension directory with the code in the foo method in misc/model.php. If it is a new method, it will be appended to the code of misc/model.php, and finally, a merged model class file will be generated.

2. Extend by hooks

In addition to adding or overriding methods, it can also be extended through hooks. All hooks are stored in the ext/model/hook directory, and the file naming rules are: method name. extension. php

 

For example, I extend the hook of the hello method in the misc module, create a hello.abc.php file under misc/ext/model/hook/, and then implement the code in it.

 

ZenTao framework will merge all the hook code of a method into the final code.

 

However, this method has many limitations and unexpected behavior, and it is not recommended to use it.

3. Extend by class

In addition to the above two ways, there is a third way to extend the model, which is to put all the extensions in a class, and then load them through the framework's loadExtension() method. This method is mainly to solve the problem of conflicting encrypted files. When the zentaoPHP framework processes the extension of the model, it will merge the corresponding code, but the problems come after. If you extend by the first method, which is encrypting exe/model/abc.php, it will conflict with other open-source model extensions. Without encryption, the author's code cannot be protected. In order to solve this problem, we specially implemented a third extension method, which we will explain below:

3.1 Create a class in the ext/model/class directory of the corresponding module under extension/custom/, and the file naming rule is extension name.class.php.

For example, our Gantt chart plugin is defined as project/ext/model/class/gantt.class.php, which defines various codes. The class name rule is {plugin name}{module name}, the first letter of the module name is capitalized, such as ganttProject.

class ganttProject extends projectModel
{
    public function createRelationOfTasks($projectID)
    {
    }
} 

Note: This local class is inherited from projectModel, so that the original code can be reused.

3.2 Create the called program in ext/model/. For example, extension/custom/project/ext/model/gantt.php

public function createRelationOfTasks($projectID)
{
    $this->loadExtension('gantt')->createRelationOfTasks($projectID);
} 

The method in gantt.class.php called in 3.1 is called through the loadExtension() method.

 

In this way, the ZenTao framework only needs to encrypt all class extensions, and the problem of conflicting encrypted files can be solved.

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