yii2的Model类部分详解

我们公司把每个查询的方法封装起来,如下是基本固定写法:

public function getAll($ids, $select = 'SKU_ID,PROD_ID,SKU_SPEC_ID,SKU_SPEC_NAME,PROD_CODE,SKU_CODE,SKU_IMG_URL,STOCK_BALANCE,MARKET_PRICE,SALE_PRICE,SKU_TYPE,SKU_STATUS,SORT_RANK,CREATE_DATE,YW_P_P_D_IDS,SALED_AMOUNT', $isArray = true)
{
    $result = array();
    if ($isArray) {
        $result = $this->find()->select($select)->where(['IN','SKU_ID',$ids])->andWhere(['SKU_STATUS'=>1])->asArray()->all();
    } else {
       //因为该句没有asArray(),所以查询出来是对象
        $result = $this->find()->select($select)->where(['IN','SKU_ID',$ids])->andWhere(['SKU_STATUS'=>1])->all();
    }
    return $result;
}

在此我想说的就是控制器类的查询:

public function actionIndex()
{
    $skuModel=new Sku;
    $ids= array(136982,136686,136694,136695);
    $id=136982;
    $skus=$skuModel->getAll($ids);
    //平时写的查询不加参数false,所以都会走model的if,在确定是单条的时候应该加上false,这样缩短查询时间
    $skuInfo=$skuModel->getAll($id,'SKU_ID',false);

    print_r($skus);
    echo "
"; print_r($skuInfo); }
查询oracle数据库的日期
public function getScoreByUserId($id, $select =['U_SCORE_ID','USER_ID',"to_char(CREATE_DATE,'yyyy-mm-dd hh24:mi:ss') AS CREATE_DATE"], $isArray = true) 

你可能感兴趣的:(yii2的Model类部分详解)