Model Metadata


    To speed up development Phalcon\Mvc\Model helps you to query fields and constraints from tables related to models. To achieve this, is available to manage and cache table metadata.

    Sometimes it is necessary to get those attributes when working with models. You can get a metadata instance as follows:

    Once the application is in a production stage, it is not necessary to query the metadata of the table from the database system each time you use the table. This could be done caching the metadata using any of the following adapters:

    As other ORM’s dependencies, the metadata manager is requested from the services container:

    1. use Phalcon\Mvc\Model\MetaData\Apc as ApcMetaData;
    2. $di['modelsMetadata'] = function () {
    3. // Create a metadata manager with APC
    4. $metadata = new ApcMetaData(
    5. [
    6. 'lifetime' => 86400,
    7. 'prefix' => 'my-prefix',
    8. ]
    9. );
    10. return $metadata;
    11. };

    You can change the default metadata introspection in the following way:

    This strategy doesn’t require any customization and is implicitly used by all the metadata adapters.

    Annotations

    This strategy makes use of annotations <annotations> to describe the columns in a model:

    1. <?php
    2. use Phalcon\Mvc\Model;
    3. class Robots extends Model
    4. {
    5. /**
    6. * @Primary
    7. * @Identity
    8. * @Column(type='integer', nullable=false)
    9. */
    10. public $id;
    11. /**
    12. * @Column(type='string', length=70, nullable=false)
    13. */
    14. public $name;
    15. /**
    16. * @Column(type='string', length=32, nullable=false)
    17. */
    18. public $type;
    19. * @Column(type='integer', nullable=false)
    20. */
    21. public $year;
    22. }

    Annotations must be placed in properties that are mapped to columns in the mapped source. Properties without the annotation are handled as simple class attributes.

    The annotation @Column supports the following parameters:

    The annotations strategy could be set up this way:

    Using the introspection strategies presented above, Phalcon can obtain the metadata for each model automatically without the developer needing to set them manually.

    The developer also has the option of define the metadata manually. This strategy overrides any strategy set in the metadata manager. New columns added/modified/removed to/from the mapped table must be added/modified/removed also for everything to work properly.

    1. <?php
    2. use Phalcon\Mvc\Model;
    3. use Phalcon\Db\Column;
    4. use Phalcon\Mvc\Model\MetaData;
    5. class Robots extends Model
    6. {
    7. public function metaData()
    8. {
    9. return array(
    10. // Every column in the mapped table
    11. MetaData::MODELS_ATTRIBUTES => [
    12. 'id',
    13. 'name',
    14. 'type',
    15. 'year',
    16. ],
    17. // Every column part of the primary key
    18. MetaData::MODELS_PRIMARY_KEY => [
    19. 'id',
    20. ],
    21. // Every column that isn't part of the primary key
    22. MetaData::MODELS_NON_PRIMARY_KEY => [
    23. 'name',
    24. 'type',
    25. 'year',
    26. ],
    27. // Every column that doesn't allows null values
    28. MetaData::MODELS_NOT_NULL => [
    29. 'id',
    30. 'name',
    31. 'type',
    32. ],
    33. // Every column and their data types
    34. MetaData::MODELS_DATA_TYPES => [
    35. 'name' => Column::TYPE_VARCHAR,
    36. 'year' => Column::TYPE_INTEGER,
    37. ],
    38. // The columns that have numeric data types
    39. MetaData::MODELS_DATA_TYPES_NUMERIC => [
    40. 'id' => true,
    41. 'year' => true,
    42. ],
    43. // The identity column, use boolean false if the model doesn't have
    44. // an identity column
    45. MetaData::MODELS_IDENTITY_COLUMN => 'id',
    46. // How every column must be bound/casted
    47. MetaData::MODELS_DATA_TYPES_BIND => [
    48. 'id' => Column::BIND_PARAM_INT,
    49. 'name' => Column::BIND_PARAM_STR,
    50. 'type' => Column::BIND_PARAM_STR,
    51. 'year' => Column::BIND_PARAM_INT,
    52. ],
    53. // Fields that must be ignored from INSERT SQL statements
    54. MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT => [
    55. 'year' => true,
    56. ],
    57. // Fields that must be ignored from UPDATE SQL statements
    58. MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE => [
    59. 'year' => true,
    60. ],
    61. // Default values for columns
    62. MetaData::MODELS_DEFAULT_VALUES => [
    63. 'year' => '2015',
    64. ],
    65. // Fields that allow empty strings
    66. MetaData::MODELS_EMPTY_STRING_VALUES => [
    67. 'name' => true,
    68. ],
    69. );
    70. }