<pre><code>/**
 * Class SearchRelationship record
 *
 * @property int $id
 * @property int $aaa
 * @property int $bbb
 * @property string $ccc
*/
class SearchRelationship extends ActiveRecord
{
    /**
     * @inheritdoc
     * @return string
     */
    public static function tableName(): string
    {
        return '{{%somename}}';
    }
}
</code></pre>
<p>All you really need to define is the table name.  Yii 2 uses reflection on the database table to generate the column/object property names at runtime (replacing the old <code>setAttributes</code> method).</p>
<p>Worth reading: <a href="https://www.yiiframework.com/doc/guide/2.0/en/db-active-record" rel="nofollow noreferrer">https://www.yiiframework.com/doc/guide/2.0/en/db-active-record</a></p>
<p>Indexes should be initially defined in your plugin's <a href="https://docs.craftcms.com/v3/extend/migrations.html#plugin-install-migrations" rel="nofollow noreferrer">install migration</a> (replacing <code>defineIndexes</code>).</p>

CC BY-SA 4.0
2020-06-22T04:28:55.680Z
2020-06-22T04:28:55.680Z
Brad Bell
<p>Trying to convert a plugin from Craft 2 to Craft 3, not sure what's the <a href="https://docs.craftcms.com/v2/plugins/records.html" rel="nofollow noreferrer">Records</a> equivalent in Craft 3.
How do I get this to work in C3?</p>
<pre><code>class SearchRelationshipRecord extends BaseRecord
{
    public function tableName()
    {
        return 'some_name';
    }

    protected function setAttributes()
    {
        return [
            'aaa' =&gt; AttributeType::Number,
            'bbb' =&gt; AttributeType::Number,
            'ccc' =&gt; AttributeType::String
        ];
    }

    public function defineIndexes()
    {
        return [
            ['columns' =&gt; ['aaa']],
            ['columns' =&gt; ['bbb']],
            ['columns' =&gt; ['aaa', 'ccc']],
            ['columns' =&gt; ['bbb', 'ccc']],
        ];
    }
}
</code></pre>

<plugin-development><records>
Equivalent of Records in Craft 3?
# Equivalent of Records in Craft 3?

(Asked by: username_0 on 2020-06-21)

Trying to convert a plugin from Craft 2 to Craft 3, not sure what's the Records equivalent in Craft 3.
How do I get this to work in C3?
class SearchRelationshipRecord extends BaseRecord
{
    public function tableName()
    {
        return 'some_name';
    }

    protected function setAttributes()
    {
        return [
            'aaa' => AttributeType::Number,
            'bbb' => AttributeType::Number,
            'ccc' => AttributeType::String
        ];
    }

    public function defineIndexes()
    {
        return [
            ['columns' => ['aaa']],
            ['columns' => ['bbb']],
            ['columns' => ['aaa', 'ccc']],
            ['columns' => ['bbb', 'ccc']],
        ];
    }
}

**Tags:** plugin-development, records


---

## 1 answers

### Answer by username_1 on 2020-06-22. Score: 3

/**
 * Class SearchRelationship record
 *
 * @property int $id
 * @property int $aaa
 * @property int $bbb
 * @property string $ccc
*/
class SearchRelationship extends ActiveRecord
{
    /**
     * @inheritdoc
     * @return string
     */
    public static function tableName(): string
    {
        return '{{%somename}}';
    }
}

All you really need to define is the table name.  Yii 2 uses reflection on the database table to generate the column/object property names at runtime (replacing the old setAttributes method).
Worth reading: https://www.yiiframework.com/doc/guide/2.0/en/db-active-record
Indexes should be initially defined in your plugin's install migration (replacing defineIndexes).


---

