title: Inner join in Yii2 - trying to get property of non-object
body: <p>I'm trying to link two tables: employees and departments, when each employee is a member of one department. Then I'll retrieve some information using the "department_id" field from both tables, "type" field from "departments" and "section" field from "departments". I've done it before using vanilla PHP and SQL, but now I have to implement it to Yii2.</p>
  
  <p>I started with adding relations in the view. Then I tried to link those tables using Yii2 functions like innerJoinWhere etc. When this failed, I tried executing raw SQL in Yii, but that didn't help.</p>
  
  <p>Every snippet is from the view.</p>
  
  <p>Relations:</p>
  
  <pre class="lang-php prettyprint-override"><code>class Employee extends \yii\db\ActiveRecord{
      public static function tableName(){
          return 'employees';
      }
  
      public function getDepartments(){
          return $this-&gt;hasOne(Departments::className(), ['department_id' =&gt; 'department_id']);
      }
  }
  
  class Departments extends \yii\db\ActiveRecord{
      public static function tableName(){
          return 'departments';
      }
      public function getEmployee(){
          return $this-&gt;hasOne(Employee::className(), ['department_id' =&gt; 'department_id']);
      }
  }
  </code></pre>
  
  <p>The SQL line that I tried to reproduce ($dep and $sec are some variables that are irrelevant right now):</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT DISTINCT departments.type, departments.department FROM departments
   INNER JOIN employees ON department.department_id = employees.department_id
   WHERE departments.department_id = '$dep' AND departments.section = '$sec'
  </code></pre>
  
  <p>First attempt (Yii2 functions):</p>
  
  <pre class="lang-php prettyprint-override"><code>$result = Departments::find()
      -&gt;select(['departments.type', 'departments.department_id'])
      -&gt;innerJoin('employees', '`employees`.`department_id` =  `departments`.`department_id`')
      -&gt;where(['departments.department_id' =&gt; $dep])
      -&gt;andwhere(['departments.section' =&gt; $sec])
      -&gt;with('employees')
      -&gt;one();
  </code></pre>
  
  <p>Second attempt (raw SQL):</p>
  
  <pre class="lang-php prettyprint-override"><code>$connection = Yii::$app-&gt;getDb();
  $command = $connection-&gt;createCommand("..."); //same as above
  $result= $command-&gt;queryOne();
  </code></pre>
  
  <p>Finally, I need to retrieve the information:</p>
  
  <pre class="lang-php prettyprint-override"><code>$type = $result-&gt;type;
  //etc.
  </code></pre>
  
  <p>The error message is the same for both attempts:</p>
  
  <blockquote>
    <p>Trying to get property of non-object, when the last line of above is
    executed.</p>
  </blockquote>