code
stringlengths
17
296k
docstring
stringlengths
30
30.3k
func_name
stringlengths
1
89
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
7
153
url
stringlengths
51
209
license
stringclasses
4 values
protected function findPrimaryKey($table,$indices) { $indices=implode(', ',preg_split('/\s+/',$indices)); $sql=<<<EOD SELECT attnum, attname FROM pg_catalog.pg_attribute WHERE attrelid=( SELECT oid FROM pg_catalog.pg_class WHERE relname=:table AND relnamespace=( SELECT oid FROM pg_catalog.pg_namespace WHERE nspname=:schema ) ) AND attnum IN ({$indices}) EOD; $command=$this->getDbConnection()->createCommand($sql); $command->bindValue(':table',$table->name); $command->bindValue(':schema',$table->schemaName); foreach($command->queryAll() as $row) { $name=$row['attname']; if(isset($table->columns[$name])) { $table->columns[$name]->isPrimaryKey=true; if($table->primaryKey===null) $table->primaryKey=$name; elseif(is_string($table->primaryKey)) $table->primaryKey=array($table->primaryKey,$name); else $table->primaryKey[]=$name; } } }
Collects primary key information. @param CPgsqlTableSchema $table the table metadata @param string $indices pgsql primary key index list
findPrimaryKey
php
yiisoft/yii
framework/db/schema/pgsql/CPgsqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/pgsql/CPgsqlSchema.php
BSD-3-Clause
protected function findTableNames($schema='') { if($schema==='') $schema=self::DEFAULT_SCHEMA; $sql=<<<EOD SELECT table_name, table_schema FROM information_schema.tables WHERE table_schema=:schema AND table_type='BASE TABLE' EOD; $command=$this->getDbConnection()->createCommand($sql); $command->bindParam(':schema',$schema); $rows=$command->queryAll(); $names=array(); foreach($rows as $row) { if($schema===self::DEFAULT_SCHEMA) $names[]=$row['table_name']; else $names[]=$row['table_schema'].'.'.$row['table_name']; } return $names; }
Returns all table names in the database. @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. If not empty, the returned table names will be prefixed with the schema name. @return array all table names in the database.
findTableNames
php
yiisoft/yii
framework/db/schema/pgsql/CPgsqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/pgsql/CPgsqlSchema.php
BSD-3-Clause
public function renameTable($table, $newName) { return 'ALTER TABLE ' . $this->quoteTableName($table) . ' RENAME TO ' . $this->quoteTableName($newName); }
Builds a SQL statement for renaming a DB table. @param string $table the table to be renamed. The name will be properly quoted by the method. @param string $newName the new table name. The name will be properly quoted by the method. @return string the SQL statement for renaming a DB table. @since 1.1.6
renameTable
php
yiisoft/yii
framework/db/schema/pgsql/CPgsqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/pgsql/CPgsqlSchema.php
BSD-3-Clause
public function addColumn($table, $column, $type) { $type=$this->getColumnType($type); $sql='ALTER TABLE ' . $this->quoteTableName($table) . ' ADD COLUMN ' . $this->quoteColumnName($column) . ' ' . $type; return $sql; }
Builds a SQL statement for adding a new DB column. @param string $table the table that the new column will be added to. The table name will be properly quoted by the method. @param string $column the name of the new column. The name will be properly quoted by the method. @param string $type the column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. @return string the SQL statement for adding a new column. @since 1.1.6
addColumn
php
yiisoft/yii
framework/db/schema/pgsql/CPgsqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/pgsql/CPgsqlSchema.php
BSD-3-Clause
public function alterColumn($table, $column, $type) { $type=$this->getColumnType($type); $sql='ALTER TABLE ' . $this->quoteTableName($table) . ' ALTER COLUMN ' . $this->quoteColumnName($column) . ' TYPE ' . $this->getColumnType($type); return $sql; }
Builds a SQL statement for changing the definition of a column. @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. @param string $column the name of the column to be changed. The name will be properly quoted by the method. @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. @return string the SQL statement for changing the definition of a column. @since 1.1.6
alterColumn
php
yiisoft/yii
framework/db/schema/pgsql/CPgsqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/pgsql/CPgsqlSchema.php
BSD-3-Clause
public function dropIndex($name, $table) { return 'DROP INDEX '.$this->quoteTableName($name); }
Builds a SQL statement for dropping an index. @param string $name the name of the index to be dropped. The name will be properly quoted by the method. @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. @return string the SQL statement for dropping an index. @since 1.1.6
dropIndex
php
yiisoft/yii
framework/db/schema/pgsql/CPgsqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/pgsql/CPgsqlSchema.php
BSD-3-Clause
protected function createCommandBuilder() { return new CPgsqlCommandBuilder($this); }
Creates a command builder for the database. This method may be overridden by child classes to create a DBMS-specific command builder. @return CPgsqlCommandBuilder command builder instance.
createCommandBuilder
php
yiisoft/yii
framework/db/schema/pgsql/CPgsqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/pgsql/CPgsqlSchema.php
BSD-3-Clause
protected function getIntegerPrimaryKeyDefaultValue() { return 'DEFAULT'; }
Returns default value of the integer/serial primary key. Default value means that the next autoincrement/sequence value would be used. @return string default value of the integer/serial primary key. @since 1.1.14
getIntegerPrimaryKeyDefaultValue
php
yiisoft/yii
framework/db/schema/pgsql/CPgsqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/pgsql/CPgsqlCommandBuilder.php
BSD-3-Clause
public function quoteSimpleTableName($name) { return '['.$name.']'; }
Quotes a table name for use in a query. A simple table name does not schema prefix. @param string $name table name @return string the properly quoted table name @since 1.1.6
quoteSimpleTableName
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
public function quoteSimpleColumnName($name) { return '['.$name.']'; }
Quotes a column name for use in a query. A simple column name does not contain prefix. @param string $name column name @return string the properly quoted column name @since 1.1.6
quoteSimpleColumnName
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
public function compareTableNames($name1,$name2) { $name1=str_replace(array('[',']'),'',$name1); $name2=str_replace(array('[',']'),'',$name2); return parent::compareTableNames(strtolower($name1),strtolower($name2)); }
Compares two table names. The table names can be either quoted or unquoted. This method will consider both cases. @param string $name1 table name 1 @param string $name2 table name 2 @return boolean whether the two table names refer to the same table.
compareTableNames
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
protected function loadTable($name) { $table=new CMssqlTableSchema; $this->resolveTableNames($table,$name); //if (!in_array($table->name, $this->tableNames)) return null; $table->primaryKey=$this->findPrimaryKey($table); $table->foreignKeys=$this->findForeignKeys($table); if($this->findColumns($table)) { return $table; } else return null; }
Loads the metadata for the specified table. @param string $name table name @return CMssqlTableSchema driver dependent table metadata. Null if the table does not exist.
loadTable
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
protected function findPrimaryKey($table) { $kcu='INFORMATION_SCHEMA.KEY_COLUMN_USAGE'; $tc='INFORMATION_SCHEMA.TABLE_CONSTRAINTS'; if (isset($table->catalogName)) { $kcu=$table->catalogName.'.'.$kcu; $tc=$table->catalogName.'.'.$tc; } $sql = <<<EOD SELECT k.column_name field_name FROM {$this->quoteTableName($kcu)} k LEFT JOIN {$this->quoteTableName($tc)} c ON k.table_name = c.table_name AND k.constraint_name = c.constraint_name WHERE c.constraint_type ='PRIMARY KEY' AND k.table_name = :table AND k.table_schema = :schema EOD; $command = $this->getDbConnection()->createCommand($sql); $command->bindValue(':table', $table->name); $command->bindValue(':schema', $table->schemaName); $primary=$command->queryColumn(); switch (count($primary)) { case 0: // No primary key on table $primary=null; break; case 1: // Only 1 primary key $primary=$primary[0]; break; } return $primary; }
Gets the primary key column(s) details for the given table. @param CMssqlTableSchema $table table @return mixed primary keys (null if no pk, string if only 1 column pk, or array if composite pk)
findPrimaryKey
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
protected function findTableNames($schema='',$includeViews=true) { if($schema==='') $schema=self::DEFAULT_SCHEMA; if($includeViews) $condition="TABLE_TYPE in ('BASE TABLE','VIEW')"; else $condition="TABLE_TYPE='BASE TABLE'"; $sql=<<<EOD SELECT TABLE_NAME FROM [INFORMATION_SCHEMA].[TABLES] WHERE TABLE_SCHEMA=:schema AND $condition EOD; $command=$this->getDbConnection()->createCommand($sql); $command->bindParam(":schema", $schema); $rows=$command->queryAll(); $names=array(); foreach ($rows as $row) { if ($schema == self::DEFAULT_SCHEMA) $names[]=$row['TABLE_NAME']; else $names[]=$schema.'.'.$row['TABLE_NAME']; } return $names; }
Returns all table names in the database. @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. If not empty, the returned table names will be prefixed with the schema name. @param boolean $includeViews whether to include views in the result. Defaults to true. @return array all table names in the database.
findTableNames
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
protected function createCommandBuilder() { return new CMssqlCommandBuilder($this); }
Creates a command builder for the database. This method overrides parent implementation in order to create a MSSQL specific command builder @return CDbCommandBuilder command builder instance
createCommandBuilder
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
public function renameTable($table, $newName) { return "sp_rename '$table', '$newName'"; }
Builds a SQL statement for renaming a DB table. @param string $table the table to be renamed. The name will be properly quoted by the method. @param string $newName the new table name. The name will be properly quoted by the method. @return string the SQL statement for renaming a DB table. @since 1.1.6
renameTable
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
public function renameColumn($table, $name, $newName) { return "sp_rename '$table.$name', '$newName', 'COLUMN'"; }
Builds a SQL statement for renaming a column. @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. @param string $name the old name of the column. The name will be properly quoted by the method. @param string $newName the new name of the column. The name will be properly quoted by the method. @return string the SQL statement for renaming a DB column. @since 1.1.6
renameColumn
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
public function alterColumn($table, $column, $type) { $type=$this->getColumnType($type); $sql='ALTER TABLE ' . $this->quoteTableName($table) . ' ALTER COLUMN ' . $this->quoteColumnName($column) . ' ' . $this->getColumnType($type); return $sql; }
Builds a SQL statement for changing the definition of a column. @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. @param string $column the name of the column to be changed. The name will be properly quoted by the method. @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. @return string the SQL statement for changing the definition of a column. @since 1.1.6
alterColumn
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSchema.php
BSD-3-Clause
public function lastInsertId ($sequence=NULL) { return $this->query('SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS bigint)')->fetchColumn(); }
Get the last inserted id value MSSQL doesn't support sequence, so, argument is ignored @param string|null $sequence sequence name. Defaults to null @return integer last inserted id
lastInsertId
php
yiisoft/yii
framework/db/schema/mssql/CMssqlPdoAdapter.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlPdoAdapter.php
BSD-3-Clause
public function beginTransaction () { $this->exec('BEGIN TRANSACTION'); return true; }
Begin a transaction Is is necessary to override pdo's method, as mssql pdo drivers does not support transaction @return boolean
beginTransaction
php
yiisoft/yii
framework/db/schema/mssql/CMssqlPdoAdapter.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlPdoAdapter.php
BSD-3-Clause
public function commit () { $this->exec('COMMIT TRANSACTION'); return true; }
Commit a transaction Is is necessary to override pdo's method, as mssql pdo drivers does not support transaction @return boolean
commit
php
yiisoft/yii
framework/db/schema/mssql/CMssqlPdoAdapter.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlPdoAdapter.php
BSD-3-Clause
public function rollBack () { $this->exec('ROLLBACK TRANSACTION'); return true; }
Rollback a transaction Is is necessary to override pdo's method, ac mssql pdo drivers does not support transaction @return boolean
rollBack
php
yiisoft/yii
framework/db/schema/mssql/CMssqlPdoAdapter.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlPdoAdapter.php
BSD-3-Clause
public function init($dbType, $defaultValue) { if ($defaultValue=='(NULL)') { $defaultValue=null; } parent::init($dbType, $defaultValue); }
Initializes the column with its DB type and default value. This sets up the column's PHP type, size, precision, scale as well as default value. @param string $dbType the column's DB type @param mixed $defaultValue the default value
init
php
yiisoft/yii
framework/db/schema/mssql/CMssqlColumnSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlColumnSchema.php
BSD-3-Clause
protected function extractType($dbType) { if(strpos($dbType,'float')!==false || strpos($dbType,'real')!==false) $this->type='double'; elseif(strpos($dbType,'bigint')===false && (strpos($dbType,'int')!==false || strpos($dbType,'smallint')!==false || strpos($dbType,'tinyint'))) $this->type='integer'; elseif(strpos($dbType,'bit')!==false) $this->type='boolean'; else $this->type='string'; }
Extracts the PHP type from DB type. @param string $dbType DB type
extractType
php
yiisoft/yii
framework/db/schema/mssql/CMssqlColumnSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlColumnSchema.php
BSD-3-Clause
protected function extractDefault($defaultValue) { if($this->dbType==='timestamp' ) $this->defaultValue=null; else parent::extractDefault(str_replace(array('(',')',"'"), '', $defaultValue)); }
Extracts the default value for the column. The value is typecasted to correct PHP type. @param mixed $defaultValue the default value obtained from metadata
extractDefault
php
yiisoft/yii
framework/db/schema/mssql/CMssqlColumnSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlColumnSchema.php
BSD-3-Clause
protected function extractLimit($dbType) { }
Extracts size, precision and scale information from column's DB type. We do nothing here, since sizes and precisions have been computed before. @param string $dbType the column's DB type
extractLimit
php
yiisoft/yii
framework/db/schema/mssql/CMssqlColumnSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlColumnSchema.php
BSD-3-Clause
public function typecast($value) { if($this->type==='boolean') return $value ? 1 : 0; else return parent::typecast($value); }
Converts the input value to the type that this column is of. @param mixed $value input value @return mixed converted value
typecast
php
yiisoft/yii
framework/db/schema/mssql/CMssqlColumnSchema.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlColumnSchema.php
BSD-3-Clause
public function createCountCommand($table,$criteria,$alias='t') { $criteria->order=''; return parent::createCountCommand($table, $criteria,$alias); }
Creates a COUNT(*) command for a single table. Override parent implementation to remove the order clause of criteria if it exists @param CDbTableSchema $table the table metadata @param CDbCriteria $criteria the query criteria @param string $alias the alias name of the primary table. Defaults to 't'. @return CDbCommand query command.
createCountCommand
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
public function createFindCommand($table,$criteria,$alias='t') { $criteria=$this->checkCriteria($table,$criteria); return parent::createFindCommand($table,$criteria,$alias); }
Creates a SELECT command for a single table. Override parent implementation to check if an orderby clause if specified when querying with an offset @param CDbTableSchema $table the table metadata @param CDbCriteria $criteria the query criteria @param string $alias the alias name of the primary table. Defaults to 't'. @return CDbCommand query command.
createFindCommand
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
public function createDeleteCommand($table,$criteria) { $criteria=$this->checkCriteria($table, $criteria); return parent::createDeleteCommand($table, $criteria); }
Creates a DELETE command. Override parent implementation to check if an orderby clause if specified when querying with an offset @param CDbTableSchema $table the table metadata @param CDbCriteria $criteria the query criteria @return CDbCommand delete command.
createDeleteCommand
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
public function createUpdateCounterCommand($table,$counters,$criteria) { $criteria=$this->checkCriteria($table, $criteria); return parent::createUpdateCounterCommand($table, $counters, $criteria); }
Creates an UPDATE command that increments/decrements certain columns. Override parent implementation to check if an orderby clause if specified when querying with an offset @param CDbTableSchema $table the table metadata @param CDbCriteria $counters the query criteria @param array $criteria counters to be updated (counter increments/decrements indexed by column names.) @return CDbCommand the created command @throws CException if no counter is specified
createUpdateCounterCommand
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
public function applyJoin($sql,$join) { if(trim($join)!=='') $sql=preg_replace('/^\s*DELETE\s+FROM\s+((\[.+\])|([^\s]+))\s*/i',"DELETE \\1 FROM \\1",$sql); return parent::applyJoin($sql,$join); }
Alters the SQL to apply JOIN clause. Overrides parent implementation to comply with the DELETE command syntax required when multiple tables are referenced. @param string $sql the SQL statement to be altered @param string $join the JOIN clause (starting with join type, such as INNER JOIN) @return string the altered SQL statement
applyJoin
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
public function applyLimit($sql, $limit, $offset) { $limit = $limit!==null ? (int)$limit : -1; $offset = $offset!==null ? (int)$offset : -1; if($limit <= 0 && $offset <=0) // no limit, no offset return $sql; if($limit > 0 && $offset <= 0) // only limit return preg_replace('/^([\s(])*SELECT( DISTINCT)?(?!\s*TOP\s*\()/i',"\\1SELECT\\2 TOP $limit", $sql); if(version_compare($this->dbConnection->getServerVersion(), '11', '<')) return $this->oldRewriteLimitOffsetSql($sql, $limit, $offset); else return $this->newRewriteLimitOffsetSql($sql, $limit, $offset); }
Apply limit and offset to sql query @param string $sql SQL query string. @param integer $limit maximum number of rows, -1 to ignore limit. @param integer $offset row offset, -1 to ignore offset. @return string SQL with limit and offset. @see https://github.com/yiisoft/yii/issues/4491
applyLimit
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
protected function oldRewriteLimitOffsetSql($sql, $limit, $offset) { if ($limit <= 0) // Offset without limit has never worked for MSSQL 10 and older, see https://github.com/yiisoft/yii/pull/4501 return $sql; $fetch = $limit+$offset; $sql = preg_replace('/^([\s(])*SELECT( DISTINCT)?(?!\s*TOP\s*\()/i',"\\1SELECT\\2 TOP $fetch", $sql); $ordering = $this->findOrdering($sql); $originalOrdering = $this->joinOrdering($ordering, '[__outer__]'); $reverseOrdering = $this->joinOrdering($this->reverseDirection($ordering), '[__inner__]'); $sql = "SELECT * FROM (SELECT TOP {$limit} * FROM ($sql) as [__inner__] {$reverseOrdering}) as [__outer__] {$originalOrdering}"; return $sql; }
Rewrite sql to apply $limit and $offset for MSSQL database version 10 (2008) and lower. This is a port from Prado Framework. Overrides parent implementation. Alters the sql to apply $limit and $offset. The idea for limit with offset is done by modifying the sql on the fly with numerous assumptions on the structure of the sql string. The modification is done with reference to the notes from https://troels.arvin.dk/db/rdbms/#select-limit-offset <code> SELECT * FROM ( SELECT TOP n * FROM ( SELECT TOP z columns -- (z=n+skip) FROM tablename ORDER BY key ASC ) AS FOO ORDER BY key DESC -- ('FOO' may be anything) ) AS BAR ORDER BY key ASC -- ('BAR' may be anything) </code> <b>Regular expressions are used to alter the SQL query. The resulting SQL query may be malformed for complex queries.</b> The following restrictions apply <ul> <li> In particular, <b>commas</b> should <b>NOT</b> be used as part of the ordering expression or identifier. Commas must only be used for separating the ordering clauses. </li> <li> In the ORDER BY clause, the column name should NOT be be qualified with a table name or view name. Alias the column names or use column index. </li> <li> No clauses should follow the ORDER BY clause, e.g. no COMPUTE or FOR clauses. </li> </ul> @param string $sql sql query @param integer $limit $limit @param integer $offset $offset @return string modified sql query applied with limit and offset. @see https://troels.arvin.dk/db/rdbms/#select-limit-offset @see https://github.com/yiisoft/yii/issues/4491
oldRewriteLimitOffsetSql
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
protected function newRewriteLimitOffsetSql($sql, $limit, $offset) { // ORDER BY is required when using OFFSET and FETCH if(count($this->findOrdering($sql)) === 0) $sql .= " ORDER BY (SELECT NULL)"; $sql .= sprintf(" OFFSET %d ROWS", $offset); if($limit > 0) $sql .= sprintf(' FETCH NEXT %d ROWS ONLY', $limit); return $sql; }
Rewrite SQL to apply $limit and $offset for MSSQL database version 11 (2012) and newer. @see https://learn.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql?view=sql-server-ver15#using-offset-and-fetch-to-limit-the-rows-returned @see https://github.com/yiisoft/yii/issues/4491 @param string $sql sql query @param integer $limit $limit @param integer $offset $offset @return string modified sql query applied w th limit and offset.
newRewriteLimitOffsetSql
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
protected function checkCriteria($table, $criteria) { if ($criteria->offset > 0 && $criteria->order==='') { $criteria->order=is_array($table->primaryKey)?implode(',',$table->primaryKey):$table->primaryKey; } return $criteria; }
Checks if the criteria has an order by clause when using offset/limit. Override parent implementation to check if an orderby clause if specified when querying with an offset If not, order it by pk. @param CMssqlTableSchema $table table schema @param CDbCriteria $criteria criteria @return CDbCriteria the modified criteria
checkCriteria
php
yiisoft/yii
framework/db/schema/mssql/CMssqlCommandBuilder.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlCommandBuilder.php
BSD-3-Clause
public function lastInsertId($sequence=null) { $parts = explode('.', phpversion('pdo_sqlsrv')); $sqlsrvVer = phpversion('pdo_sqlsrv') ? intval(array_shift($parts)) : 0; if(!$sequence || $sqlsrvVer >= 5) return parent::lastInsertId(); return parent::lastInsertId($sequence); }
Returns last inserted ID value. Before version 5.0, the SQLSRV driver supports PDO::lastInsertId() with one peculiarity: when $sequence's value is null or empty string it returns empty string. But when parameter is not specified at all it's working as expected and returns actual last inserted ID (like other PDO drivers). Version 5.0 of the Microsoft PHP Drivers for SQL Server changes the behaviour of PDO::lastInsertID to be consistent with the behaviour outlined in the PDO documentation. It returns the ID of the last inserted sequence or row. @param string|null $sequence the sequence/table name. Defaults to null. @return integer last inserted ID value.
lastInsertId
php
yiisoft/yii
framework/db/schema/mssql/CMssqlSqlsrvPdoAdapter.php
https://github.com/yiisoft/yii/blob/master/framework/db/schema/mssql/CMssqlSqlsrvPdoAdapter.php
BSD-3-Clause
public function events() { return array_merge(parent::events(), array( 'onBeforeSave'=>'beforeSave', 'onAfterSave'=>'afterSave', 'onBeforeDelete'=>'beforeDelete', 'onAfterDelete'=>'afterDelete', 'onBeforeFind'=>'beforeFind', 'onAfterFind'=>'afterFind', 'onBeforeCount'=>'beforeCount', )); }
Declares events and the corresponding event handler methods. If you override this method, make sure you merge the parent result to the return value. @return array events (array keys) and the corresponding event handler methods (array values). @see CBehavior::events
events
php
yiisoft/yii
framework/db/ar/CActiveRecordBehavior.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecordBehavior.php
BSD-3-Clause
protected function beforeSave($event) { }
Responds to {@link CActiveRecord::onBeforeSave} event. Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}. You may set {@link CModelEvent::isValid} to be false to quit the saving process. @param CModelEvent $event event parameter
beforeSave
php
yiisoft/yii
framework/db/ar/CActiveRecordBehavior.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecordBehavior.php
BSD-3-Clause
protected function afterSave($event) { }
Responds to {@link CActiveRecord::onAfterSave} event. Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}. @param CEvent $event event parameter
afterSave
php
yiisoft/yii
framework/db/ar/CActiveRecordBehavior.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecordBehavior.php
BSD-3-Clause
protected function beforeDelete($event) { }
Responds to {@link CActiveRecord::onBeforeDelete} event. Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}. You may set {@link CModelEvent::isValid} to be false to quit the deletion process. @param CEvent $event event parameter
beforeDelete
php
yiisoft/yii
framework/db/ar/CActiveRecordBehavior.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecordBehavior.php
BSD-3-Clause
protected function afterDelete($event) { }
Responds to {@link CActiveRecord::onAfterDelete} event. Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}. @param CEvent $event event parameter
afterDelete
php
yiisoft/yii
framework/db/ar/CActiveRecordBehavior.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecordBehavior.php
BSD-3-Clause
protected function beforeFind($event) { }
Responds to {@link CActiveRecord::onBeforeFind} event. Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}. @param CEvent $event event parameter
beforeFind
php
yiisoft/yii
framework/db/ar/CActiveRecordBehavior.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecordBehavior.php
BSD-3-Clause
protected function afterFind($event) { }
Responds to {@link CActiveRecord::onAfterFind} event. Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}. @param CEvent $event event parameter
afterFind
php
yiisoft/yii
framework/db/ar/CActiveRecordBehavior.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecordBehavior.php
BSD-3-Clause
protected function beforeCount($event) { }
Responds to {@link CActiveRecord::onBeforeCount} event. Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}. @param CEvent $event event parameter @since 1.1.14
beforeCount
php
yiisoft/yii
framework/db/ar/CActiveRecordBehavior.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecordBehavior.php
BSD-3-Clause
public function __construct($model,$with) { $this->_builder=$model->getCommandBuilder(); $this->_joinTree=new CJoinElement($this,$model); $this->buildJoinTree($this->_joinTree,$with); }
Constructor. A join tree is built up based on the declared relationships between active record classes. @param CActiveRecord $model the model that initiates the active finding process @param mixed $with the relation names to be actively looked for
__construct
php
yiisoft/yii
framework/db/ar/CActiveFinder.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveFinder.php
BSD-3-Clause
public function query($criteria,$all=false) { $this->joinAll=$criteria->together===true; if($criteria->alias!='') { $this->_joinTree->tableAlias=$criteria->alias; $this->_joinTree->rawTableAlias=$this->_builder->getSchema()->quoteTableName($criteria->alias); } $this->_joinTree->find($criteria); $this->_joinTree->afterFind(); if($all) { $result = array_values($this->_joinTree->records); if ($criteria->index!==null) { $index=$criteria->index; $array=array(); foreach($result as $object) $array[$object->$index]=$object; $result=$array; } } elseif(count($this->_joinTree->records)) $result = reset($this->_joinTree->records); else $result = null; $this->destroyJoinTree(); return $result; }
Do not call this method. This method is used internally to perform the relational query based on the given DB criteria. @param CDbCriteria $criteria the DB criteria @param boolean $all whether to bring back all records @return mixed the query result
query
php
yiisoft/yii
framework/db/ar/CActiveFinder.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveFinder.php
BSD-3-Clause
public function count($criteria) { Yii::trace(get_class($this->_joinTree->model).'.count() eagerly','system.db.ar.CActiveRecord'); $this->joinAll=$criteria->together!==true; $alias=$criteria->alias===null ? 't' : $criteria->alias; $this->_joinTree->tableAlias=$alias; $this->_joinTree->rawTableAlias=$this->_builder->getSchema()->quoteTableName($alias); $n=$this->_joinTree->count($criteria); $this->destroyJoinTree(); return $n; }
This method is internally called. @param CDbCriteria $criteria the query criteria @return string
count
php
yiisoft/yii
framework/db/ar/CActiveFinder.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveFinder.php
BSD-3-Clause
public function lazyFind($baseRecord) { $this->_joinTree->lazyFind($baseRecord); if(!empty($this->_joinTree->children)) { foreach($this->_joinTree->children as $child) $child->afterFind(); } $this->destroyJoinTree(); }
Finds the related objects for the specified active record. This method is internally invoked by {@link CActiveRecord} to support lazy loading. @param CActiveRecord $baseRecord the base record whose related objects are to be loaded
lazyFind
php
yiisoft/yii
framework/db/ar/CActiveFinder.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveFinder.php
BSD-3-Clause
public function getModel($className) { return CActiveRecord::model($className); }
Given active record class name returns new model instance. @param string $className active record class name @return CActiveRecord active record model instance @since 1.1.14
getModel
php
yiisoft/yii
framework/db/ar/CActiveFinder.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveFinder.php
BSD-3-Clause
public function destroy() { if(!empty($this->children)) { foreach($this->children as $child) $child->destroy(); } unset($this->_finder, $this->_parent, $this->model, $this->relation, $this->master, $this->slave, $this->records, $this->children, $this->stats); }
Removes references to child elements and finder to avoid circular references. This is internally used.
destroy
php
yiisoft/yii
framework/db/ar/CActiveFinder.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveFinder.php
BSD-3-Clause
public function runQuery($query) { $command=$query->createCommand($this->_builder); foreach($command->queryAll() as $row) $this->populateRecord($query,$row); }
Executes the join query and populates the query results. @param CJoinQuery $query the query to be executed.
runQuery
php
yiisoft/yii
framework/db/ar/CActiveFinder.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveFinder.php
BSD-3-Clause
public function join($element) { if($element->slave!==null) $this->join($element->slave); if(!empty($element->relation->select)) $this->selects[]=$element->getColumnSelect($element->relation->select); $this->conditions[]=$element->relation->condition; $this->orders[]=$element->relation->order; $this->joins[]=$element->getJoinCondition(); $this->joins[]=$element->relation->join; $this->groups[]=$element->relation->group; $this->havings[]=$element->relation->having; if(is_array($element->relation->params)) { if(is_array($this->params)) $this->params=array_merge($this->params,$element->relation->params); else $this->params=$element->relation->params; } $this->elements[$element->id]=true; }
Joins with another join element @param CJoinElement $element the element to be joined
join
php
yiisoft/yii
framework/db/ar/CActiveFinder.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveFinder.php
BSD-3-Clause
public function __construct($scenario='insert') { if($scenario===null) // internally used by populateRecord() and model() return; $this->setScenario($scenario); $this->setIsNewRecord(true); $this->_attributes=$this->getMetaData()->attributeDefaults; $this->init(); $this->attachBehaviors($this->behaviors()); $this->afterConstruct(); }
Constructor. @param string $scenario scenario name. See {@link CModel::scenario} for more details about this parameter. Note: in order to setup initial model parameters use {@link init()} or {@link afterConstruct()}. Do NOT override the constructor unless it is absolutely necessary!
__construct
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function init() { }
Initializes this model. This method is invoked when an AR instance is newly created and has its {@link scenario} set. You may override this method to provide code that is needed to initialize the model (e.g. setting initial property values.)
init
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function cache($duration, $dependency=null, $queryCount=1) { $this->getDbConnection()->cache($duration, $dependency, $queryCount); return $this; }
Sets the parameters about query caching. This is a shortcut method to {@link CDbConnection::cache()}. It changes the query caching parameter of the {@link dbConnection} instance. @param integer $duration the number of seconds that query results may remain valid in cache. If this is 0, the caching will be disabled. @param CCacheDependency|ICacheDependency $dependency the dependency that will be used when saving the query results into cache. @param integer $queryCount number of SQL queries that need to be cached after calling this method. Defaults to 1, meaning that the next SQL query will be cached. @return static the active record instance itself. @since 1.1.7
cache
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function __sleep() { return array_keys((array)$this); }
PHP sleep magic method. This method ensures that the model meta data reference is set to null. @return array
__sleep
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function __get($name) { if(isset($this->_attributes[$name])) return $this->_attributes[$name]; elseif(isset($this->getMetaData()->columns[$name])) return null; elseif(isset($this->_related[$name])) return $this->_related[$name]; elseif(isset($this->getMetaData()->relations[$name])) return $this->getRelated($name); else return parent::__get($name); }
PHP getter magic method. This method is overridden so that AR attributes can be accessed like properties. @param string $name property name @return mixed property value @see getAttribute
__get
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function __set($name,$value) { if($this->setAttribute($name,$value)===false) { if(isset($this->getMetaData()->relations[$name])) $this->_related[$name]=$value; else parent::__set($name,$value); } }
PHP setter magic method. This method is overridden so that AR attributes can be accessed like properties. @param string $name property name @param mixed $value property value @throws CException
__set
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function __isset($name) { if(isset($this->_attributes[$name])) return true; elseif(isset($this->getMetaData()->columns[$name])) return false; elseif(isset($this->_related[$name])) return true; elseif(isset($this->getMetaData()->relations[$name])) return $this->getRelated($name)!==null; else return parent::__isset($name); }
Checks if a property value is null. This method overrides the parent implementation by checking if the named attribute is null or not. @param string $name the property name or the event name @return boolean whether the property value is null
__isset
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function __unset($name) { if(isset($this->getMetaData()->columns[$name])) unset($this->_attributes[$name]); elseif(isset($this->getMetaData()->relations[$name])) unset($this->_related[$name]); else parent::__unset($name); }
Sets a component property to be null. This method overrides the parent implementation by clearing the specified attribute value. @param string $name the property name or the event name @throws CException
__unset
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function __call($name,$parameters) { if(isset($this->getMetaData()->relations[$name])) { if(empty($parameters)) return $this->getRelated($name,false); else return $this->getRelated($name,false,$parameters[0]); } $scopes=$this->scopes(); if(isset($scopes[$name])) { $this->getDbCriteria()->mergeWith($scopes[$name]); return $this; } return parent::__call($name,$parameters); }
Calls the named method which is not a class method. Do not call this method. This is a PHP magic method that we override to implement the named scope feature. @param string $name the method name @param array $parameters method parameters @return mixed the method return value
__call
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function hasRelated($name) { return isset($this->_related[$name]) || array_key_exists($name,$this->_related); }
Returns a value indicating whether the named related object(s) has been loaded. @param string $name the relation name @return boolean a value indicating whether the named related object(s) has been loaded.
hasRelated
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getDbCriteria($createIfNull=true) { if($this->_c===null) { if(($c=$this->defaultScope())!==array() || $createIfNull) $this->_c=new CDbCriteria($c); } return $this->_c; }
Returns the query criteria associated with this model. @param boolean $createIfNull whether to create a criteria instance if it does not exist. Defaults to true. @return CDbCriteria the query criteria that is associated with this model. This criteria is mainly used by {@link scopes named scope} feature to accumulate different criteria specifications.
getDbCriteria
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function setDbCriteria($criteria) { $this->_c=$criteria; }
Sets the query criteria for the current model. @param CDbCriteria $criteria the query criteria @since 1.1.3
setDbCriteria
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function defaultScope() { return array(); }
Returns the default named scope that should be implicitly applied to all queries for this model. Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries. The default implementation simply returns an empty array. You may override this method if the model needs to be queried with some default criteria (e.g. only active records should be returned). @return array the query criteria. This will be used as the parameter to the constructor of {@link CDbCriteria}.
defaultScope
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function resetScope($resetDefault=true) { if($resetDefault) $this->_c=new CDbCriteria(); else $this->_c=null; return $this; }
Resets all scopes and criterias applied. @param boolean $resetDefault including default scope. This parameter available since 1.1.12 @return static the AR instance itself @since 1.1.2
resetScope
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getMetaData() { $className=get_class($this); if(!array_key_exists($className,self::$_md)) { self::$_md[$className]=null; // preventing recursive invokes of {@link getMetaData()} via {@link __get()} self::$_md[$className]=new CActiveRecordMetaData($this); } return self::$_md[$className]; }
Returns the meta-data for this AR @return CActiveRecordMetaData the meta for this AR class.
getMetaData
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function tableName() { $tableName = get_class($this); if(($pos=strrpos($tableName,'\\')) !== false) return substr($tableName,$pos+1); return $tableName; }
Returns the name of the associated database table. By default this method returns the class name as the table name. You may override this method if the table is not named after this convention. @return string the table name
tableName
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function attributeNames() { return array_keys($this->getMetaData()->columns); }
Returns the list of all attribute names of the model. This would return all column names of the table associated with this AR class. @return array list of attribute names.
attributeNames
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getDbConnection() { if(self::$db!==null) return self::$db; else { self::$db=Yii::app()->getDb(); if(self::$db instanceof CDbConnection) return self::$db; else throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.')); } }
Returns the database connection used by active record. By default, the "db" application component is used as the database connection. You may override this method if you want to use a different database connection. @throws CDbException if "db" application component is not defined @return CDbConnection the database connection used by active record.
getDbConnection
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getActiveRelation($name) { return isset($this->getMetaData()->relations[$name]) ? $this->getMetaData()->relations[$name] : null; }
Returns the named relation declared for this AR class. @param string $name the relation name @return CActiveRelation the named relation declared for this AR class. Null if the relation does not exist.
getActiveRelation
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getTableSchema() { return $this->getMetaData()->tableSchema; }
Returns the metadata of the table that this AR belongs to @return CDbTableSchema the metadata of the table that this AR belongs to
getTableSchema
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getCommandBuilder() { return $this->getDbConnection()->getSchema()->getCommandBuilder(); }
Returns the command builder used by this AR. @return CDbCommandBuilder the command builder used by this AR
getCommandBuilder
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function hasAttribute($name) { return isset($this->getMetaData()->columns[$name]); }
Checks whether this AR has the named attribute @param string $name attribute name @return boolean whether this AR has the named attribute (table column).
hasAttribute
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getAttribute($name) { if(property_exists($this,$name)) return $this->$name; elseif(isset($this->_attributes[$name])) return $this->_attributes[$name]; }
Returns the named attribute value. If this is a new record and the attribute is not set before, the default column value will be returned. If this record is the result of a query and the attribute is not loaded, null will be returned. You may also use $this->AttributeName to obtain the attribute value. @param string $name the attribute name @return mixed the attribute value. Null if the attribute is not set or does not exist. @see hasAttribute
getAttribute
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function setAttribute($name,$value) { if(property_exists($this,$name)) $this->$name=$value; elseif(isset($this->getMetaData()->columns[$name])) $this->_attributes[$name]=$value; else return false; return true; }
Sets the named attribute value. You may also use $this->AttributeName to set the attribute value. @param string $name the attribute name @param mixed $value the attribute value. @return boolean whether the attribute exists and the assignment is conducted successfully @see hasAttribute
setAttribute
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function addRelatedRecord($name,$record,$index) { if($index!==false) { if(!isset($this->_related[$name])) $this->_related[$name]=array(); if($record instanceof CActiveRecord) { if($index===true) $this->_related[$name][]=$record; else $this->_related[$name][$index]=$record; } } elseif(!isset($this->_related[$name])) $this->_related[$name]=$record; }
Do not call this method. This method is used internally by {@link CActiveFinder} to populate related objects. This method adds a related object to this record. @param string $name attribute name @param mixed $record the related record @param mixed $index the index value in the related object collection. If true, it means using zero-based integer index. If false, it means a HAS_ONE or BELONGS_TO object and no index is needed.
addRelatedRecord
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getIsNewRecord() { return $this->_new; }
Returns if the current record is new (was never saved to database) @return boolean whether the record is new and should be inserted when calling {@link save}. This property is automatically set in constructor and {@link populateRecord} and is set to false right after inserting record to database. Defaults to false, but it will be set to true if the instance is created using the new operator.
getIsNewRecord
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function onBeforeSave($event) { $this->raiseEvent('onBeforeSave',$event); }
This event is raised before the record is saved. By setting {@link CModelEvent::isValid} to be false, the normal {@link save()} process will be stopped. @param CModelEvent $event the event parameter
onBeforeSave
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function onAfterSave($event) { $this->raiseEvent('onAfterSave',$event); }
This event is raised after the record is saved. @param CEvent $event the event parameter
onAfterSave
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function onBeforeDelete($event) { $this->raiseEvent('onBeforeDelete',$event); }
This event is raised before the record is deleted. By setting {@link CModelEvent::isValid} to be false, the normal {@link delete()} process will be stopped. @param CModelEvent $event the event parameter
onBeforeDelete
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function onAfterDelete($event) { $this->raiseEvent('onAfterDelete',$event); }
This event is raised after the record is deleted. @param CEvent $event the event parameter
onAfterDelete
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function onBeforeFind($event) { $this->raiseEvent('onBeforeFind',$event); }
This event is raised before an AR finder performs a find call. This can be either a call to CActiveRecords find methods or a find call when model is loaded in relational context via lazy or eager loading. If you want to access or modify the query criteria used for the find call, you can use {@link getDbCriteria()} to customize it based on your needs. When modifying criteria in beforeFind you have to make sure you are using the right table alias which is different on normal find and relational call. You can use {@link getTableAlias()} to get the alias used for the upcoming find call. Please note that modification of criteria is fully supported as of version 1.1.13. Earlier versions had some problems with relational context and applying changes correctly. @param CModelEvent $event the event parameter @see beforeFind
onBeforeFind
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function onAfterFind($event) { $this->raiseEvent('onAfterFind',$event); }
This event is raised after the record is instantiated by a find method. @param CEvent $event the event parameter
onAfterFind
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function getActiveFinder($with) { return new CActiveFinder($this,$with); }
Given 'with' options returns a new active finder instance. @param mixed $with the relation names to be actively looked for @return CActiveFinder active finder for the operation @since 1.1.14
getActiveFinder
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function onBeforeCount($event) { $this->raiseEvent('onBeforeCount',$event); }
This event is raised before an AR finder performs a count call. If you want to access or modify the query criteria used for the count call, you can use {@link getDbCriteria()} to customize it based on your needs. When modifying criteria in beforeCount you have to make sure you are using the right table alias which is different on normal count and relational call. You can use {@link getTableAlias()} to get the alias used for the upcoming count call. @param CModelEvent $event the event parameter @see beforeCount @since 1.1.14
onBeforeCount
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
protected function beforeSave() { if($this->hasEventHandler('onBeforeSave')) { $event=new CModelEvent($this); $this->onBeforeSave($event); return $event->isValid; } else return true; }
This method is invoked before saving a record (after validation, if any). The default implementation raises the {@link onBeforeSave} event. You may override this method to do any preparation work for record saving. Use {@link isNewRecord} to determine whether the saving is for inserting or updating record. Make sure you call the parent implementation so that the event is raised properly. @return boolean whether the saving should be executed. Defaults to true.
beforeSave
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
protected function afterSave() { if($this->hasEventHandler('onAfterSave')) $this->onAfterSave(new CEvent($this)); }
This method is invoked after saving a record successfully. The default implementation raises the {@link onAfterSave} event. You may override this method to do postprocessing after record saving. Make sure you call the parent implementation so that the event is raised properly.
afterSave
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
protected function beforeDelete() { if($this->hasEventHandler('onBeforeDelete')) { $event=new CModelEvent($this); $this->onBeforeDelete($event); return $event->isValid; } else return true; }
This method is invoked before deleting a record. The default implementation raises the {@link onBeforeDelete} event. You may override this method to do any preparation work for record deletion. Make sure you call the parent implementation so that the event is raised properly. @return boolean whether the record should be deleted. Defaults to true.
beforeDelete
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
protected function afterDelete() { if($this->hasEventHandler('onAfterDelete')) $this->onAfterDelete(new CEvent($this)); }
This method is invoked after deleting a record. The default implementation raises the {@link onAfterDelete} event. You may override this method to do postprocessing after the record is deleted. Make sure you call the parent implementation so that the event is raised properly.
afterDelete
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
protected function beforeFind() { if($this->hasEventHandler('onBeforeFind')) { $event=new CModelEvent($this); $this->onBeforeFind($event); } }
This method is invoked before an AR finder executes a find call. The find calls include {@link find}, {@link findAll}, {@link findByPk}, {@link findAllByPk}, {@link findByAttributes}, {@link findAllByAttributes}, {@link findBySql} and {@link findAllBySql}. The default implementation raises the {@link onBeforeFind} event. If you override this method, make sure you call the parent implementation so that the event is raised properly. For details on modifying query criteria see {@link onBeforeFind} event.
beforeFind
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
protected function afterFind() { if($this->hasEventHandler('onAfterFind')) $this->onAfterFind(new CEvent($this)); }
This method is invoked after each record is instantiated by a find method. The default implementation raises the {@link onAfterFind} event. You may override this method to do postprocessing after each newly found record is instantiated. Make sure you call the parent implementation so that the event is raised properly.
afterFind
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function beforeFindInternal() { $this->beforeFind(); }
Calls {@link beforeFind}. This method is internally used.
beforeFindInternal
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function afterFindInternal() { $this->afterFind(); }
Calls {@link afterFind}. This method is internally used.
afterFindInternal
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function insert($attributes=null) { if(!$this->getIsNewRecord()) throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.')); if($this->beforeSave()) { Yii::trace(get_class($this).'.insert()','system.db.ar.CActiveRecord'); $builder=$this->getCommandBuilder(); $table=$this->getTableSchema(); $command=$builder->createInsertCommand($table,$this->getAttributes($attributes)); if($command->execute()) { $primaryKey=$table->primaryKey; if($table->sequenceName!==null) { if(is_string($primaryKey) && $this->$primaryKey===null) $this->$primaryKey=$builder->getLastInsertID($table); elseif(is_array($primaryKey)) { foreach($primaryKey as $pk) { if($this->$pk===null) { $this->$pk=$builder->getLastInsertID($table); break; } } } } $this->_pk=$this->getPrimaryKey(); $this->afterSave(); $this->setIsNewRecord(false); $this->setScenario('update'); return true; } } return false; }
Inserts a row into the table based on this active record attributes. If the table's primary key is auto-incremental and is null before insertion, it will be populated with the actual value after insertion. Note, validation is not performed in this method. You may call {@link validate} to perform the validation. After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false, and its {@link scenario} property will be set to be 'update'. @param array $attributes list of attributes that need to be saved. Defaults to null, meaning all attributes that are loaded from DB will be saved. @return boolean whether the attributes are valid and the record is inserted successfully. @throws CDbException if the record is not new
insert
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function update($attributes=null) { if($this->getIsNewRecord()) throw new CDbException(Yii::t('yii','The active record cannot be updated because it is new.')); if($this->beforeSave()) { Yii::trace(get_class($this).'.update()','system.db.ar.CActiveRecord'); if($this->_pk===null) $this->_pk=$this->getPrimaryKey(); $this->updateByPk($this->getOldPrimaryKey(),$this->getAttributes($attributes)); $this->_pk=$this->getPrimaryKey(); $this->afterSave(); return true; } else return false; }
Updates the row represented by this active record. All loaded attributes will be saved to the database. Note, validation is not performed in this method. You may call {@link validate} to perform the validation. @param array $attributes list of attributes that need to be saved. Defaults to null, meaning all attributes that are loaded from DB will be saved. @return boolean whether the update is successful @throws CDbException if the record is new
update
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function delete() { if(!$this->getIsNewRecord()) { Yii::trace(get_class($this).'.delete()','system.db.ar.CActiveRecord'); if($this->beforeDelete()) { $result=$this->deleteByPk($this->getPrimaryKey())>0; $this->afterDelete(); return $result; } else return false; } else throw new CDbException(Yii::t('yii','The active record cannot be deleted because it is new.')); }
Deletes the row corresponding to this active record. @throws CDbException if the record is new @return boolean whether the deletion is successful.
delete
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function refresh() { Yii::trace(get_class($this).'.refresh()','system.db.ar.CActiveRecord'); if(($record=$this->findByPk($this->getPrimaryKey()))!==null) { $this->_attributes=array(); $this->_related=array(); foreach($this->getMetaData()->columns as $name=>$column) { if(property_exists($this,$name)) $this->$name=$record->$name; else $this->_attributes[$name]=$record->$name; } return true; } else return false; }
Repopulates this active record with the latest data. @return boolean whether the row still exists in the database. If true, the latest data will be populated to this active record.
refresh
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause
public function equals($record) { return $this->tableName()===$record->tableName() && $this->getPrimaryKey()===$record->getPrimaryKey(); }
Compares current active record with another one. The comparison is made by comparing table name and the primary key values of the two active records. @param CActiveRecord $record record to compare to @return boolean whether the two active records refer to the same row in the database table.
equals
php
yiisoft/yii
framework/db/ar/CActiveRecord.php
https://github.com/yiisoft/yii/blob/master/framework/db/ar/CActiveRecord.php
BSD-3-Clause