3

I have a console script in a Yii2 advanced installation from which I can successfully use several models under 'common\models\modelName', but when I try to use a model from under 'backend\models\db\AuthAssignment' I get the following error:

Exception 'yii\base\UnknownClassException' with message 'Unable to find 'backend\models\db\AuthAssignment' in file: /var/www/html/mvu/backend/models/db/AuthAssignment.php. Namespace missing?'

This model file starts as follows:

<?php

namespace app\models\db;

use Yii;

class AuthAssignment extends \yii\db\ActiveRecord {

And the call from the console\controller file is as follows:

<?php
namespace console\controllers;

use Yii;
use yii\console\Controller;
use backend\models\db\AuthAssignment;
use common\models\CourseLessons;
use common\models\Courses;
use common\models\Customer;
use common\models\Users;

class MijnvuController extends Controller {

What namespace could the error mean here and where to include it?

1
  • The actual name space in the model file doesn't fit the namespace you use to call the model itself. That's the core of your problem. Commented Jul 5, 2016 at 18:19

2 Answers 2

1

Turns out that I needed to produce a duplicate of the specific model under 'frontend\models\db\AuthAssignment' because frontend and backend have similar functionality while running different databases.

Called it accordingly and it works:

<?php
namespace console\controllers;

use Yii;
use yii\console\Controller;
use backend\models\db\AuthAssignment;
use common\models\CourseLessons;
use common\models\Courses;
use common\models\Customer;
use common\models\Users;

class MijnvuController extends Controller {
Sign up to request clarification or add additional context in comments.

Comments

0

You can not directly extend/use models from backend directory.

To use models as per you requirements you need to add those models classes under console/models directory.

and then in your console controller use like:

use app/models/Classname;

Try this link for more details http://latcoding.com/2015/08/27/run-controller-yii2-via-console/

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.