yii2

Yii2 merge queries like cdbcriteria in yii1

拥有回忆 提交于 2019-12-23 12:26:01
问题 I have a problem regarding merge of multiple queries. In Yii 1.x you could merge a CDbCriteria with $criteria->merge($otherCriteria) How can I achieve the same nested conditions etc with queries in Yii2? Edit: Let's say I want separate queries to form subqueries. And after all subqueries are done I want to merge them together to the one big query. 回答1: There is no CDbCriteria concept in Yii2 anymore. Instead you can refer to the following classes: http://www.yiiframework.com/doc-2.0/yii-db

Yii2 dropdown multiple selected values

人盡茶涼 提交于 2019-12-23 12:01:48
问题 I used Yii2 multiple selection dropdown , it is working fine at create but not showing me selected values on update! Form: $form->field($model, 'categories[]') ->dropDownList($model->CategoryDropdown, [ 'multiple'=>'multiple' 'class'=>'chosen-select input-md required', ] )->label("Add Categories"); Model: public function getCategoryDropdown() { $listCategory = Category::find()->select('ID,name') ->where(['is_subcategory' => 'Yes']) ->andWhere(['status' => 'active','approved' => 'active']) -

Using find() in Active Record with multiple where clause

自闭症网瘾萝莉.ら 提交于 2019-12-23 12:00:03
问题 I want to divide (using brackets) following Active Record query in 3 groups. First group would be from first "Where" clause to last "orWhere". Second and third would be using "andWhere". Please give me suggestions about how can I use brackets to separate all 3 sections. $query = Book::find() ->where('book_name LIKE :book_name', array(':book_name' => '%'.$book_name.'%')) ->orWhere('book_category LIKE :book_category', array(':book_category' =>'%'.$category.'%')) ->orWhere('finance_subcategory

Codeception not found Yii

邮差的信 提交于 2019-12-23 11:57:34
问题 I have tried to run unit tests in Yii2 project with using codecept. And received next error: PHP Fatal error: Class 'Yii' not found in /var/www/html/mysite/vendor/codeception/codeception/src/Codeception/Module/Yii2.php on line 77 It's my unit.suite.yml content: # Codeception Test Suite Configuration # suite for unit (internal) tests. # RUN `build` COMMAND AFTER ADDING/REMOVING MODULES. class_name: CodeGuy modules: enabled: [CodeHelper, Yii2] config: Yii2: configFile: 'config/web.php' Does

SELECT sub-query with WHERE condition in Yii2 find() / QueryBuilder

谁说我不能喝 提交于 2019-12-23 11:52:39
问题 I was able to find simple examples of sub-query building, but am unable to figure out nor find the solution when I need to include a WHERE condition. I am trying to simulate the following statement... SELECT ParentTable.*, (SELECT MAX(ChildTable.NumberField) FROM ChildTable WHERE ChildTable.FK_Id = ParentTable.Id) FROM ParentTable Guess I'd need something like... $query = ParentClass::find() ->addSelect( ChildClass::find() ->where('childTable.fk_id=parentTable.id') ->max('childTable.field1')

Not able to get Yii2 object data returned as Json

房东的猫 提交于 2019-12-23 11:46:07
问题 I am new to the Yii2 framework and PHP. When I try to retrieve a model data from the server as json , I am getting an empty result. But, when I use var_dump , I am getting a non-empty result. Controller class code: public function actionIndex() { $client = new Client(); $client->name = "ajith"; echo json_encode($client); } Model class code: class Client extends \yii\mongodb\ActiveRecord { public static function collectionName() { return ['gym', 'client']; } public function attributes() {

Yii2 isGuest giving exception in console application

二次信任 提交于 2019-12-23 10:57:51
问题 In console application when I used Yii::$app->user->isGuest it is giving the below exception: Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown prop erty: yii\console\Application::user' I even tried adding the user in components array in config file. But it didn't worked. Any idea what am I doing wrong? 回答1: In Console application Yii->$app->user does not exist. So, you need to configure user component in config\console.php . like as, config\console.php 'components'

Yii2 isGuest giving exception in console application

ぃ、小莉子 提交于 2019-12-23 10:56:08
问题 In console application when I used Yii::$app->user->isGuest it is giving the below exception: Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown prop erty: yii\console\Application::user' I even tried adding the user in components array in config file. But it didn't worked. Any idea what am I doing wrong? 回答1: In Console application Yii->$app->user does not exist. So, you need to configure user component in config\console.php . like as, config\console.php 'components'

Connect yii2 with mongodb

随声附和 提交于 2019-12-23 10:54:25
问题 I am new to Yii2. Can anyone tell me how to configure YII2 with mongodb and how to establish connection between YII2 and mongodb? I have tried to download the mongodb package from git hub and tried to run the following command php composer.phar require --prefer-dist yiisoft/yii2-mongodb "*" In the command prompt inside the root folder where I have installed Yii2 but I get the following error Your requirements could not be resolved to an installable set of packages. Problem 1 - yiisoft/yii2 2

Multiple Databases Connection on yii2

余生颓废 提交于 2019-12-23 10:08:18
问题 I'm trying to use multiple database connection on yii2 framework. Under my db.php file inside the config folder, I have this piece of code: return [ 'class' => 'yii\db\Connection', 'components' => [ 'db1' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=new', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8', ], 'db2' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=old', 'username' => 'root', 'password' => 'password',