duanchongzi9997 2014-12-19 09:54 采纳率: 0%
浏览 120

Yii2:将函数值的返回值保存到数据库

I have created a function in my model PatientDetail.php.

This function is working well and returning the desired value, but I am not able to save the same in database.

public function getRoomCategory(){
     $model = \app\models\RoomCategory::find('id')
     ->innerJoin('room_charges','room_charges.room_category=room_category.id')
     ->innerjoin('patient_detail', 'room_charges.id = patient_detail.bed_type')               
     ->Where(['patient_detail.id'=> $this->id])->one();

     return $model;

   }

I have a field/Column in the model as 'room_category' and I want to save the return value of the function getRoomCategory in the Database.

I have tried to create the function with the same name like getroom_category(){}, but then I am not getting any value in the form field in my _form.php

I have also tried in my controller like:

if(isset($_POST['RoomCategory'])){
                $RoomCategory = $_POST['RoomCategory'];

            $model->room_category=$RoomCategory;
            $model->save();
            }

What is the best way to save the calculated value to the database? Thank for your valuable guidance.

  • 写回答

1条回答 默认 最新

  • doushi4795 2014-12-24 11:52
    关注

    It seems that the rules validation of the model is not allowing you to save it so If u want to save it without the validation you can do like this :

    if(isset($_POST['RoomCategory'])){
    
       $RoomCategory = $_POST['RoomCategory'];
    
       $model->room_category=$RoomCategory;
       $model->save(false);
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line