3

Following code is working on Craft 2:

class My_TableRecord extends BaseRecord {
  public function getTableName() {
    return 'my_table';
  }

  protected function defineAttributes() {
    return [
      'title'         => AttributeType::String,
      'description'   => AttributeType::Mixed
    ];
  }
}

According to this documentation the defineAttributes() is not available in Craft 3. So how can I define table's attributes in Craft3 to complete following code:

class My_TableRecord extends ActiveRecord
{   public static function tableName()
    {
        return '{{%my_table}}';
    }

  //equivalent function of defineAttributes() of Craft3 will go here

}

1 Answer 1

1

So how can I define table's attributes in Craft 3 to complete following code:

You don't.

By just defining the table name, that gives Yii enough information to reflect on the underlying columns for the table.

You can add property doc blocks to get type-hinting in your IDE for the columns.

Probably also worth reading: https://docs.craftcms.com/v3/extend/updating-plugins.html

2
  • :) Thank you @bell 01. Just property doc? does not have any alternative method? 02. I've not found anything about the defineAttributes() in 'updating plugin' docs. Not understood, could you please give me a direct code example to define a table's column in Craft3.
    – johirpro
    Commented Jun 19, 2020 at 0:49
  • Not even property docs (that's just useful for an IDE). Define the table name, like you're doing and Yii will take care of the rest. You'll be able to access the column names as properties on your table object.
    – Brad Bell
    Commented Jun 20, 2020 at 3:32

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.