LiveStreetとORM

バージョン0.5のリリースは、私にとっては、活動ページと署名付きブログからのトピックフィードを追加する以上のものでした。 新しいバージョンはORMActiveRecordを実装しています。 これらが一緒になって、開発者に強力なツールキットを提供し、プラグインを開発するたびに記述する必要があった同じタイプのコードのヒープを排除します。 更新後に記事で説明する同じフォーラムでは、2177行のコードが失われました 。 この記事では、例としてLiveStreet用のプラグインを作成することにより、ORMとARをより深く掘り下げたいと思います。

ほぼ1年前に、 runawayedはtrunkバージョンの革新に関する記事を投稿しまし

私はその記事から始めましたが、それはそのツールキットの紹介にすぎません。 配列のキャッシュ、ページング、ツリーのような形式についての言葉はありません。 不必要にロードすることなく、できるだけ多くの情報を読者に提供するようにします。 始めましょうか?

フォーラムプラグインの例ですべてを検討します。 まず、プラグインスケルトンを作成する必要があります。

plugin

ソースファイルはgithubで表示できます。プラグインの説明については触れません。

次に、forumというモジュールを作成します。

plugin

モジュールをModuleORMとして記述する必要があります。また、Initメソッドでは、通常のモジュールからInitメソッドの親を継承する必要があります。

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


エンティティを説明する前に、エンティティ間のすべての接続を考慮する必要があります。これにより、ゲッターを介した操作が容易になります。

relations

そして、作成されたエンティティを説明します。

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 {} 


これで、プラグインモジュールでクラスを記述せずに、これらすべてを操作できるようになりました。例:

 $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' => '')     */ 


#cacheフィルターは、次のパラメーターを取ることもできます。
 array( '#cache' => array( 'keys', 'tags', 'time' ) ); 


関係にはいくつかのタイプがあります。
 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   ); 


エンティティを説明した後、次のメソッドを使用できます。
 /** *   [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(); 


データベース内のテーブルは次のように命名する必要があります:prefix_ <module-name> _ <entity-name>(または構成に名前を書き込む)が、エンティティの名前がモジュールの名前と一致する場合は、次のようにテーブルに名前を付けます:prefix_ <module-名前>。 フィールドにも独自の標準があります:<entity-name> _ <field-name>、または単に<field-name>。

紳士の皆さん、ご清聴ありがとうございました。 最新のフォーラムソースはgitで表示できることを思い出してください。

UPD :記事に修正を加えました。彼のコメントに感謝します

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


All Articles