Search code examples
phpmysqldatabasephpactiverecord

PHPActiveRecord Find Column Names & Types & In Particular ENUM Components


Is there a way to get all the column names, and then figure out what type the column is? For example, ENUM, VARCHAR, TINYINT, etc..., in particular for ENUM the different components of the ENUM?

I found something here (http://www.phpactiverecord.org/docs/ActiveRecord/Column)

Not sure how to get the type though from this documentation?


Solution

  • To list column names and their types:

    $columns = MyModel::table()->columns;
    foreach ($columns as $column) {
        echo "{$column->name} - {$column->raw_type} <br>\n";
    }
    

    Where MyModel is the name of your model class.

    This will output "raw" database type as defined in the DB schema. You can also use $column->type for normalized type in ActiveRecord internal representation.

    See AR source here and here.