Переглянути джерело

feat: EK-2086: Member Portal : Dashboard Article and Ad only show self country.

kevin 3 місяців тому
батько
коміт
d24d1d64c1

+ 2 - 2
backendApi/modules/v1/controllers/AdController.php

@@ -96,10 +96,10 @@ class AdController extends BaseController
             $placeholders = [];
             foreach ($countryIds as $key => $id) {
                 $paramName = ':countryId_' . $key;
-                $placeholders[] = 'FIND_IN_SET(' . $paramName . ', AD.COUNTRY_ID)';
+                $placeholders[] = $paramName;
                 $params[$paramName] = $id;
             }
-            $condition .= ' AND (' . implode(' OR ', $placeholders) . ')';
+            $condition .= ' AND AD.COUNTRY_ID IN (' . implode(',', $placeholders) . ')';
         }
 
         $obj = new AdIndexList();

+ 4 - 15
backendApi/modules/v1/models/lists/ad/AdIndexList.php

@@ -25,24 +25,13 @@ class AdIndexList extends \common\libs\dataList\DataList implements DataListInte
     {
         $this->condition .= '';
         $this->listData = Ad::lists($this->condition, $this->params, [
-            'select' => 'AD.*',
+            'select' => 'AD.*,ADC.NAME AS COUNTRY_NAME, ADC.CODE AS COUNTRY_CODE',
             'from' => Ad::tableName().' AS AD',
+            'join' => [
+                ['INNER JOIN', Countries::tableName() . ' AS ADC', 'ADC.ID=AD.COUNTRY_ID'],
+            ],
             'orderBy' => 'AD.STATUS DESC,AD.SORT DESC,AD.CREATED_AT ASC',
         ]);
-
-        // COUNTRY_ID是逗号分割的ID,需要提取国家名称、CODE
-        foreach($this->listData['list'] as $key => $row){
-            $countryIds = explode(',', $row['COUNTRY_ID']);
-            $countryNames = [];
-            $countryCodes = [];
-            foreach($countryIds as $countryId){
-                $country = Countries::findOneAsArray('ID=:ID', [':ID'=>$countryId], 'NAME,CODE');
-                $countryNames[] = $country['NAME'];
-                $countryCodes[] = $country['CODE'];
-            }
-            $this->listData['list'][$key]['COUNTRY_NAME'] = implode(',', $countryNames);
-            $this->listData['list'][$key]['COUNTRY_CODE'] = implode(',', $countryCodes);
-        }
     }
 
     public function getColumn(){

+ 7 - 6
common/models/forms/AdForm.php

@@ -25,7 +25,7 @@ class AdForm extends Model
     public $content;
     public $sort;
     public $status;
-    public $countries;
+    public $countryId;
 
     private $_adModel;
 
@@ -42,8 +42,8 @@ class AdForm extends Model
     public function rules()
     {
         return [
-            [['id', 'title', 'image', 'type', 'lid', 'content', 'sort', 'status', 'countries'], 'trim'],
-            [['id', 'title', 'image', 'type', 'lid', 'content', 'sort', 'status', 'countries'], 'required'],
+            [['id', 'title', 'image', 'type', 'lid', 'content', 'sort', 'status', 'countryId'], 'trim'],
+            [['id', 'title', 'image', 'type', 'lid', 'content', 'sort', 'status', 'countryId'], 'required'],
 //            [['image'], 'url'],
             [['id'], 'exist', 'targetClass'=>Ad::class, 'targetAttribute'=>'ID'],
             [['id'], 'initModel'],
@@ -64,6 +64,7 @@ class AdForm extends Model
             'content' => 'External link or article ID', // 链接地址或文章ID
             'sort' => 'Order', // 排序
             'status' => 'State', // 状态
+            'countryId' => 'Country', // 国家
         ];
     }
 
@@ -75,8 +76,8 @@ class AdForm extends Model
     {
         $parentScenarios =  parent::scenarios();
         $customScenarios = [
-            'add' => ['title', 'image', 'type', 'lid', 'content'],
-            'edit' => ['id', 'title', 'image', 'type', 'lid', 'content'],
+            'add' => ['title', 'image', 'type', 'lid', 'content', 'countryId'],
+            'edit' => ['id', 'title', 'image', 'type', 'lid', 'content', 'countryId'],
             'sort' => ['id','sort'],
             'status' => ['id','status'],
         ];
@@ -141,7 +142,7 @@ class AdForm extends Model
             $model->TYPE = $this->type;
             $model->LID = $this->lid;
             $model->CONTENT = $this->content;
-            $model->COUNTRIES = $this->countries;
+            $model->COUNTRY_ID = $this->countryId;
             $model->STATUS = 1;
             if(!$model->save()){
                 throw new Exception(Form::formatErrorsForApi($model->getErrors()));