/** * Initializes models meta-data * * @param Phalcon\Mvc\ModelInterface $model * @param Phalcon\DiInterface $di * @return array */ public function getMetaData(ModelInterface $model, DiInterface $di) { $reflection = $di->getAnnotations()->get($model); $properties = $reflection->getPropertiesAnnotations(); if (!$properties) { throw new \Exception("There are no properties with annotations defined in the class"); } $attributes = array(); $nullables = array(); $dataTypes = array(); $dataTypesBind = array(); $numericTypes = array(); $primaryKeys = array(); $nonPrimaryKeys = array(); $identity = false; foreach ($properties as $name => $collection) { if ($collection->has('Column')) { $arguments = $collection->get('Column')->getArguments(); /** * Get the column's name */ if (isset($arguments['column'])) { $columnName = $arguments['column']; } else { $columnName = $name; } /** * Check for the 'type' parameter in the 'Column' annotation */ if (isset($arguments['type'])) { switch ($arguments['type']) { case 'integer': $dataTypes[$columnName] = Column::TYPE_INTEGER; $dataTypesBind[$columnName] = Column::BIND_PARAM_INT; $numericTypes[$columnName] = true; break; case 'decimal': $dataTypes[$columnName] = Column::TYPE_DECIMAL; $dataTypesBind[$columnName] = Column::BIND_SKIP; $numericTypes[$columnName] = true; break; case 'string': $dataTypes[$columnName] = Column::TYPE_VARCHAR; $dataTypesBind[$columnName] = Column::BIND_PARAM_STR; break; default: throw new Exception("Invalid type: " . $arguments['type']); } } else { $dataTypes[$columnName] = Column::TYPE_VARCHAR; $dataTypesBind[$columnName] = Column::BIND_PARAM_STR; } /** * Check for the 'nullable' parameter in the 'Column' annotation */ if (!$collection->has('Identity')) { if (isset($arguments['nullable'])) { if (!$arguments['nullable']) { $nullables[] = $columnName; } } } $attributes[] = $columnName; /** * Check if the attribute is marked as primary */ if ($collection->has('Primary')) { $primaryKeys[] = $columnName; } else { $nonPrimaryKeys[] = $columnName; } /** * Check if the attribute is marked as identity */ if ($collection->has('Identity')) { if ($identity !== false) { throw new \Exception("More than one field has been marked as identity"); } $identity = $columnName; } } } return array(MetaData::MODELS_ATTRIBUTES => $attributes, MetaData::MODELS_PRIMARY_KEY => $primaryKeys, MetaData::MODELS_NON_PRIMARY_KEY => $nonPrimaryKeys, MetaData::MODELS_NOT_NULL => $nullables, MetaData::MODELS_DATA_TYPES => $dataTypes, MetaData::MODELS_DATA_TYPES_NUMERIC => $numericTypes, MetaData::MODELS_IDENTITY_COLUMN => $identity, MetaData::MODELS_DATA_TYPES_BIND => $dataTypesBind, MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT => array(), MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE => array()); }