Yii Framework Forum: Activerecord Findbysql Exception - Yii Framework Forum

Jump to content

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

Activerecord Findbysql Exception Where should potential bugs for yii2 be discussed?

#1 User is offline   Erik_r 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 22-January 14
  • Location:Zurich

Posted 22 January 2014 - 03:26 AM

I'm newbie in Yii2. But I decided to start with Yii2.

I wanted to use the method "findBySQL" in my model. The SQL has the aggregation function "MAX" in the select Statement. see:

   $sql = 'SELECT MAX(' . $keyName . ') from ' . $this->tableName();
   // 'Select MAX(rcv_Id) from Table'
   $row[] = $this->findBySQL($sql)->one();


The reply is an exception:


Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: app\models\GenericCodeValue::MAX(rcv_id)

1. in C:\xampp\htdocs\yii2\TestApp\vendor\yiisoft\yii2\yii\base\Component.php at line 116 
            }
        }
        if (method_exists($this, 'get' . $name)) {
            throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
        } else {
            throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
        }
    }
 
    /**
     * Checks if a property value is null.


It seems that ActiveQuery::one expect an Attribute 'MAX(rcv_id)' in my model which isn't the case. Aggregated attributes shouldn't be used for further Actions.

I will use ActiveQuery directly.
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 567
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 22 January 2014 - 03:57 AM

this should work:

   $row[] = $this->find()->max($keyName);


in general when selecting something like MAX() you should write MAX(...) AS attrName and have an attribute or public property in your AR class with that name.
0

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