Answers.Body: <p>Because the property <code>sectionId</code> does not exist in the sections table. All models in Craft have the property <code>id</code> as their primary key not - for example - <code>sectionId</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T19:54:13.430Z
Answers.Text: Yes, there is an "id" as primary key, but there also is "sectionId" column which I wanna use to join with "Entry" table. Do you mean only primary key can be used to join tables together?
Answers.UserDisplayName: yu xia
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T20:40:52.803Z
Answers.Text: No I mean your section has only the property id not sectionId.. as you can see here http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#declaring-relations "An easy rule to remember this is, as you see in the example above, you write the column that belongs to the related Active Record directly next to it. You see there that customer_id is a property of Order and id is a property of Customer." But the section does not have the property `sectionId` only `id`. However I'm not sure why you even need those functions since they already exist
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T21:08:43.667Z
Answers.Text: I'm new to craft and I wanna get all the entries under a certain section, so I tried to join "Entries" table and "Sections" table.
  "sectionId" belong to "Entries" table if you check the database. I run the sql below is working, but I don't know what's going wrong in craft:
  
  `select * from entries 
   join sections
   on entries."sectionId"=sections.id`
Answers.UserDisplayName: yu xia
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T21:12:40.163Z
Answers.Text: `$entries = craft\elements\Entry::find()->sectionId(>>insert your id<<)->all();` will do the trick and `$section = $entry->getSection()` you usually don't need to join anything
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T22:15:50.313Z
Answers.Text: Thanks man!  that's another way. Because I'm new, so I wanna try different ways to play around with craft including writing query, the way you mentioned and active records. XD
Answers.UserDisplayName: yu xia
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T05:53:27.320Z
Answers.LastActivityDate: 2018-03-20T05:53:27.320Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I built up a plugin and inside the plugin I created the relation between Entry and Section using ActiveRecord, like below:</p>
  
  <p>In SectionRecord :</p>
  
  <pre><code>class SectionRecord extends ActiveRecord
  {
      public static function tableName()
      {
          return '{{sections}}';
      }
  
      public function getEntries()
      {
          return $this-&gt;hasMany(EntryRecord::className(),['id' =&gt; 'sectionId']);
      }
  }
  </code></pre>
  
  <p>In EntryRecord file:</p>
  
  <pre><code>class EntryRecord extends ActiveRecord
  {
      public static function tableName()
      {
          return '{{entries}}';
      }
  
      public function getSection()
      {
          return $this-&gt;hasOne(SectionRecord::className(),['sectionId' =&gt; 'id']);
      }
  }
  </code></pre>
  
  <p>At last in controller:</p>
  
  <pre><code>$hub = SectionRecord::findOne(['handle'=&gt;'hub']);
  dd($hub-&gt;entries);
  </code></pre>
  
  <p>I tried to get section which is working, but when I wanna fetch entries under that section, showed me an error:</p>
  
  <pre><code>Unknown Property – yii\base\UnknownPropertyException
  Getting unknown property: raintestplugin\test\records\SectionRecord::sectionId
  </code></pre>
  
Tags: <relations><database><query>
Title: Relational query get error "Getting unknown property"
ThreadText: # Relational query get error "Getting unknown property"
  
  (Asked by: username_0 on 2018-03-20)
  
  I built up a plugin and inside the plugin I created the relation between Entry and Section using ActiveRecord, like below:
  
  In SectionRecord :
  
  ```
  class SectionRecord extends ActiveRecord
  {
      public static function tableName()
      {
          return '{{sections}}';
      }
  
      public function getEntries()
      {
          return $this->hasMany(EntryRecord::className(),['id' => 'sectionId']);
      }
  }
  
  ```
  
  In EntryRecord file:
  
  ```
  class EntryRecord extends ActiveRecord
  {
      public static function tableName()
      {
          return '{{entries}}';
      }
  
      public function getSection()
      {
          return $this->hasOne(SectionRecord::className(),['sectionId' => 'id']);
      }
  }
  
  ```
  
  At last in controller:
  
  ```
  $hub = SectionRecord::findOne(['handle'=>'hub']);
  dd($hub->entries);
  
  ```
  
  I tried to get section which is working, but when I wanna fetch entries under that section, showed me an error:
  
  ```
  Unknown Property – yii\base\UnknownPropertyException
  Getting unknown property: raintestplugin\test\records\SectionRecord::sectionId
  
  ```
  
  **Tags:** relations, database, query
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-20. Score: 1
  
  Because the property `sectionId` does not exist in the sections table. All models in Craft have the property `id` as their primary key not - for example - `sectionId`
  
  > **Comments:**
  > **username_0**: Yes, there is an "id" as primary key, but there also is "sectionId" column which I wanna use to join with "Entry" table. Do you mean only primary key can be used to join tables together?  
  > **username_1**: No I mean your section has only the property id not sectionId.. as you can see here http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#declaring-relations "An easy rule to remember this is, as you see in the example above, you write the column that belongs to the related Active Record directly next to it. You see there that customer_id is a property of Order and id is a property of Customer." But the section does not have the property `sectionId` only `id`. However I'm not sure why you even need those functions since they already exist  
  > **username_0**: I'm new to craft and I wanna get all the entries under a certain section, so I tried to join "Entries" table and "Sections" table. "sectionId" belong to "Entries" table if you check the database. I run the sql below is working, but I don't know what's going wrong in craft:  `select * from entries   join sections  on entries."sectionId"=sections.id`  
  > **username_1**: `$entries = craft\elements\Entry::find()->sectionId(>>insert your id<<)->all();` will do the trick and `$section = $entry->getSection()` you usually don't need to join anything  
  > **username_0**: Thanks man!  that's another way. Because I'm new, so I wanna try different ways to play around with craft including writing query, the way you mentioned and active records. XD  
  
  ---
  
  