How in ActiveRecord by regular means to do sampling with calculated fields at SQL level?

For example, as in the query:

SELECT *, INET_NTOA(ip_addr) AS ip_addr FROM table 

where, the ip_addr field in the table is stored in the INT format, and through INET_NTOA() converted to a string representation.

    1 answer 1

    We read the group of conditions by default look here

    And here is your performance:

    Add a field to the class MyClass:

      class MyClass extends CActiveRecord { public ip_addr; // далее ваш код //Также атрибут необходимо зделать несохраняемым в базу public function rules() { //ваш код array ( 'ip_addr','unSafe') } //группа условий по умолчанию public function defaultScope() { return array( 'select '=>" *, INET_NTOA(ip_addr) AS ip_addr", ); } } 

    If the value of the field ip_addr comes from the field from the User, then it should not be done not saved. But then the question is - how do you convert it before saving? And another line

    'select '=>" *, INET_NTOA(ip_addr) AS ip_addr", nada will rewrite it like this

     'select '=>"перечисляем все поля таблицы за исключением ip_addr, INET_NTOA(ip_addr) AS ip_addr" 
    • Does this option only work with non-persistent attributes? Tried to override the actual field using defaultScope() . Ie, after INET_NTOA put an alias with the name of the real field. Deduced everything as expected. And, behold, did not want to save. - KiTE
    • I made it unsupervised so that its value was not taken from the form by force. Did not quite understand the comment about this field - can I write in more detail the task? - Ale_x