无法找到后端\ models \ QuoteDocs名称空间缺失

时间:2016-05-01 07:13:24

标签: php yii2 yii-extensions yii2-advanced-app yii-components

动态表单Yii2,在文件中收到错误“无法找到'backend \ models \ QuoteDocs':C:\ xampp \ htdocs \ taskmanagement / backend / models / QuoteDocs.php。命名空间丢失了吗?”

当我尝试编写后端\ models \ QuoteDocs时出现以下错误

enter image description here

当我尝试编写app \ models \ QuoteDocs时出现以下错误

enter image description here

控制器代码:

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.');
        }
    }
}

2 个答案:

答案 0 :(得分:0)

似乎与您有关use backend\models\QuoteDocs;

然后确保在后端模型中有QuoteDocs模型..

可能存在错误且模型在app中

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

否则可能是您没有为

分配适当的命名空间

后端\模型\ QuoteDocs

模型

答案 1 :(得分:0)

只需添加控制器使用部分

即可
use yii\base\Model;