我调用服务: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 /**
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;
}
}
你的配置有问题,看这个例子: 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"
],
],
]