Unexpected behavior in Yii model

Maybe I’m not aware of some Yii2 limitations.

I call a model from a component and use a component function of the same component in the model. This lead to the error “component not found” (call function on 0).

Here my questions: Is this Yii behavior normal? Should I call the component from model explicite?

use app\components\authManager;

authManager::get(‘user’,‘branch_id’)

Hi @Goggo66

A quick answer to your question is quite simple : the behavior is normal, not only for Yii but also for PHP in general. PHP code can not find anything out of the current name space.

BTW, I don’t understand what you are trying to do.

  1. Is your ‘model’ an instance of Model or ActiveRecord?
  2. What is authManager? What does it do? Why don’t you use the standard DbManager or PhpManager?
  3. What does ‘call a model from a component’ and ‘use a component function of the same component in the model’ mean? Can you show us the relevant code?

Ok, thank you. Let’s take a look.

  1. Yes, it’s an AR

  2. authManager is my component, I use the DbManager also. authManager is more specific for my application and has more functions like encryption, no matter.

  3. Here comes the relevant source code

    config:

             'bootstrap' => ['log','app\components\authManager'],
    
             'auth' => [
                 'class' => 'app\components\authManager'
             ],
    

    authManager:

     $holding = \app\models\Holding::find()
                     ->where(['default_contact_id' => $default_contact->id])->one();
    
     $branches = $holding->getBranches()->all();
    

    holding AR:

     use Yii;
     use app\components\authManager; // why do I need this?
    
     class Holding extends \yii\db\ActiveRecord
     {
    
         /**
          * @return \yii\db\ActiveQuery
          */
         public function getBranches()
         {
             $branches = $this->hasMany(Branch::className(), ['id' => 'branch_id'])
                     ->viaTable(HoldingBranch::tableName(), ['holding_id' => 'id']);
    
             if (authManager::can('branch_user,branch_editor'))
             {
                 $branches = $branches->andWhere(['id' =>
                 authManager::get('user','branch_id')]);
             }
    
             return $branches;
         }
    
         // still breaks, doesn't find component:
         // Yii::$app->auth->can('branch_user,branch_editor');
         // Yii::$app->auth->get('user','branch_id');

I think the current code should work, if your authManager has those static methods of can() and get(). Does it still break?

And, a silly question, maybe, but is it really authManager and not AuthManager? Is it stored in authManager.php? Your code might be broken in config, not in getBranches method. Did you check the call stack?

Yii-Debugger says with Yii::$app->auth->can(): “Call to a member function can() on null”

Now I use the direct call of the component: \app\components\authManager::can()

I believe it’s an issue with the recursive call of the component and its not a namespace problem because all other of my components works well in this model.

Hmm, I think you should be right.
Probably you have to resolve the mutual dependence between authManager and Holding.