Note that there are some explanatory texts on larger screens.

plurals
  1. POYii - Joins on tables are not working
    primarykey
    data
    text
    <p>I want to get the data from two tables. I have used the below code to do so. I am new to Yii, if I am doing it the wrong way, please suggest the right way.</p> <p>Here is the code of controller:</p> <pre><code>$dataProvider=new CActiveDataProvider('Users', array( 'criteria'=&gt;array( 'with'=&gt;'leave', 'together'=&gt;true, 'condition'=&gt;'leave.user_id=:user_id', 'params'=&gt;array(':user_id'=&gt;$this-&gt;loadModel(Yii::app()-&gt;user-&gt;getId())-&gt;user_id), ), )); $this-&gt;render('admin',array( 'dataProvider'=&gt;$dataProvider, )); </code></pre> <p>Here is the code of view:</p> <pre><code>$this-&gt;widget('zii.widgets.grid.CGridView', array( 'id'=&gt;'users-grid', 'dataProvider'=&gt;$dataProvider, 'columns'=&gt;array( array( 'header' =&gt; 'Leave Type', 'name'=&gt;'leave_type', 'value'=&gt;'$data-&gt;leave-&gt;leave_type', ), 'employee_code', 'username', 'password', array( 'class'=&gt;'CButtonColumn', ), ), )); </code></pre> <p><strong>My Problem is:</strong> </p> <p>I am joining two tables <strong>User</strong> and <strong>Leave</strong>. I want to get the data of users from <strong>Leave</strong> table as well as the users table. the above code shows me data from <strong>user</strong> table, when i try to show data from <strong>Leave</strong> table it throws me following error:</p> <blockquote> <p>Trying to get property of non-object</p> </blockquote> <p><strong>UPDATE</strong></p> <p>Here is my user model relations:</p> <pre><code>return array( 'leaves' =&gt; array(self::HAS_MANY, 'Leaves', 'leave_id'), 'creator' =&gt; array(self::BELONGS_TO, 'Users', 'created_by'), 'updator' =&gt; array(self::BELONGS_TO, 'Users', 'modified_by'), 'leave' =&gt; array(self::HAS_MANY, 'Leaves', 'user_id'), ); </code></pre> <p>Here is my leave model relations:</p> <pre><code>return array( 'user' =&gt; array(self::BELONGS_TO, 'Users', 'user_id'), 'creator' =&gt; array(self::BELONGS_TO, 'Users', 'created_by'), 'updator' =&gt; array(self::BELONGS_TO, 'Users', 'modified_by'), ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload