0

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?

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

thank you it's work. but i don't know why only two field is displayed from three.
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.