Best way to retrieve model ID to be input in another model

What’s the best way to retrieve a model ID? I need to retrieve a status ID to be input to another model. I’ve thought of multiple ways:

  1. Function that searches the model using a identification code.
public function UpdateStatus($code) {
     $statusModel = Status::find()->where(['code' => $code])->one();
     $this->status = $statusModel->id;
     ...
}
  1. An individual function the searches for each status
public function UpdateStatus($code) {
     $statusModel = Status::getCancelledId();
     $this->status = $statusModel->id;
     ...
}

public class Status {
   public static function getCancelledId() {
      $statusModel = Status::find()->where(['code' => 'cancelled_code'])->one();
      return $statusModel->id;
   }

}

Answer

Your first method is close. I’d do something like your first method but return it asArray and only select the columns you need since you only need the ID. Doing this will make your query as light as possible while still active record.

Status::find()->select('id,code'])->where(['code' => $code])->asArray()->one()