2

Dynamic form Yii2, getting error "Unable to find 'backend\models\QuoteDocs' in file: C:\xampp\htdocs\taskmanagement/backend/models/QuoteDocs.php. Namespace missing?"

when I am trying to write backend\models\QuoteDocs I am getting following error

enter image description here

when I am trying to write app\models\QuoteDocs I am getting following error

enter image description here

Controller Code:

namespace backend\controllers;
use Yii;
use app\models\Quotations;
use app\models\QuotationsSearch;
use backend\models\QuoteDocs;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\DocType;
/**
 * QuotationsController implements the CRUD actions for Quotations model.
 */
class QuotationsController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Quotations models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new QuotationsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Quotations model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Quotations model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {

        $model = new Quotations();
        $modelQuoteDocs=[new QuoteDocs];

       if ($model->load(Yii::$app->request->post())) {
        $modelQuoteDocs = Model::createMultiple(QuoteDocs::classname());
            Model::loadMultiple($modelQuoteDocs, Yii::$app->request->post());


            // validate all models
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelQuoteDocs) && $valid;

            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        foreach ($modelQuoteDocs as $modelQuoteDoc) {
                            $modelQuoteDoc->qdoc_quotation_id = $model->q_id;
                            if (! ($flag = $modelQuoteDoc->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction->commit();

                        if ($model->save(false)) {
                            return $this->redirect(['view', 'id' => $model->q_id]);
                        }   
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
        }else {
            return $this->render('create', [
                'model' => $model,
                'modelQuoteDocs' => (empty($modelQuoteDocs)) ? [new QuoteDocs] : $modelQuoteDocs
            ]);
        }
    }


    /**
     * Updates an existing Quotations model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->q_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Quotations model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Quotations model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Quotations the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Quotations::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
Nayank
  • 101
  • 1
  • 3
  • 14

2 Answers2

3

Seems related to you use backend\models\QuoteDocs;

Then Be sure you have the QuoteDocs model in backend model ..

Could be there is mistake and the model is in app

........
use app\models\QuoteDocs;

Otherwise could be you have no assigned a proper namespace to the

backend\models\QuoteDocs

model

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Just add in controller use section

use yii\base\Model;
Lesser Jank
  • 381
  • 1
  • 4
  • 9