1

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']],
        ];
    }
}

1 Answer 1

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.