Child Service [type] is not find in appadmin\local\local_services\Promotion, you must config it

bug问题 · zhuzhi1 · 于 6年前 发布 · 1892 次阅读

我调用服务:Yii::$service->promotion->type时报错:Child Service [type] is not find in appadmin\local\local_services\Promotion, you must config it

common/config/fecshop_local_services/Promotion.php中配置子服务代码:return [

'promotion' => [
    'class' => 'appadmin\local\local_services\Promotion',
],
'childService' => [
        'type'=>[
            'class'=>"appadmin\local\local_services\Promotiontype"
        ],

    ],

];

服务appadmin\local\local_services\Promotiontype.php中代码: <?php /**

  • Created by PhpStorm.
  • User: xi_qing
  • Date: 2017/10/24
  • Time: 21:34 */

namespace appadmin\local\local_services;

use fecshop\services\Service; use Yii;

class Promotiontype extends Service {

protected $_typeModelName = '\appadmin\local\local_models\mysqldb\Type';
protected $_typeModel;
public function __construct(){
    list($this->_typeModelName,$this->_typeModel) = \Yii::mapGet($this->_typeModelName);
}
/**
 * 得到shop_config 表的主键(mysql表)
 */
protected function actionGetPrimaryKey()
{
    return 'id';
}
/**
 * 通过主键,得到shop_config model
 */
protected function actionGetByPrimaryKey($val)
{
    if ($val) {
        $one = $this->_typeModel->findOne($val);
        $primaryKey = $this->getPrimaryKey();
        if ($one[$primaryKey]) {
            return $one;
        } else {
            return new $this->_typeModelName();
        }
    }
}

/**
 * @property $filter|array
 * get  collection by $filter
 * example filter:
 * [
 *      'numPerPage'    => 20,
 *      'pageNum'       => 1,
 *      'orderBy'   => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
 *      'where'         => [
 *          ['>','price','1'],
 *          ['<','price','10'],
 *          ['sku' => 'uk10001'],
 *      ],
 *  'asArray' => true,
 * ]
 * 通过上面的filter数组,得到过滤后的用户数据列表集合。
 */
protected function actionColl($filter = '')
{
    $query=$this->_typeModel->find();
    $query->select(array('*',
        new \yii\db\Expression("case ifnull(enabled, 0) when 0 then '禁用' else '启用' end AS enable_cn")));
    $query->from($this->_typeModel->tableName());
    $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
    //var_dump($query->all());exit;
    return [
        'coll' => $query->all(),
        'count'=> $query->limit(null)->offset(null)->count(),
    ];
}

/**
 * @property $param | array ,用户的数组
 * 数据格式如下:
 * ['email' => 'xxx', 'password' => 'xxxx','firstname' => 'xxx','lastname' => 'xxx',]
 * 保存shop_product_type信息
 */
protected function actionSave($param)
{
    $time = time();
    $primaryKey = $this->getPrimaryKey();
    $primaryVal = isset($param[$primaryKey]) ? $param[$primaryKey] : '';
    if ($primaryVal) {
        $model = $this->_typeModel->findOne($primaryVal);
        if (!$model) {
            Yii::$service->helper->errors->add('站点 '.$this->getPrimaryKey().' 不存在');
            return;
        } else {
            $o_one = $this->_typeModel->find()
                ->where("domain=:domain or site_name=:site_name",[':domain' =>$param['domain'],":site_name"=>$param['site_name']])
                ->andWhere(['!=', $primaryKey, $primaryVal])
                ->one();
            if ($o_one[$primaryKey]) {
                Yii::$service->helper->errors->add('站点名称或者站点域名已存在!');
                return;
            }
        }
    } else {
        $o_one = $this->_typeModel->find()
            ->where("domain=:domain or site_name=:site_name",[':domain' =>$param['domain'],":site_name"=>$param['site_name']])
            ->one();
        if ($o_one[$primaryKey]) {
            Yii::$service->helper->errors->add('站点名称或者站点域名已存在!');
            return;
        }
        $model = new $this->_typeModelName();
        $model->created_at = time();

// if (isset(Yii::$app->user)) { // $user = Yii::$app->user; // if (isset($user->identity)) { // $identity = $user->identity; // $person_id = $identity['id']; // $model->created_person = $person_id; // } // }

    }
    $model->updated_at = time();
    $model      = Yii::$service->helper->ar->save($model, $param);
    $primaryVal = $model[$primaryKey];
    return $primaryVal;
}

/**
 * 删除产品类型
 * @property $ids | Array or String
 */
protected function actionRemove($ids)
{
    if (!$ids) {
        Yii::$service->helper->errors->add('请选择要删除的站点信息!');
        return false;
    }
    if (is_array($ids) && !empty($ids)) {
        $innerTransaction = Yii::$app->db->beginTransaction();
        try {
            foreach ($ids as $id) {
                $model = $this->_typeModel->findOne($id);
                if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
                    $model->delete();
                } else {
                    //throw new InvalidValueException("ID:$id is not exist.");
                    Yii::$service->helper->errors->add("站点删除错误:ID $id 不存在.");
                    $innerTransaction->rollBack();
                    return false;
                }
            }
            $innerTransaction->commit();
        } catch (Exception $e) {
            Yii::$service->helper->errors->add('站点删除错误: 事务回滚'.$e->getMessage());
            $innerTransaction->rollBack();
            return false;
        }
    } else {
        $id = $ids;
        $model = $this->_typeModel->findOne($id);
        if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
            $innerTransaction = Yii::$app->db->beginTransaction();
            try {
                $model->delete();
                $innerTransaction->commit();
            } catch (Exception $e) {
                Yii::$service->helper->errors->add('站点删除错误: 事务回滚'.$e->getMessage());
                $innerTransaction->rollBack();
            }
        } else {
            Yii::$service->helper->errors->add("站点删除错误:ID $id 不存在.");
            return false;
        }
    }
    return true;
}

}

共收到 3 条回复
Fecmall#16年前 0 个赞

先用markdown格式把代码理好

Fecmall#26年前 0 个赞

你的配置有问题,看这个例子: Yii::$service->cms->article

return [
    'cms' => [
        'class' => 'fecshop\services\Cms',
        // 子服务
        'childService' => [
            'article' => [
                'class'            => 'fecshop\services\cms\Article',
                'storage' => 'ArticleMysqldb', // ArticleMysqldb or ArticleMongodb.
            ],

            'staticblock' => [
                'class'    => 'fecshop\services\cms\StaticBlock',
                'storage'    => 'StaticBlockMongodb', // mysqldb or mongodb.
            ],
        ],
    ],
];

你在看看你的配置:

return [
	'promotion' => [
    	'class' => 'appadmin\local\local_services\Promotion',
	],
	'childService' => [
        'type'=>[
            'class'=>"appadmin\local\local_services\Promotiontype"
        ],
    ],
]
Fecmall#36年前 1 个赞

建议你的servcies和子services的文件结构,参考fecshop的方式存放,这样方便维护,参看上面配置中class的值

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics