Esempio n. 1
0
 function &find($arguments)
 {
     $conditions = array();
     # Be sure to have arguments in an array as needed below
     if (!is_array($arguments)) {
         $arguments = array($arguments);
     }
     # Instantiate model singleton to access scope
     $model =& Singleton::instance();
     $scope = $model->_scope["find"];
     $where = $order = $limit = $offset = NULL;
     $options = array();
     # preset scoped options
     if (isset($scope["order"])) {
         $order = $scope["order"];
     }
     # flatten all arguments if their key is numeric
     # this resolves e.g. array(1, 2, array("conditions" => "foo")) into array(1, 2, "conditions" => "foo")
     while (true) {
         $break = true;
         foreach ($arguments as $key => $value) {
             if (is_numeric($key) && is_array($value)) {
                 unset($arguments[$key]);
                 $arguments = array_merge_recursive($arguments, $value);
                 $break = false;
                 break;
             }
         }
         if ($break) {
             break;
         }
     }
     # parse arguments into query parameters
     foreach ($arguments as $key => $arg) {
         if (is_numeric($key)) {
             switch ($arg) {
                 case "all":
                     $limit = $offset = NULL;
                     $options[] = $arg;
                     continue 2;
                 case "first":
                     $limit = 1;
                     $offset = NULL;
                     $options[] = $arg;
                     continue 2;
                 default:
                     $key = $this->_primary_key;
             }
         }
         switch ($key) {
             case "conditions":
                 # FIXME make this DRY (see also AdoDBRecord_AssociationProxy)
                 if (!is_array($arg)) {
                     $arg = array($arg);
                 }
                 $conditions = array_merge_recursive($conditions, $arg);
                 break;
             case "limit":
             case "offset":
             case "order":
                 ${$key} = $arg;
                 break;
             default:
                 if (is_array($arg)) {
                     $conditions[] = sprintf("{$key} IN (?)", $arg);
                 } else {
                     $conditions[] = array("{$key} = ?", $arg);
                 }
         }
     }
     # parse conditions
     $parsed_conditions = array();
     $parsed_params = array();
     AdoDBRecord_Tools::parse_conditions($conditions, $parsed_conditions, $parsed_params);
     # join scoped conditions
     if (!empty($scope["conditions"])) {
         $parsed_conditions[] = $scope["conditions"];
     }
     # convert parsed options to sql
     if ($order !== NULL) {
         $order = " ORDER BY {$order}";
     }
     if (!empty($parsed_conditions)) {
         $where = sprintf(" WHERE (%s)", join($parsed_conditions, ") AND ("));
         AdoDBRecord_Tools::convert_sql_params($where, $parsed_params);
     }
     $objs = array();
     $conn =& _adodb_conn();
     # FIXME re-add table and column quotes again later
     if ($limit === NULL) {
         $limit = -1;
     }
     if ($offset === NULL) {
         $offset = -1;
     }
     if ($rs =& $conn->SelectLimit("SELECT * FROM {$this->_table_name}{$where}{$order}", $limit, $offset, $parsed_params)) {
         $rows =& $rs->GetRows();
         foreach ($rows as $row) {
             $class = empty($row["type"]) ? get_class($this) : $row["type"];
             $obj = new $class($row);
             $obj->_new_record = false;
             $objs[] = $obj;
         }
         if (in_array("all", $options)) {
             return $objs;
         }
         if (in_array("first", $options)) {
             return $objs[0];
         }
         if (count($objs) == 1) {
             return $objs[0];
         }
         return $objs;
     }
     $objs = NULL;
     return $objs;
 }
Esempio n. 2
0
 function get_columns()
 {
     $registration =& AdoDBRecord_Tools::registration();
     $table = $this->_table_name;
     if (array_key_exists($table, $registration->_column_cache)) {
         return $registration->_column_cache[$table];
     }
     $conn =& _adodb_conn();
     return $registration->_column_cache[$table] =& $conn->MetaColumnNames($table);
 }
Esempio n. 3
0
 function delete()
 {
     $conn =& _adodb_conn();
     if ($this->_new_record) {
         return false;
     }
     # FIXME re-add table and column quotes again later
     if ($res = $conn->Execute(sprintf("DELETE FROM %s WHERE %s", $this->_table_name, $this->_id()))) {
         $this->_new_record = true;
     }
     return $res;
 }