Returns the schema information for the database opened by this connection.
public getSchema ( ) : |
||
return | the schema information for the database opened by this connection. |
/** * Important - modelClass must have a public method "attributeTypes" * Example: * * public function attributeTypes() * { * return [ * 'id' => Schema::TYPE_PK, * 'owner_id' => Schema::TYPE_INTEGER, * 'name' => Schema::TYPE_STRING, * 'description' => Schema::TYPE_TEXT, * 'status' => Schema::TYPE_SMALLINT, * 'updated_at' => Schema::TYPE_TIMESTAMP, * 'created_at' => Schema::TYPE_DATETIME, * ]; * } * * Example of use: * * (new DbSync([ * 'common\models\', // if namespace equivalent to path * 'Path to directory' => 'someName\models\', * ]))->run(); * * * @param array $nameSpaces */ public function __construct(array $nameSpaces) { foreach ($nameSpaces as $key => $nameSpace) { $this->nameSpaces[$key] = trim($nameSpace, '\\') . '\\'; } $this->db = \Yii::$app->getDb(); $this->tableNames = $this->db->getSchema()->getTableNames(); }
/** * @param Connection $db * @param bool $refresh * @return string[] */ public static function getSchemaNames(Connection $db, $refresh = false) { try { $schemaNames = array_diff($db->getSchema()->getSchemaNames($refresh), ['public']); } catch (NotSupportedException $e) { $schemaNames = []; } return $schemaNames; }
/** * @inheritdoc */ public function init() { $this->db = Instance::ensure($this->db, Connection::className()); $this->createTable(); if (Yii::$app instanceof WebApplication) { $id = Yii::$app->getRequest()->getCookies()->getValue($this->cookieKey); } else { $file = Yii::getAlias($this->cliFileKey); $id = is_file($file) ? (int) file_get_contents($file) : null; } if ($id === null || !is_numeric($id) || ($this->_states = $this->getData($id)) === false) { $primary = $this->db->getSchema()->insert($this->tableName, ['created_at' => time()]); $id = $primary['id']; $this->_states = []; } if (Yii::$app instanceof WebApplication) { $cookie = new Cookie(['name' => $this->cookieKey, 'value' => $id, 'expire' => time() + 30 * 24 * 3600]); Yii::$app->getResponse()->getCookies()->add($cookie); } elseif (isset($file)) { file_put_contents($file, $id, LOCK_EX); } $this->_states['id'] = $this->_id = $id; }
/** * @param string $attribute * @param string $value * @param array $formats * @param string $tablePrefix * @param Connection $db * @return array in format supported by Query::where() */ protected function getAttributeCondition($attribute, $value, $formats, $tablePrefix, $db) { $likeOp = $db->driverName === 'pgsql' ? 'ILIKE' : 'LIKE'; $columnName = $tablePrefix . '.' . $db->getSchema()->quoteSimpleColumnName($attribute); if (!is_array($value)) { $parts = explode(ActiveSearchInterface::TOKEN_SEPARATOR, $value); $parts = array_filter(array_map('trim', $parts)); if (count($parts) > 1) { // add the original value for an exact match $parts[] = trim($value); } $value = $parts; } switch ($formats[$attribute]) { default: if (!is_string($value) || strlen($value) < 2 || $value[0] !== '>' && $value[0] !== '<') { return [$columnName => $value]; } $op = substr($value, 0, $value[1] !== '=' ? 1 : 2); $value = substr($value, strlen($op)); if (trim($value) === '') { return []; } return [$op, $columnName, $value]; case 'string': case 'text': case 'email': case 'url': if (is_array($value)) { $result = ['or']; foreach ($value as $token) { $result[] = [$likeOp, $columnName, $token]; } return $result; } return [$likeOp, $columnName, $value]; case 'json': $subquery = (new Query())->select(1)->from('json_array_elements(' . $columnName . ') a')->where([$likeOp, 'a::text', $value]); return ['exists', $subquery]; } }
public function init() { parent::init(); $this->db = Instance::ensure($this->db, Connection::className()); $this->db->getSchema()->refresh(); }