Пример #1
0
 public function getColumns()
 {
     $primaries = 0;
     $columns = array();
     // recupera o nome da classe (Model que está sendo manipulado)
     $class = get_class($this);
     // armazena os tipos de dados que o modelo entende numa variável
     $types = $this->_types;
     // cria o objeto inspector
     $inspector = new Inspector($this);
     // recupera as propriedades da classe através do objeto Inspector
     $properties = $inspector->getClassProperties();
     // função interna pra retornar o primeiro elemento do array
     $first = function ($array, $key) {
         if (!empty($array[$key]) && sizeof($array[$key]) == 1) {
             return $array[$key][0];
         }
         return null;
     };
     foreach ($properties as $property) {
         $propertyMeta = $inspector->getPropertyMeta($property);
         // se tiver a marcação @column
         if (!empty($propertyMeta["@column"])) {
             $name = preg_replace("#^_#", "", $property);
             $primary = !empty($propertyMeta["@primary"]);
             $foreign = !empty($propertyMeta["@foreign"]);
             $references = $first($propertyMeta, "@references");
             $delete = $first($propertyMeta, "@delete");
             $update = $first($propertyMeta, "@update");
             $type = $first($propertyMeta, "@type");
             $length = $first($propertyMeta, "@length");
             $index = !empty($propertyMeta["@index"]);
             $readwrite = !empty($propertyMeta["@readwrite"]);
             $read = !empty($propertyMeta["@read"]) || $readwrite;
             $write = !empty($propertyMeta["@write"]) || $readwrite;
             $validate = !empty($propertyMeta["@validate"]) ? $propertyMeta["@validate"] : false;
             $label = $first($propertyMeta, "@label");
             if (!in_array($type, $types)) {
                 throw new Exception("{$type} is not a valid type");
             }
             $columns[$name] = array("raw" => $property, "name" => $name, "primary" => $primary, "foreign" => $foreign, "type" => $type, "references" => $references, "delete" => $delete, "update" => $update, "length" => $length, "index" => $index, "read" => $read, "write" => $write, "validate" => $validate, "label" => $label);
             if ($primary) {
                 $primaries++;
             }
         }
     }
     if ($primaries !== 1) {
         throw new Exception("{$class} must have exactly one @primary column");
     }
     $this->_columns = $columns;
     return $this->_columns;
 }
Пример #2
0
 /**
  *This method returns the columns as defined with all their metadata
  *
  *@param null
  *@return array Array of column names
  */
 public function getColumns()
 {
     //check if the columns array has been set
     if (empty($this->columns)) {
         //set variable to contain the primaries
         $primaries = 0;
         //set array to contain the column names
         $columns = array();
         //get the class name
         $class = get_class($this);
         //get the data types into an array
         $types = $this->types;
         //get new inspector class instance
         $inspector = new Inspector($this);
         //get all the class properties for this class
         $properties = $inspector->getClassProperties();
         $first = function ($array, $key) {
             //check if the key exists in array and the size of array greater than one
             if (!empty($array[$key]) && sizeof($array[$key]) == 1) {
                 //return the first element in $array[$key]
                 return $array[$key][0];
             }
             //if no instance of this was found, return null
             return null;
         };
         //loop through each property in properties array performing the associated function
         foreach ($properties as $property) {
             $propertyMeta = $inspector->getPropertyMeta($property);
             if (!empty($propertyMeta["@column"])) {
                 $name = preg_replace("#^_#", "", $property);
                 $primary = !empty($propertyMeta["@primary"]);
                 $type = $first($propertyMeta, "@type");
                 $length = $first($propertyMeta, "@length");
                 $index = !empty($propertyMeta["@index"]);
                 $readwrite = !empty($propertyMeta["@readwrite"]);
                 $read = !empty($propertyMeta["@read"]) || $readwrite;
                 $write = !empty($propertyMeta["@write"]) || $readwrite;
                 $validate = !empty($propertyMeta["@validate"]) ? $propertyMeta["@validate"] : false;
                 $label = $first($propertyMeta, "@label");
                 if (!in_array($type, $types)) {
                     throw new Exception\Type("{$type} is not a valid type");
                 }
                 if ($primary) {
                     $primaries++;
                 }
                 $columns[$name] = array("raw" => $property, "name" => $name, "primary" => $primary, "type" => $type, "length" => $length, "index" => $index, "read" => $read, "write" => $write, "validate" => $validate, "label" => $label);
             }
         }
         if ($primaries !== 1) {
             throw new Exception\Primary("{$class} must have exactly one @primary column");
         }
         $this->columns = $columns;
     }
     return $this->columns;
 }
Пример #3
0
function inicializaDados($obj)
{
    $dados = array();
    $inspector = new Inspector($obj);
    $first = function ($array, $key) {
        if (!empty($array[$key]) && sizeof($array[$key]) == 1) {
            return $array[$key][0];
        }
        return null;
    };
    foreach ($inspector->getClassProperties() as $property) {
        $meta = $inspector->getPropertyMeta($property);
        $type = $first($meta, "@type");
        if (!empty($type)) {
            $name = preg_replace("#^_#", "", $property);
            if ($type == "autonumber") {
                $dados[$name] = 0;
            }
            if ($type == "text" || $type == "longtext") {
                $dados[$name] = "";
            }
            if ($type == "integer") {
                $dados[$name] = "";
            }
            if ($type == "datetime") {
                $dados[$name] = "";
            }
            if ($type == "time") {
                $dados[$name] = "";
            }
            if ($type == "array") {
                $dados[$name] = array();
            }
            if ($type == "decimal") {
                $dados[$name] = "0,00";
            }
            if ($type == "date") {
                //$dados['timestamp' . ucwords($name)] = 0;
                $dados[$name] = "";
            }
        }
    }
    return $dados;
}