sudo_ee_
sudo_ee_

Reputation: 109

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

i want to show one data from table client when create data Goods, but i got error Object of class frontend\modules\cargo\models\Client could not be converted.

here is 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,
      ]);
  }
}

I need to display data client in _form Goods.somebody could help me?

Upvotes: 0

Views: 5836

Answers (2)

user206
user206

Reputation: 1105

filters and ... should not be added as a argument to find.

findOne

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

find

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

Upvotes: 0

tigrasti
tigrasti

Reputation: 342

To get object of Client with that specific id you can do it in two ways.

Client::find($idClient);

or with ->one():

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

Upvotes: 1

Related Questions