示例#1
0
文件: TableDesc.php 项目: AF83/morm
 /**
  * Constructor.
  *
  * @param string $table
  */
 public function __construct($table)
 {
     //set a table_desc object with the fields for a table
     $rs = SqlTools::sqlQuery("desc {$table}");
     if ($rs) {
         $this->table['name'] = $table;
         while ($field = mysql_fetch_object($rs, 'FieldDesc')) {
             $field->Decorate();
             $this->fields[$field->Field] = $field;
         }
         $this->field_keys = array_keys($this->fields);
         $this->nb_fields = count($this->field_keys);
         self::$tables[$table] = $this;
     } else {
         throw new Exception('Table ' . $table . ' may not exist ' . mysql_error());
     }
 }
示例#2
0
文件: Morm.php 项目: anicet/morm
 /**
  * getForeignValues 
  *
  * only used by the scaffolding, should probably be removed or extended to 
  * make it more generic 
  * 
  * @throws Exception if field is not a foreign key
  * @param mixed $field 
  * @param array $foreign_fields (can be null))
  * @param mixed $conditions 
  * @return array
  */
 public function getForeignValues($field, $foreign_fields = NULL, $conditions = NULL)
 {
     if ($this->isForeignKey($field)) {
         if (!isset($this->_foreign_values[$field])) {
             $select = is_null($foreign_fields) ? '*' : '`' . implode('`,`', $foreign_fields) . '`';
             $conditions = is_null($conditions) ? '' : $conditions;
             $sql = "select " . $select . " from `" . $this->getForeignTable($field) . "` " . $conditions;
             $rs = SqlTools::sqlQuery($sql);
             $foreign_values = array();
             while ($line = mysql_fetch_assoc($rs)) {
                 $foreign_values[] = $line;
             }
             $this->_foreign_values[$field] = $foreign_values;
         }
         return $this->_foreign_values[$field];
     } else {
         throw new Exception($field . ' is not a foreign key in table ' . $this->_table);
     }
 }
示例#3
0
文件: Mormons.php 项目: anicet/morm
 /**
  * @todo cache the result
  * @param boolean $with_limit
  * @return integer
  */
 public function get_count($with_limit = false)
 {
     $limit_stmt = $with_limit ? SqlBuilder::order_by($this->order, $this->order_dir) . SqlBuilder::limit($this->offset, $this->limit) : '';
     $rs = SqlTools::sqlQuery("SELECT count(1)\n                        \nFROM " . SqlBuilder::from($this->get_from_tables()) . SqlBuilder::joins($this->joins, $this->getJoinType()) . SqlBuilder::where($this->where, $this->sql_where) . $limit_stmt);
     if ($rs) {
         return (int) mysql_result($rs, 0);
     } else {
         throw new Exception("Fatal error:" . mysql_error());
     }
 }
示例#4
0
文件: MovableMorm.php 项目: AF83/morm
 public function delete()
 {
     $position = $this->position;
     if (parent::delete()) {
         $sql = "update `" . $this->_table . "` set position = position - 1 where position > " . $position;
         if (SqlTools::sqlQuery($sql)) {
             return true;
         }
         return false;
     }
 }
示例#5
0
文件: sqltool.php 项目: anicet/morm
 public function testFormatSqlToolsWithArrayNoFake()
 {
     $result = mysql_fetch_array(SqlTools::sqlQuery('SELECT ? + ?', array(40, 1)));
     $this->assertEqual(41, $result[0]);
 }