Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Getting model property metadata

How do I determine specific metadata about a model property? For example, I want to ensure that a value I store in a propery won't exceed the length of that column as defined in the database. I realise this is something related to model metadata, but the Phalcon's "Models Metadata" documentation seems to focus on metadata caching and metadata strategies, and doesn't show any examples of how to actually get something like a column's length (as defined in the database).



68.3k
Accepted
answer

Hey, hope this quick snippet helps :)

$columns = $this->db->describeColumns('halls'); // Table name
foreach ($columns as $column) {
    echo 'Name: ', $column->getName(), '<br/>';
    echo 'Type: ', $column->getType(), '<br/>'; // Column types constants https://github.com/phalcon/cphalcon/blob/master/phalcon/db/column.zep
    echo 'Size: ', $column->getSize(), '<br/><hr/>';
}

Related links:

https://docs.phalconphp.com/ar/3.2/db-layer

https://docs.phalconphp.com/bs/3.2/api/Phalcon_Db_Column

https://github.com/phalcon/cphalcon/blob/master/phalcon/db/column.zep