yii2

yii2使用原生语句执行数据库操作yii\db\Query和yii\db\Command

别说谁变了你拦得住时间么 提交于 2019-12-07 20:39:46
某些情况下我们并不需要启用比较重的AR去操作数据库,这里Yii2也为我们提供了 yii\db\Query 查询构造器 只能创建 查询 命令 yii\db\Command sql命令工具 可以创建 curd 操作 两个工具类来满足此类需求,还有个 yii\db\QueryBuilder 感觉用起来有些别扭,感兴趣的自己去看吧 yii\db\Query 查询构造器可以比较灵活的构建出各种查询命令(不过我觉得灵活是对不太熟悉sql的人来说,因为我比较喜欢写sql,所以我没觉得有多灵活....但Yii2总归还是很不错的) $query = new \yii\db\Query(); $query->select(['`user`.`id`', '`user`.`username`, `order`.`info`']) ->distinct(false) ->from('{{%_user}} as `user`') ->leftJoin(['order' => '{{%_order}}'], '`order`.`uid` = `user`.`id`') ->where(['or', ['>=', '`user`.`id`', 20], ['<=', '`user`.`id`', 10]]) ->andWhere(['not', ['`user`.`username`' => null]]) -

How to connect 3 tables in yii2 and display in Gridview then make sorting work correctly

二次信任 提交于 2019-12-07 20:38:23
问题 I have used the gii tool to create crud application. I have 3 tables the tbl_targetcities, lib_cities, and lib_provinces. I was able to connect lib_cities to tbl_targetciteis but not the lib_provinces. And also the sorting of city / Municipality does not work. It seems that it sorts according ti the ID. tbl_target_cities lib_cities lib_provinces sample View So far here is my relation in the model. public function getCityName() { return $this->hasOne(LibCities::className(),['city_code'=>'city

Yii2: isAjax returns false

浪子不回头ぞ 提交于 2019-12-07 20:31:55
问题 Im developing a search bar in yii2 using ajax. The problem is the Yii::$app->request->isAjax property always returns false This is my action: public function actionAjaxsearch() { if(Yii::$app->request->isAjax) { $keywords = Yii::$app->request->queryParams; Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return [ 'data' => $keywords, 'code' => 200 ]; } else throw new \yii\web\HttpException(404, 'Page not found.'); And this is my script: $('#search-box').keyup(function( event ){

Yii 2 static pages

孤街醉人 提交于 2019-12-07 19:44:36
问题 I can't show static pages. Try do it as described in doc here - http://stuff.cebe.cc/yii2-guide.pdf (on page 100) but when I enable prettyurl, it doesn't work. Added in urlManager rules: 'urlManager' => array( 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => array( '' => 'site/index', 'login' => 'site/login', 'contacts' => 'site/contact', '<view:(break)>'=>'/site/page?&view=<view>', ), ), then in SiteController added: public function actions() { return [ ... 'page' => [ 'class

Best practice for setting non-normalized columns in Yii2

半世苍凉 提交于 2019-12-07 19:16:37
问题 Question to all Yii2 normalization geeks out there. Where is the best place to set non-normalized columns in Yii2? Example, I have models Customer , Branch , CashRegister , and Transaction . In a perfect world, and in a perfectly normalized Database, our Transaction model would have only the cashregister_id , The CashRegister would store branch_id , and the Branch would store customer_id . However due to performance issues, we find ourselves obliged sometimes though to have a non-normalized

Yii2 Date compare validation

被刻印的时光 ゝ 提交于 2019-12-07 19:08:54
问题 I have model rules() like below, but it is not working. It is showing error message always. public function rules() { return [ [['start_date','end_date'], 'date', 'format' => 'php:F d Y'], ['start_date','compare','compareAttribute'=>'end_date','operator'=>'<'], ['end_date','compare','compareAttribute'=>'start_date','operator'=>'>'], ]; } 回答1: public function rules() { return [ [['start_date','end_date'], 'date', 'format' => 'php:F d Y'], ['start_date','validateDates'], ]; } public function

Unable to render php variables from controller to view in yii2

梦想的初衷 提交于 2019-12-07 18:05:12
问题 I am new to yii2 . I am passing two variables into my create view which is partially rendered from _form . But somehow I am unable to pass these variables, and getting error. What i have done? In my action controller I have declared two global variables global $the_meter_id; global $the_meter_msn; Then in my else case condition I am getting the current id and current serial number. else{//$meter_alive condition else \Yii::$app->getSession()->setFlash('error', ' <div class="alert alert-error

Yii 2 ActiveForm form field how to implement “select all” option in checkboxlist?

我们两清 提交于 2019-12-07 17:56:55
问题 Yii 2 ActiveForm form field how to implement "select all" option in checkboxlist? <?php $form = ActiveForm::begin([ 'id' => 'form-id', 'type' => ActiveForm::TYPE_HORIZONTAL, 'options' => ['class' => 'well'], ]); ?> <?php echo $form->field($model, 'MY_DESC', ['template' => "{label}\n{input}\n{hint}\n{error}"]) ->label(false) ->checkboxList($mylist, ['separator' => '<hr>']); ?> <?= Html::submitButton('submit', ['class' => 'btn btn-primary']) ?> <?php ActiveForm::end(); ?> 回答1: 1) Add checkbox

Yii2 data provider for rest api call [closed]

可紊 提交于 2019-12-07 17:18:35
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . Currently I'm using the yii2 array dataprovider for listing the datas from rest api. We have more than 10k records. Each rest api call can get only maximum 100 records , If we want more i need to give the limit and offset for this rest api call. Is there any specific rest api dataprovider in yii2? else how can i

Yii2 best practices translating dynamic content

北战南征 提交于 2019-12-07 16:48:53
问题 Can anyone share own experience and best practices implementing multilingual sites with Yii2? I want translate user input that is stored in database. For example article, that may have its name in three different languages, body and some translatable attributes as well. Does Yii2 have built in features to translate the dynamic content? Or should I use third party extensions like these ones below: https://github.com/creocoder/yii2-translateable https://github.com/LAV45/yii2-translated-behavior