Questions tagged [yii2-model]

Use this tag if your question relates to the Yii2-model-class or one of its descendants.

This tag should be used whenever your question relates to the \yii\base\Model class or one of its descendants like \yii\db\ActiveRecord.

304 questions
17
votes
6 answers

Yii2: How to set default attribute values in ActiveRecord?

This may seem like a trivial question, however all of the obvious solutions that I can think of have their own flaws. What we want is to be able to set any default ActiveRecord attribute value for new records only, in a way that makes it readable…
mae
  • 12,340
  • 8
  • 28
  • 40
16
votes
10 answers

model->save() Not Working In Yii2

Previously, I was not using $model->save() function for inserting or updating any data. I was simply using createCommand() to execute query and it was working successfully. But, my team members asked me to avoid createCommand() and use…
Nana Partykar
  • 10,175
  • 8
  • 43
  • 73
12
votes
2 answers

Yii2, Custom validation message with attribute names

In Login form, I need to have glyphicon-remove icon at the end of every validation message with the corresponding field names. So I used below code in the Login model. ['email', 'required', 'message' => 'Email cannot be blank
Siva.G ツ
  • 811
  • 1
  • 7
  • 20
11
votes
3 answers

Yii2 Select only few columns from related model

In controller i have: public function actionGetItems() { $model = new \app\models\WarehouseItems; $items = $model->find()->with(['user'])->asArray()->all(); return $items; } In WarehouseItem model i have standard (created by gii)…
Ljudotina
  • 259
  • 1
  • 3
  • 14
8
votes
4 answers

JOIN multiple table using relations in Yii2

I am trying to list some data through Kartik GridView widget in yii2 by using relations. I have these tables staffs CREATE TABLE IF NOT EXISTS `staffs` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(250) CHARACTER SET utf8 DEFAULT…
user7282
  • 5,146
  • 8
  • 38
  • 67
8
votes
3 answers

Yii2 multiple models in one form

How to use multiple models in one form in Yii2? My case: In my create action I can save into agenda_fiscalizacao table, but in update I receive this error when I try to load the form: Call to a member function formName() on array My Update…
jflizandro
  • 588
  • 1
  • 5
  • 15
8
votes
2 answers

How to join two tables and get values in Yii2 gridview

I'm using Yii2 gridview widget to display the data. I'm using two tables named as message and message_trigger. In message table columns are object_model, Object_id. In message_trigger the column are object_id, object_name. The grid get the values…
JeevaRekha
  • 331
  • 1
  • 6
  • 16
7
votes
3 answers

Yii2 dropdown multiple selected values

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, [ …
Muhammad Shahzad
  • 7,778
  • 20
  • 71
  • 123
7
votes
1 answer

Db Transactions in Yii2 with callbacks

I have simple code which uses DB transaction in Yii2, this updates user balance and adds a new record to user balance history. //User model public function changeBalance(UserBalanceHistory $balance) { $balance->balance = $this->balance; …
complex
  • 498
  • 4
  • 14
7
votes
1 answer

Get boolean values as true or false instead 1 or 0

I'm using a MySql database, so I defined the column type as Tinyint(1) in db schema. In my ActiveRecord I've set boolean validator. Save logic is working as expected. What I wanted now is that when I call Yii2 REST service, return boolean field as…
edrian
  • 4,377
  • 5
  • 26
  • 34
6
votes
1 answer

How to display a HTML tag in Yii2 form error summary

I am trying to display a link in error message in login for, but it is not working. The error message in LoginForm valdiation: $this->addError($attribute, 'Your account has been disabled. Enable It'); In login.php (view):
6
votes
1 answer

Yii2 wbraganca-dynamicform updation using yii2-relation-trait not deleting items

Based on the https://github.com/wbraganca/yii2-dynamicform/wiki/Dynamic-Forms-With-Yii2-relation-trait-(VERY-EASY), I am trying to implement dynamic forms.Create is working perfect, but in Update form, if I delete any dynamic form element, it is not…
user7282
  • 5,146
  • 8
  • 38
  • 67
6
votes
2 answers

Add field calculated from SQL query to Yii2 ActiveRecord model

I'm writing code to return a list of locations that are within a specified region, and close to a certain latitude and longitude. The database layout is that there is a Location table that contains the businesses, a Region table that defines the…
TMorgan
  • 625
  • 1
  • 7
  • 12
5
votes
1 answer

how to write if condition inside where condition based on subquery in yii2 query builder

I have a subquery inside main query as below: $subquery = (new Query())->select('COUNT(project_sup_id)') ->from('project_supervisors AS sup') ->where(['AND','sup.project_ref_id = p.project_id']); $uQuery =(new…
rji rji
  • 627
  • 2
  • 14
  • 34
5
votes
2 answers

How to add custom validation function in Dynamic model in Yii2?

I am using dynamic model in my yii2 basic application. following is code of my dynamic model. $model = new \yii\base\DynamicModel([ 'role', 'from_rm', 'to_rm', 'user1_subdistrcts' ]); $model->addRule(['user1_subdistrcts', 'role'],…
Vinit Singh
  • 1,113
  • 2
  • 21
  • 45
1
2 3
20 21