📜 ⬆️ ⬇️

LiveStreet and ORM

The release of version 0.5 for me was more than adding an activity page and a topic ribbon from subscribed blogs. The new version implemented ORM and ActiveRecord . Together they provide the most powerful toolkit for the developer, eliminating the heap of the same type of code that you had to write each time you develop a plugin. The same forum, which will be discussed in the article, has lost 2177 lines of code after the update. In this article I want to go deep into ORM and AR using the example of creating a plug-in for LiveStreet.

Almost a year ago, the user runawayed published an article about innovations in the trunk version.

I was repelled from that article, but it is only the introductory part of that toolkit. There is not a word about the cache, page-by-page, tree-like array. I will try to provide as much information as possible to the reader without overloading him. Let's start?

I will consider everything on the example of the forum plugin. First we need to create the skeleton of the plugin:
')
plugin

The source files can be viewed on the githaba; I will not dwell on the description of the plugin.

Now create a module named forum.

plugin

We need to describe the module as ModuleORM, and, in the Init method, we need to inherit the parent of the Init method from a regular module:

class PluginForum_ModuleForum extends ModuleORM { public function Init() { parent::Init(); } } 


Before describing entities, it is necessary to consider all the connections between them in order to make it easier to manipulate through getters:

relations

And describe the created entities:

Category.entity.class.php

 class PluginForum_ModuleForum_EntityCategory extends EntityORM {} 


Forum.entity.class.php

 class PluginForum_ModuleForum_EntityForum extends EntityORM { protected $aRelations = array( 'category'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'PluginForum_ModuleForum_EntityCategory','category_id'), 'user'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'ModuleUser_EntityUser','user_id'), 'topic'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'PluginForum_ModuleForum_EntityTopic','topic_id'), 'post'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'PluginForum_ModuleForum_EntityPost','post_id'), ); } 


Topic.entity.class.php

 class PluginForum_ModuleForum_EntityTopic extends EntityORM { protected $aRelations = array( 'user'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'ModuleUser_EntityUser','user_id'), 'post'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'PluginForum_ModuleForum_EntityPost','post_id'), 'forum'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'PluginForum_ModuleForum_EntityForum','forum_id'), ); } 


Post.entity.class.php

 class PluginForum_ModuleForum_EntityPost extends EntityORM { protected $aRelations = array( 'user'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'ModuleUser_EntityUser','user_id'), 'topic'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'PluginForum_ModuleForum_EntityTopic','topic_id'), 'forum'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'PluginForum_ModuleForum_EntityForum','forum_id'), ); } 


Read.entity.class.php

 class PluginForum_ModuleForum_EntityRead extends EntityORM {} 


Now all these can be manipulated without a description of the classes in the plug-in module, for example:

 $this->PluginForum_ModuleForum_GetCategoryItemsAll(); //    


 $this->PluginForum_ModuleForum_GetForumItemsByCategoryId($oCategory->getId()); //     ID  


 $this->PluginForum_ModuleForum_GetTopicItemsByForumId($oForum->getId(),array('#page' => array(1,15), '#cache'=>'')); /** *           : * array( * 'collection' => array of objects * 'count' => integer * ); *  array('#page' => array(' ', '  ')) *  array('#cache' => '')     */ 


The #cache filter can also take the following parameters:
 array( '#cache' => array( 'keys', 'tags', 'time' ) ); 


There are several types of links:
 protected $aRelations = array( 'entity'=>array(EntityORM::RELATION_TYPE_BELONGS_TO,'ModuleSome_EntitySome','field_id'), //      . ,   prefix_forum_topic    ID' ,      ID    'entity'=>array(EntityORM::RELATION_TYPE_HAS_MANY,'ModuleSome_EntitySome','field_id'), //    'entity'=>array(EntityORM::RELATION_TYPE_HAS_ONE,'ModuleSome_EntitySome','field_id'), // , ,    ( )  ,       () ort 'entity'=>array(EntityORM::RELATION_TYPE_MANY_TO_MANY,'ModuleSome_EntitySome','field_id'), //      EntityORM::RELATION_TYPE_TREE //  ,    parent_id   ); 


After describing the entities, we will have the following methods available:
 /** *   [name]    */ /** *    */ Add(); Update(); Save(); //   Update()  Add(),        ,      () ort Delete(); Reload(); // : $oParam->setTitle('--'); $oParam->Update(); /** *    /  */ ShowColumnsFrom[Table](); LoadTreeOf[Entity](); Get[Entity]ItemsBy[Filter, Array, JoinTable, Gte, Lte, Gt, Lt, Like, In](); Get[Entity]All(); Get[Entity]ItemsAll(); /** * ,     tree */ GetChildrenOf[Entity](); GetParentOf[Entity](); GetDescendantsOf[Entity](); GetAncestorsOf[Entity](); // : $this->ModuleHabr_GetTopicItemsAll(); /** *  -   singleton Engine,     */ get[Column](); // : $oParam->getTitle(); 


Tables in the database should be named as follows: prefix_ <module-name> _ <entity-name> (or write their names in the config), and if the entity name matches the module name, it is enough to name the table like this: prefix_ <module- name>. Fields also have their own standards: <entity-name> _ <field-name>, or simply <field-name>.

That's all, gentlemen, thank you for your attention. I remind you that fresh source code of the forum can be viewed in git'e .

UPD : Made corrections in the article, thanks ort 'u for his comments .

Source: https://habr.com/ru/post/129342/


All Articles