class 的 Yii2 对象 ..... 无法转换为字符串

Yii2 Object of class ..... could not be converted to string

我想在创建数据商品时显示来自 table 客户端的一个数据,但是我得到错误 class frontend\modules\cargo\models\Client 的对象无法转换。

这里是 GoodsController

public function actionCreate()
{
  $model = new Goods();
  $idClient = Yii::$app->user->identity->id_client;
  $client = Client::find($idClient)->one();

  if ($model->loadAll(Yii::$app->request->post()) && $model->saveAll()) {
      return $this->redirect(['view', 'id' => $model->id]);
  } else {
      return $this->render('create', [
          'model' => $model,
          'client' => $client,
      ]);
  }
}

我需要在 _form 中显示数据客户端Goods.somebody可以帮我吗?

要获取具有该特定 ID 的客户端对象,您可以通过两种方式进行。

Client::find($idClient);

->one():

Client::find()->where(['id' => $idClient])->one();

过滤器和...不应添加为查找的参数。

findOne

Client::findOne($idClient);
Client::findOne(['id' => $idClient])

查找

Client::find()->where(['id' => $idClient])->one();
Client::find()->where('id = :id', [':id' => $idClient])->one();