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
5
votes
2 answers

Sorting calculated fields in Yii2 (Grid View)

i am need to sort some fields (asc,desc) in GridView, but same fields are calculated. Look at code below: SearchModel: class ObjectSearch extends Object { use SearchModelTrait; public function rules() { return [ ['id', 'integer', 'min'…
Max Maximov
  • 113
  • 1
  • 11
5
votes
1 answer

Yii2 : how to use deleteAll with two conditions = and NOT IN

I am trying to delete data from RestoFoods model like that: RestoFoods::deleteAll(["restaurant_id"=>$postData['resto_id'], 'food_id NOT IN'=> [1,2] ]); I want this sql: DELETE FROM `resto_foods` WHERE `restaurant_id`=1 AND (`food_id` NOT IN (1,…
Vaseem Khan
  • 51
  • 1
  • 3
5
votes
3 answers

Yii2: Can I use scenarios to specify different set of model fields for different actions?

I can see in the Yii2 model page (http://www.yiiframework.com/doc-2.0/yii-base-model.html), in the "fields" section that you can set "different lists of fields based on some context information. For example, depending on $scenario or the privilege…
FlamingMoe
  • 1,878
  • 4
  • 29
  • 54
4
votes
1 answer

Yii2 "has no relation named" at search model even the function it is defined in the main model

I am having a yii2 error : common\models\Book has no relation named "favorite". When I try to add: public function search($params) { $query = Book::find(); $query->joinWith(['author', 'profile', 'favorite']); In the book model I do have the…
open-ecommerce.org
  • 1,674
  • 24
  • 38
4
votes
1 answer

Yii 2 Filter many-to-many relation on a value in the junction table

How can I use a many to many relation in Yii 2.0 where I filter the results on values that are in the connection/junction table? I have the following tables Member (id INT, name, ...) Event (id INT, name, date, ...) EventMemberConnection (id INT,…
Hans Westman
  • 781
  • 1
  • 15
  • 24
4
votes
1 answer

Yii2: custom setter method/data transformation doesn't work on ActiveRecord?

I'm trying to do data transformation on an attribute by using the setter method setUsr_firstname for an attribute that is named usr_firstname. class User extends ActiveRecord implements IdentityInterface { public function rules() { …
TheStoryCoder
  • 2,874
  • 4
  • 24
  • 54
4
votes
0 answers

Yii2 mongodb relational searching

I have two collections - Users and Order I have a list of orders(OrderSearch model) with some users details(email, name..). Now I want to search user details relationally. OrderSearch Model--- public function search($params) { // Email…
mohit
  • 1,661
  • 1
  • 12
  • 23
4
votes
1 answer

Yii2 tableName return values

Could you tell me what is the difference between function tableName() in class which return value {{%table_name}} and 'table_name' in Yii2 ?? public static function tableName(){ return {{%admin}}; } public static function tableName(){ return…
rafaa1994
  • 71
  • 3
4
votes
1 answer

Yii2 Validate multiple models

I have two models in Yii2 (masterTransaction and splitTransaction), where each masterTransactions can have multiple splitTransactions. Each splitTransaction has an attribute 'amount'. My problem is I need to validate if the sum over all 'amount'…
coldice4
  • 51
  • 2
  • 7
4
votes
4 answers

Yii2 how to check if two models are already linked

I have two models related through a junction table. $model->link() is the method used to establish the relationship between the two models. It basically populates the junction table with the corresponding keys of both models. If two models are…
Arge
  • 77
  • 2
  • 8
4
votes
3 answers

Trying to get property of non-object in yii2

I want access to hasMany relation but i get error with this content PHP Notice – yii\base\ErrorException Trying to get property of non-object this is my view : news/index.php
majid
  • 672
  • 1
  • 8
  • 32
4
votes
2 answers

Multiple relations in Yii 2.0 ActiveRecord Model

I am trying to define relations in ActiveRecord Model Class, but the problem is our database is too large and has many relations. So is there a simpler way to declare relation in only one model so that Yii can understand the relation for every other…
3
votes
1 answer

Yii 2 Update All Not IN

Based on the code below, I want to update all in customer to status 1 where status = 2. However, i want to run the code below where customer_id not in (1, 3, 5, 8). $customerNotIn = array(1, 3, 5 ,8); Customer::updateAll(['status' => 1], 'status =…
CloudSeph
  • 803
  • 3
  • 12
  • 29
3
votes
1 answer

How to change sorter param in Yii2 / GridView?I

I have two tables with farily same names of the columns, usinng two different search models (both using ActiveDataProvider's) so when I sort one by a column, the other gets affected as well. I have tried setting this in my second GridView: 'sorter'…
Zlatan Omerović
  • 3,184
  • 4
  • 30
  • 58
3
votes
3 answers

How to search with an array in Yii2 search model

public function actionIndex() { $searchModel = new SubjectSearch(); $searchModel->search()->query->andFilterWhere(['in','subjectID',[1,2,3]]); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return…
Tsz Fung Li
  • 57
  • 1
  • 9
1
2
3
20 21