簡體   English   中英

Yii2:如何使用PostgreSQL的功能?

[英]Yii2: How to use a PostgreSQL's function?

我有一個名為persons的表,有三列名為idnamebirth

我可以得到namebirth但我需要計算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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM