Yii Framework Forum: Custom attributes in model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Custom attributes in model

#1 User is offline   jahau 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 05-February 17

Posted 16 March 2017 - 01:47 PM

How is it possible to define just simple custom attribute? I've got the following information:

public function rules()
{
return [
[['ID', 'PinNumber', 'TimeSpent'], 'integer'],
[['LogTime'], 'safe'],
];
}

Now I want to add StartTime (computed, does not exist in the database) attribute that will be calculated based on LogTime and TimeSpent (StartTime = LogTime - TimeSpent).

What should I do to the model and search / filtering etc. in order to get this done properly?
0

#2 User is offline   dimis283 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 364
  • Joined: 24-January 09

Posted 16 March 2017 - 03:52 PM

This may be usefull. http://www.yiiframew...ng-extra-fields
Posted Image
0

#3 User is offline   jahau 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 05-February 17

Posted 17 March 2017 - 03:44 AM

View Postdimis283, on 16 March 2017 - 03:52 PM, said:



"Then you need to compose a query, which calculates volume of the room and performs the sort."

Where and why? Didn't help that much.

I'd rather take my example and see what places are needed to be touched. Thanks.
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,806
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 17 March 2017 - 05:10 AM

Scroll to the top of that page and you will get it.
Don't forget to read The Definitive Guide to Yii (1.1 en) (1.1 sv) (2.0 en) | The class reference (1.1) (2.0) has the details
0

#5 User is offline   jahau 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 05-February 17

Posted 19 March 2017 - 04:06 AM

View Posttri, on 17 March 2017 - 05:10 AM, said:

Scroll to the top of that page and you will get it.


Now got to the point where the data is in the GridView. How to get the sorting and filtering to work? I need to add the same logic to the SQL query?
0

#6 User is offline   Rabb T. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 06-February 12

Posted 19 March 2017 - 11:41 AM

All you need is define the attribute in your model class:
class MyModel extends \yii\db\ActiveRecord {
    public $StartTime;
    
    // (...)
}

The rest is like with any other attribute (label, rule, search, filtering, ...)
0

#7 User is offline   jahau 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 05-February 17

Posted 20 March 2017 - 12:51 PM

I've defined this and get the following error when sorting the column:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'StartTime' in 'order clause'
The SQL being executed was: SELECT * FROM `logs` ORDER BY `StartTime` LIMIT 20
Error Info: Array
(
[0] => 42S22
[1] => 1054
[2] => Unknown column 'StartTime' in 'order clause'
)
0

#8 User is offline   dimis283 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 364
  • Joined: 24-January 09

Posted 20 March 2017 - 02:39 PM

This has also an order by example http://www.yiiframew...ng-extra-fields
Posted Image
0

#9 User is offline   jahau 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 05-February 17

Posted 20 March 2017 - 03:03 PM

View Postdimis283, on 20 March 2017 - 02:39 PM, said:

This has also an order by example http://www.yiiframew...ng-extra-fields


I've already defined the logic in the model how the StarTime is calculated. Do I really need to add that logic also to the query in order to get it working (that's how I understand the example, but is that correct)? Thx.
0

#10 User is offline   Rabb T. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 06-February 12

Posted 20 March 2017 - 03:58 PM

View Postjahau, on 20 March 2017 - 03:03 PM, said:

I've already defined the logic in the model how the StarTime is calculated. Do I really need to add that logic also to the query in order to get it working (that's how I understand the example, but is that correct)? Thx.

Yes, if you want to sort according to that attribute, you have to include it in the query as shown on that page. You don't have to calculate it elsewhere else, because the value will be returned by the query. If you don't need sorting, you can leave it out of the query.
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users