Join Model With Third Party Model

Hi, I’m using dektrium user extension to manage my user login.

How do I join the Profile table in there with my custom model?




namespace app\models;


use Yii;

use yii\db\ActiveRecord;

use yii\data\ActiveDataProvider;

use yii\db\BaseActiveRecord;

use dektrium\user\models\Profile;


class Analysis extends ActiveRecord{

         ...............

         public function getMyAnalysis($user_id){

                $query = Analysis::find()

                 ->leftJoin('profile', 'profile.user_id = analysis.user')

                 ->where(['analyst' => $user_id]);

                ->with('Profile');

         }

}




I got "app\models\Analysis has no relation named "Profile"." as my error.

From what I understand I can’t access Profile model, but I already add it “use dektrium\user\models\Profile;

Any suggestions?

Hi,

You’re missing the actual relation.

Something like:




namespace app\models;


use Yii;

use yii\db\ActiveRecord;

use yii\data\ActiveDataProvider;

use yii\db\BaseActiveRecord;

use dektrium\user\models\Profile;


class Analysis extends ActiveRecord{

  

   public function getProfile()

   {

       return $this->hasOne(Profile::className(), ['user_id' => 'user_id_field_of_this_table']);

   }


}



Read: http://www.yiiframework.com/doc-2.0/guide-db-active-record.html

"Working with relational data"

Regards

Thanks, it worked perfectly