Activerecord 获取相关模型中的最后一条记录
我有两个模型:个人模型和租借模型 人员表字段:Activerecord 获取相关模型中的最后一条记录,activerecord,yii2,relationship,Activerecord,Yii2,Relationship,我有两个模型:个人模型和租借模型 人员表字段: 身份证 名字 RentedItem字段: 摆脱 电影识别码 租期 我需要添加一个新属性的人模型,其中包含最后的租金日期,但我不知道如何。我已经有了一个getRentDates方法,其中定义了许多关系 你能帮我吗 谢谢。准备以下关系: public function getLastRentItem() { return $this->hasMany(RentItem::class, ['rid' => 'id'])
- 身份证
- 名字
- 摆脱
- 电影识别码
- 租期
public function getLastRentItem()
{
return $this->hasMany(RentItem::class, ['rid' => 'id'])
->orderBy(['rent_date' => SORT_DESC])
->limit(1)
->one();
}
像这样使用它:
$model->lastRentItem ? $model->lastRentItem->rent_date : null;
*你必须调整这个代码,因为我没有看到你的