We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



93.7k
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.phalcon.io/ar/3.2/db-layer

https://docs.phalcon.io/bs/3.2/api/Phalcon_Db_Column

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