3
votes

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?

2
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. - BRO_THOM

2 Answers

1
votes

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 {
0
votes

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/