0

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?

1
  • The answer by byte255 is very close, I only need to figure out the ENUM values now instead of it just returning type ENUM. Commented Sep 21, 2014 at 19:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer! This worked great and helps a lot so far. I did get that the value was ENUM... but I unfortunately did not get the values that make up the ENUM. Lets say the ENUM was made up of values such as 'Yes', 'No', 'Maybe', I need to figure out what those are.
As opposed to the current answer which just returns ENUM. None the less this is a great start for me to explore into.* - forgot to add this into last comment.
@JosephAstrahan Unfortunately PHP ActiveRecord doesn't parse ENUM values, see the source. So there is no list of values readily available. However, you can issue SHOW COLUMNS statement and parse the ENUM values yourself, see this answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.