|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
-
- namespace yii\db;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Inflector;
- use yii\helpers\StringHelper;
-
- /**
- * ActiveRecord is the base class for classes representing relational data in terms of objects.
- *
- * Active Record implements the [Active Record design pattern](http://en.wikipedia.org/wiki/Active_record).
- * The premise behind Active Record is that an individual [[ActiveRecord]] object is associated with a specific
- * row in a database table. The object's attributes are mapped to the columns of the corresponding table.
- * Referencing an Active Record attribute is equivalent to accessing the corresponding table column for that record.
- *
- * As an example, say that the `Customer` ActiveRecord class is associated with the `customer` table.
- * This would mean that the class's `name` attribute is automatically mapped to the `name` column in `customer` table.
- * Thanks to Active Record, assuming the variable `$customer` is an object of type `Customer`, to get the value of
- * the `name` column for the table row, you can use the expression `$customer->name`.
- * In this example, Active Record is providing an object-oriented interface for accessing data stored in the database.
- * But Active Record provides much more functionality than this.
- *
- * To declare an ActiveRecord class you need to extend [[\yii\db\ActiveRecord]] and
- * implement the `tableName` method:
- *
- * ```php
- * <?php
- *
- * class Customer extends \yii\db\ActiveRecord
- * {
- * public static function tableName()
- * {
- * return 'customer';
- * }
- * }
- * ```
- *
- * The `tableName` method only has to return the name of the database table associated with the class.
- *
- * > Tip: You may also use the [Gii code generator](guide:start-gii) to generate ActiveRecord classes from your
- * > database tables.
- *
- * Class instances are obtained in one of two ways:
- *
- * * Using the `new` operator to create a new, empty object
- * * Using a method to fetch an existing record (or records) from the database
- *
- * Here is a short teaser how working with an ActiveRecord looks like:
- *
- * ```php
- * $user = new User();
- * $user->name = 'Qiang';
- * $user->save(); // a new row is inserted into user table
- *
- * // the following will retrieve the user 'CeBe' from the database
- * $user = User::find()->where(['name' => 'CeBe'])->one();
- *
- * // this will get related records from orders table when relation is defined
- * $orders = $user->orders;
- * ```
- *
- * For more details and usage information on ActiveRecord, see the [guide article on ActiveRecord](guide:db-active-record).
- *
- * @method ActiveQuery hasMany(string $class, array $link) see BaseActiveRecord::hasMany() for more info
- * @method ActiveQuery hasOne(string $class, array $link) see BaseActiveRecord::hasOne() for more info
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @author Carsten Brandt <mail@cebe.cc>
- * @since 2.0
- */
- class ActiveRecord extends BaseActiveRecord
- {
- /**
- * The insert operation. This is mainly used when overriding [[transactions()]] to specify which operations are transactional.
- */
- const OP_INSERT = 0x01;
- /**
- * The update operation. This is mainly used when overriding [[transactions()]] to specify which operations are transactional.
- */
- const OP_UPDATE = 0x02;
- /**
- * The delete operation. This is mainly used when overriding [[transactions()]] to specify which operations are transactional.
- */
- const OP_DELETE = 0x04;
- /**
- * All three operations: insert, update, delete.
- * This is a shortcut of the expression: OP_INSERT | OP_UPDATE | OP_DELETE.
- */
- const OP_ALL = 0x07;
-
-
- /**
- * Loads default values from database table schema
- *
- * You may call this method to load default values after creating a new instance:
- *
- * ```php
- * // class Customer extends \yii\db\ActiveRecord
- * $customer = new Customer();
- * $customer->loadDefaultValues();
- * ```
- *
- * @param boolean $skipIfSet whether existing value should be preserved.
- * This will only set defaults for attributes that are `null`.
- * @return static the model instance itself.
- */
- public function loadDefaultValues($skipIfSet = true)
- {
- foreach ($this->getTableSchema()->columns as $column) {
- if ($column->defaultValue !== null && (!$skipIfSet || $this->{$column->name} === null)) {
- $this->{$column->name} = $column->defaultValue;
- }
- }
- return $this;
- }
-
- /**
- * Returns the database connection used by this AR class.
- * 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.
- * @return Connection the database connection used by this AR class.
- */
- public static function getDb()
- {
- return Yii::$app->getDb();
- }
-
- /**
- * Creates an [[ActiveQuery]] instance with a given SQL statement.
- *
- * Note that because the SQL statement is already specified, calling additional
- * query modification methods (such as `where()`, `order()`) on the created [[ActiveQuery]]
- * instance will have no effect. However, calling `with()`, `asArray()` or `indexBy()` is
- * still fine.
- *
- * Below is an example:
- *
- * ~~~
- * $customers = Customer::findBySql('SELECT * FROM customer')->all();
- * ~~~
- *
- * @param string $sql the SQL statement to be executed
- * @param array $params parameters to be bound to the SQL statement during execution.
- * @return ActiveQuery the newly created [[ActiveQuery]] instance
- */
- public static function findBySql($sql, $params = [])
- {
- $query = static::find();
- $query->sql = $sql;
-
- return $query->params($params);
- }
-
- /**
- * Finds ActiveRecord instance(s) by the given condition.
- * This method is internally called by [[findOne()]] and [[findAll()]].
- * @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter
- * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance.
- * @throws InvalidConfigException if there is no primary key defined
- * @internal
- */
- protected static function findByCondition($condition)
- {
- $query = static::find();
-
- if (!ArrayHelper::isAssociative($condition)) {
- // query by primary key
- $primaryKey = static::primaryKey();
- if (isset($primaryKey[0])) {
- $pk = $primaryKey[0];
- if (!empty($query->join) || !empty($query->joinWith)) {
- $pk = static::tableName() . '.' . $pk;
- }
- $condition = [$pk => $condition];
- } else {
- throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.');
- }
- }
-
- return $query->andWhere($condition);
- }
-
- /**
- * Updates the whole table using the provided attribute values and conditions.
- * For example, to change the status to be 1 for all customers whose status is 2:
- *
- * ~~~
- * Customer::updateAll(['status' => 1], 'status = 2');
- * ~~~
- *
- * @param array $attributes attribute values (name-value pairs) to be saved into the table
- * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
- * Please refer to [[Query::where()]] on how to specify this parameter.
- * @param array $params the parameters (name => value) to be bound to the query.
- * @return integer the number of rows updated
- */
- public static function updateAll($attributes, $condition = '', $params = [])
- {
- $command = static::getDb()->createCommand();
- $command->update(static::tableName(), $attributes, $condition, $params);
-
- return $command->execute();
- }
-
- /**
- * Updates the whole table using the provided counter changes and conditions.
- * For example, to increment all customers' age by 1,
- *
- * ~~~
- * Customer::updateAllCounters(['age' => 1]);
- * ~~~
- *
- * @param array $counters the counters to be updated (attribute name => increment value).
- * Use negative values if you want to decrement the counters.
- * @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
- * Please refer to [[Query::where()]] on how to specify this parameter.
- * @param array $params the parameters (name => value) to be bound to the query.
- * Do not name the parameters as `:bp0`, `:bp1`, etc., because they are used internally by this method.
- * @return integer the number of rows updated
- */
- public static function updateAllCounters($counters, $condition = '', $params = [])
- {
- $n = 0;
- foreach ($counters as $name => $value) {
- $counters[$name] = new Expression("[[$name]]+:bp{$n}", [":bp{$n}" => $value]);
- $n++;
- }
- $command = static::getDb()->createCommand();
- $command->update(static::tableName(), $counters, $condition, $params);
-
- return $command->execute();
- }
-
- /**
- * Deletes rows in the table using the provided conditions.
- * WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
- *
- * For example, to delete all customers whose status is 3:
- *
- * ~~~
- * Customer::deleteAll('status = 3');
- * ~~~
- *
- * @param string|array $condition the conditions that will be put in the WHERE part of the DELETE SQL.
- * Please refer to [[Query::where()]] on how to specify this parameter.
- * @param array $params the parameters (name => value) to be bound to the query.
- * @return integer the number of rows deleted
- */
- public static function deleteAll($condition = '', $params = [])
- {
- $command = static::getDb()->createCommand();
- $command->delete(static::tableName(), $condition, $params);
-
- return $command->execute();
- }
-
- /**
- * @inheritdoc
- * @return ActiveQuery the newly created [[ActiveQuery]] instance.
- */
- public static function find()
- {
- return Yii::createObject(ActiveQuery::className(), [get_called_class()]);
- }
-
- /**
- * Declares the name of the database table associated with this AR class.
- * By default this method returns the class name as the table name by calling [[Inflector::camel2id()]]
- * with prefix [[Connection::tablePrefix]]. For example if [[Connection::tablePrefix]] is 'tbl_',
- * 'Customer' becomes 'tbl_customer', and 'OrderItem' becomes 'tbl_order_item'. You may override this method
- * if the table is not named after this convention.
- * @return string the table name
- */
- public static function tableName()
- {
- return '{{%' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_') . '}}';
- }
-
- /**
- * Returns the schema information of the DB table associated with this AR class.
- * @return TableSchema the schema information of the DB table associated with this AR class.
- * @throws InvalidConfigException if the table for the AR class does not exist.
- */
- public static function getTableSchema()
- {
- $schema = static::getDb()->getSchema()->getTableSchema(static::tableName());
- if ($schema !== null) {
- return $schema;
- } else {
- throw new InvalidConfigException("The table does not exist: " . static::tableName());
- }
- }
-
- /**
- * Returns the primary key name(s) for this AR class.
- * The default implementation will return the primary key(s) as declared
- * in the DB table that is associated with this AR class.
- *
- * If the DB table does not declare any primary key, you should override
- * this method to return the attributes that you want to use as primary keys
- * for this AR class.
- *
- * Note that an array should be returned even for a table with single primary key.
- *
- * @return string[] the primary keys of the associated database table.
- */
- public static function primaryKey()
- {
- return static::getTableSchema()->primaryKey;
- }
-
- /**
- * Returns the list of all attribute names of the model.
- * The default implementation will return all column names of the table associated with this AR class.
- * @return array list of attribute names.
- */
- public function attributes()
- {
- return array_keys(static::getTableSchema()->columns);
- }
-
- /**
- * Declares which DB operations should be performed within a transaction in different scenarios.
- * The supported DB operations are: [[OP_INSERT]], [[OP_UPDATE]] and [[OP_DELETE]],
- * which correspond to the [[insert()]], [[update()]] and [[delete()]] methods, respectively.
- * By default, these methods are NOT enclosed in a DB transaction.
- *
- * In some scenarios, to ensure data consistency, you may want to enclose some or all of them
- * in transactions. You can do so by overriding this method and returning the operations
- * that need to be transactional. For example,
- *
- * ~~~
- * return [
- * 'admin' => self::OP_INSERT,
- * 'api' => self::OP_INSERT | self::OP_UPDATE | self::OP_DELETE,
- * // the above is equivalent to the following:
- * // 'api' => self::OP_ALL,
- *
- * ];
- * ~~~
- *
- * The above declaration specifies that in the "admin" scenario, the insert operation ([[insert()]])
- * should be done in a transaction; and in the "api" scenario, all the operations should be done
- * in a transaction.
- *
- * @return array the declarations of transactional operations. The array keys are scenarios names,
- * and the array values are the corresponding transaction operations.
- */
- public function transactions()
- {
- return [];
- }
-
- /**
- * @inheritdoc
- */
- public static function populateRecord($record, $row)
- {
- $columns = static::getTableSchema()->columns;
- foreach ($row as $name => $value) {
- if (isset($columns[$name])) {
- $row[$name] = $columns[$name]->phpTypecast($value);
- }
- }
- parent::populateRecord($record, $row);
- }
-
- /**
- * Inserts a row into the associated database table using the attribute values of this record.
- *
- * This method performs the following steps in order:
- *
- * 1. call [[beforeValidate()]] when `$runValidation` is true. If validation
- * fails, it will skip the rest of the steps;
- * 2. call [[afterValidate()]] when `$runValidation` is true.
- * 3. call [[beforeSave()]]. If the method returns false, it will skip the
- * rest of the steps;
- * 4. insert the record into database. If this fails, it will skip the rest of the steps;
- * 5. call [[afterSave()]];
- *
- * In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]],
- * [[EVENT_BEFORE_INSERT]], [[EVENT_AFTER_INSERT]] and [[EVENT_AFTER_VALIDATE]]
- * will be raised by the corresponding methods.
- *
- * Only the [[dirtyAttributes|changed attribute values]] will be inserted into database.
- *
- * If the table's primary key is auto-incremental and is null during insertion,
- * it will be populated with the actual value after insertion.
- *
- * For example, to insert a customer record:
- *
- * ~~~
- * $customer = new Customer;
- * $customer->name = $name;
- * $customer->email = $email;
- * $customer->insert();
- * ~~~
- *
- * @param boolean $runValidation whether to perform validation before saving the record.
- * If the validation fails, the record will not be inserted into the database.
- * @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 \Exception in case insert failed.
- */
- public function insert($runValidation = true, $attributes = null)
- {
- if ($runValidation && !$this->validate($attributes)) {
- Yii::info('Model not inserted due to validation error.', __METHOD__);
- return false;
- }
-
- if (!$this->isTransactional(self::OP_INSERT)) {
- return $this->insertInternal($attributes);
- }
-
- $transaction = static::getDb()->beginTransaction();
- try {
- $result = $this->insertInternal($attributes);
- if ($result === false) {
- $transaction->rollBack();
- } else {
- $transaction->commit();
- }
- return $result;
- } catch (\Exception $e) {
- $transaction->rollBack();
- throw $e;
- }
- }
-
- /**
- * Inserts an ActiveRecord into DB without considering transaction.
- * @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 record is inserted successfully.
- */
- protected function insertInternal($attributes = null)
- {
- if (!$this->beforeSave(true)) {
- return false;
- }
- $values = $this->getDirtyAttributes($attributes);
- if (empty($values)) {
- foreach ($this->getPrimaryKey(true) as $key => $value) {
- $values[$key] = $value;
- }
- }
- $db = static::getDb();
- $command = $db->createCommand()->insert($this->tableName(), $values);
- if (!$command->execute()) {
- return false;
- }
- $table = $this->getTableSchema();
- if ($table->sequenceName !== null) {
- foreach ($table->primaryKey as $name) {
- if ($this->getAttribute($name) === null) {
- $id = $table->columns[$name]->phpTypecast($db->getLastInsertID($table->sequenceName));
- $this->setAttribute($name, $id);
- $values[$name] = $id;
- break;
- }
- }
- }
-
- $changedAttributes = array_fill_keys(array_keys($values), null);
- $this->setOldAttributes($values);
- $this->afterSave(true, $changedAttributes);
-
- return true;
- }
-
- /**
- * Saves the changes to this active record into the associated database table.
- *
- * This method performs the following steps in order:
- *
- * 1. call [[beforeValidate()]] when `$runValidation` is true. If validation
- * fails, it will skip the rest of the steps;
- * 2. call [[afterValidate()]] when `$runValidation` is true.
- * 3. call [[beforeSave()]]. If the method returns false, it will skip the
- * rest of the steps;
- * 4. save the record into database. If this fails, it will skip the rest of the steps;
- * 5. call [[afterSave()]];
- *
- * In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]],
- * [[EVENT_BEFORE_UPDATE]], [[EVENT_AFTER_UPDATE]] and [[EVENT_AFTER_VALIDATE]]
- * will be raised by the corresponding methods.
- *
- * Only the [[dirtyAttributes|changed attribute values]] will be saved into database.
- *
- * For example, to update a customer record:
- *
- * ~~~
- * $customer = Customer::findOne($id);
- * $customer->name = $name;
- * $customer->email = $email;
- * $customer->update();
- * ~~~
- *
- * Note that it is possible the update does not affect any row in the table.
- * In this case, this method will return 0. For this reason, you should use the following
- * code to check if update() is successful or not:
- *
- * ~~~
- * if ($this->update() !== false) {
- * // update successful
- * } else {
- * // update failed
- * }
- * ~~~
- *
- * @param boolean $runValidation whether to perform validation before saving the record.
- * If the validation fails, the record will not be inserted into the database.
- * @param array $attributeNames list of attributes that need to be saved. Defaults to null,
- * meaning all attributes that are loaded from DB will be saved.
- * @return integer|boolean the number of rows affected, or false if validation fails
- * or [[beforeSave()]] stops the updating process.
- * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
- * being updated is outdated.
- * @throws \Exception in case update failed.
- */
- public function update($runValidation = true, $attributeNames = null)
- {
- if ($runValidation && !$this->validate($attributeNames)) {
- Yii::info('Model not updated due to validation error.', __METHOD__);
- return false;
- }
-
- if (!$this->isTransactional(self::OP_UPDATE)) {
- return $this->updateInternal($attributeNames);
- }
-
- $transaction = static::getDb()->beginTransaction();
- try {
- $result = $this->updateInternal($attributeNames);
- if ($result === false) {
- $transaction->rollBack();
- } else {
- $transaction->commit();
- }
- return $result;
- } catch (\Exception $e) {
- $transaction->rollBack();
- throw $e;
- }
- }
-
- /**
- * Deletes the table row corresponding to this active record.
- *
- * This method performs the following steps in order:
- *
- * 1. call [[beforeDelete()]]. If the method returns false, it will skip the
- * rest of the steps;
- * 2. delete the record from the database;
- * 3. call [[afterDelete()]].
- *
- * In the above step 1 and 3, events named [[EVENT_BEFORE_DELETE]] and [[EVENT_AFTER_DELETE]]
- * will be raised by the corresponding methods.
- *
- * @return integer|false the number of rows deleted, or false if the deletion is unsuccessful for some reason.
- * Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
- * @throws StaleObjectException if [[optimisticLock|optimistic locking]] is enabled and the data
- * being deleted is outdated.
- * @throws \Exception in case delete failed.
- */
- public function delete()
- {
- if (!$this->isTransactional(self::OP_DELETE)) {
- return $this->deleteInternal();
- }
-
- $transaction = static::getDb()->beginTransaction();
- try {
- $result = $this->deleteInternal();
- if ($result === false) {
- $transaction->rollBack();
- } else {
- $transaction->commit();
- }
- return $result;
- } catch (\Exception $e) {
- $transaction->rollBack();
- throw $e;
- }
- }
-
- /**
- * Deletes an ActiveRecord without considering transaction.
- * @return integer|false the number of rows deleted, or false if the deletion is unsuccessful for some reason.
- * Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
- * @throws StaleObjectException
- */
- protected function deleteInternal()
- {
- if (!$this->beforeDelete()) {
- return false;
- }
-
- // we do not check the return value of deleteAll() because it's possible
- // the record is already deleted in the database and thus the method will return 0
- $condition = $this->getOldPrimaryKey(true);
- $lock = $this->optimisticLock();
- if ($lock !== null) {
- $condition[$lock] = $this->$lock;
- }
- $result = $this->deleteAll($condition);
- if ($lock !== null && !$result) {
- throw new StaleObjectException('The object being deleted is outdated.');
- }
- $this->setOldAttributes(null);
- $this->afterDelete();
-
- return $result;
- }
-
- /**
- * Returns a value indicating whether the given active record is the same as the current one.
- * The comparison is made by comparing the table names and the primary key values of the two active records.
- * If one of the records [[isNewRecord|is new]] they are also considered not equal.
- * @param ActiveRecord $record record to compare to
- * @return boolean whether the two active records refer to the same row in the same database table.
- */
- public function equals($record)
- {
- if ($this->isNewRecord || $record->isNewRecord) {
- return false;
- }
-
- return $this->tableName() === $record->tableName() && $this->getPrimaryKey() === $record->getPrimaryKey();
- }
-
- /**
- * Returns a value indicating whether the specified operation is transactional in the current [[scenario]].
- * @param integer $operation the operation to check. Possible values are [[OP_INSERT]], [[OP_UPDATE]] and [[OP_DELETE]].
- * @return boolean whether the specified operation is transactional in the current [[scenario]].
- */
- public function isTransactional($operation)
- {
- $scenario = $this->getScenario();
- $transactions = $this->transactions();
-
- return isset($transactions[$scenario]) && ($transactions[$scenario] & $operation);
- }
- }
|