Article.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\cms;
  10. use fecshop\services\cms\article\ArticleMongodb;
  11. use fecshop\services\cms\article\ArticleMysqldb;
  12. use fecshop\services\Service;
  13. use Yii;
  14. /**
  15. * Cms Article services. 文字条款类的page单页。
  16. * @author Terry Zhao <2358269014@qq.com>
  17. * @since 1.0
  18. */
  19. class Article extends Service
  20. {
  21. /**
  22. * $storagePrex , $storage , $storagePath 为找到当前的storage而设置的配置参数
  23. * 可以在配置中更改,更改后,就会通过容器注入的方式修改相应的配置值
  24. */
  25. public $storage; // = 'ArticleMysqldb'; // ArticleMongodb | ArticleMysqldb 当前的storage,如果在config中配置,那么在初始化的时候会被注入修改
  26. /**
  27. * 设置storage的path路径,
  28. * 如果不设置,则系统使用默认路径
  29. * 如果设置了路径,则使用自定义的路径
  30. */
  31. public $storagePath = '';
  32. protected $_article;
  33. public function init()
  34. {
  35. parent::init();
  36. // 从数据库配置中得到值, 设置成当前service存储,是Mysqldb 还是 Mongodb
  37. $config = Yii::$app->store->get('service_db', 'article_and_staticblock');
  38. $this->storage = 'ArticleMysqldb';
  39. if ($config == Yii::$app->store->serviceMongodbName) {
  40. $this->storage = 'ArticleMongodb';
  41. }
  42. $currentService = $this->getStorageService($this);
  43. $this->_article = new $currentService();
  44. }
  45. /**
  46. * Get Url by article's url key.
  47. */
  48. //public function getUrlByPath($urlPath){
  49. //return Yii::$service->url->getHttpBaseUrl().'/'.$urlKey;
  50. //return Yii::$service->url->getUrlByPath($urlPath);
  51. //}
  52. /**
  53. * get artile's primary key.
  54. */
  55. protected function actionGetPrimaryKey()
  56. {
  57. return $this->_article->getPrimaryKey();
  58. }
  59. /**
  60. * get artile model by primary key.
  61. */
  62. protected function actionGetByPrimaryKey($primaryKey)
  63. {
  64. return $this->_article->getByPrimaryKey($primaryKey);
  65. }
  66. /**
  67. * @param $urlKey | String , 对应表的url_key字段
  68. * 根据url_key 查询得到article model
  69. */
  70. protected function actionGetByUrlKey($urlKey)
  71. {
  72. return $this->_article->getByUrlKey($urlKey);
  73. }
  74. /**
  75. * 得到category model的全名.
  76. */
  77. protected function actionGetModelName()
  78. {
  79. return get_class($this->_article);
  80. }
  81. /**
  82. * @param $filter|array
  83. * get artile collection by $filter
  84. * example filter:
  85. * [
  86. * 'numPerPage' => 20,
  87. * 'pageNum' => 1,
  88. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  89. * 'where' => [
  90. * ['>','price',1],
  91. * ['<=','price',10]
  92. * ['sku' => 'uk10001'],
  93. * ],
  94. * 'asArray' => true,
  95. * ]
  96. */
  97. protected function actionColl($filter = '')
  98. {
  99. return $this->_article->coll($filter);
  100. }
  101. /**
  102. * @param $one|array , save one data .
  103. * @param $originUrlKey|string , article origin url key.
  104. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  105. */
  106. protected function actionSave($one, $originUrlKey)
  107. {
  108. return $this->_article->save($one, $originUrlKey);
  109. }
  110. protected function actionRemove($ids)
  111. {
  112. return $this->_article->remove($ids);
  113. }
  114. }