Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yii2:如何使用PostgreSQL';s函数?_Postgresql_Model View Controller_Yii2 - Fatal编程技术网

Yii2:如何使用PostgreSQL';s函数?

Yii2:如何使用PostgreSQL';s函数?,postgresql,model-view-controller,yii2,Postgresql,Model View Controller,Yii2,我有一个名为persons的表,其中有三列名为id、name和birth 我可以获得姓名和出生,但我需要使用该人的出生日期计算该人的年龄。我想使用PostgreSQL的函数:。但我不知道怎么用它 有没有办法使用PostgreSQL的函数 我试图在视图中使用它,但后来我意识到它不会起作用: $person = \app\models\Persons::findOne(['id' => 1234]); $myName = $person->name; $myBirth = $perso

我有一个名为
persons
的表,其中有三列名为
id
name
birth

我可以获得
姓名
出生
,但我需要使用该人的
出生日期计算该人的年龄。我想使用PostgreSQL的函数:。但我不知道怎么用它

有没有办法使用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;