/** * Find links of this model type to a given model. * * eg.: * * \GO\Addressbook\Model\Contact::model()->findLinks($noteModel); * * selects all contacts linked to the $noteModel * * @param ActiveRecord $model * @param FindParams $findParams * @return ActiveStatement */ public function findLinks($model, $extraFindParams = false) { $findParams = FindParams::newInstance(); $findParams->select('t.*,l.description AS link_description'); $joinCriteria = FindCriteria::newInstance()->addCondition('id', $model->id, '=', 'l')->addRawCondition("t.id", "l.model_id")->addCondition('model_type_id', $this->modelTypeId(), '=', 'l'); $findParams->join("go_links_{$model->tableName()}", $joinCriteria, 'l'); if ($extraFindParams) { $findParams->mergeWith($extraFindParams); } return $this->find($findParams); }
/** * Get all columns of a model * * @param ActiveRecord $model * @return array */ public static function getColumns(ActiveRecord $model) { $tableName = $model->tableName(); $cacheKey = self::getCacheKey($model); if (self::$forceLoad) { unset(self::$_columns[$tableName]); \GO::cache()->delete($cacheKey); } if (isset(self::$_columns[$tableName]) && !self::$forceLoad) { return self::$_columns[$tableName]; } elseif ($columns = \GO::cache()->get($cacheKey)) { // \GO::debug("Got columns from cache for $tableName"); self::$_columns[$tableName] = $columns; return self::$_columns[$tableName]; } else { // \GO::debug("Loading columns for $tableName"); self::$_columns[$tableName] = array(); $sql = "SHOW COLUMNS FROM `" . $tableName . "`;"; $stmt = $model->getDbConnection()->query($sql); while ($field = $stmt->fetch()) { preg_match('/([a-zA-Z].*)\\(([1-9].*)\\)/', $field['Type'], $matches); if ($matches) { $length = $matches[2]; $type = $matches[1]; } else { $type = $field['Type']; $length = 0; } $required = false; $gotype = 'textfield'; $default = $field['Default']; $ai = strpos($field['Extra'], 'auto_increment') !== false; $pdoType = PDO::PARAM_STR; switch ($type) { case 'int': case 'tinyint': case 'bigint': $pdoType = PDO::PARAM_INT; if ($length == 1 && $type == 'tinyint') { $gotype = 'boolean'; } else { $gotype = ''; } $length = 0; $default = $ai || !isset($field['Default']) ? null : intval($default); break; case 'float': case 'double': case 'decimal': $pdoType = PDO::PARAM_STR; $length = 0; $gotype = 'number'; $default = $default == null ? null : floatval($default); break; case 'mediumtext': case 'longtext': case 'text': $gotype = 'textarea'; break; case 'mediumblob': case 'longblob': case 'blob': $gotype = 'blob'; break; case 'date': $gotype = 'date'; break; case 'time': $gotype = 'time'; break; } switch ($field['Field']) { case 'ctime': case 'mtime': $gotype = 'unixtimestamp'; break; case 'name': $required = true; break; case 'user_id': $gotype = 'user'; break; } //HACK: When a database may not be null and has no default value value is empty string if (!GO::config()->debug) { if ($field['Null'] == 'NO' && is_null($default) && !$ai) { $default = ''; } } //workaround for old boolean fields as enums. Should be using bool now. if ($type == "enum('0','1')") { $gotype = 'boolean'; $default = '0'; } //$required = is_null($default) && $field['Null']=='NO' && strpos($field['Extra'],'auto_increment')===false; self::$_columns[$tableName][$field['Field']] = array('type' => $pdoType, 'required' => $required, 'length' => $length, 'gotype' => $gotype, 'default' => $default, 'dbtype' => $type, 'null' => $field['Null'] == 'YES'); } \GO::cache()->set($cacheKey, self::$_columns[$tableName]); return self::$_columns[$tableName]; } }