[英]How to use postgres function string_agg() in yii2 ActiveDataProvider?
[英]Yii2: How to use a PostgreSQL's function?
我有一個名為persons
的表,有三列名為id
, name
和birth
。
我可以得到name
和birth
但我需要計算birth
日期的人的年齡 。 我想使用PostgreSQL的函數: age(timestamp)
。 但我不知道要用它。
有沒有辦法使用PostgreSQL的功能?
我試圖在視圖中使用它,但后來我發現它不起作用:
$person = \app\models\Persons::findOne(['id' => 1234]);
$myName = $person->name;
$myBirth = $person->birth;
$myAge = $person->age(birth);
我應該改變模型嗎? 我不知道在哪里使用age
函數。
在您的模型中,您應該定義公共財產public $age;
。 然后,您的select語句應如下所示:
$person = Persons::find()->select(['name', 'birth', 'age(birth) as age'])->andWhere(['id' => 1234])->one();
然后你可以訪問它作為屬性:
$person->age;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.