123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- namespace yii\mongodb;
- use MongoDB\BSON\Binary;
- use MongoDB\BSON\Type;
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\db\BaseActiveRecord;
- use yii\db\StaleObjectException;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Inflector;
- use yii\helpers\StringHelper;
- /**
- * ActiveRecord is the base class for classes representing Mongo documents in terms of objects.
- *
- * @author Paul Klimov <klimov.paul@gmail.com>
- * @since 2.0
- */
- abstract class ActiveRecord extends BaseActiveRecord
- {
- /**
- * Returns the Mongo connection used by this AR class.
- * By default, the "mongodb" application component is used as the Mongo 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->get('mongodb');
- }
- /**
- * Updates all documents in the collection using the provided attribute values and conditions.
- * For example, to change the status to be 1 for all customers whose status is 2:
- *
- * ```php
- * Customer::updateAll(['status' => 1], ['status' => 2]);
- * ```
- *
- * @param array $attributes attribute values (name-value pairs) to be saved into the collection
- * @param array $condition description of the objects to update.
- * Please refer to [[Query::where()]] on how to specify this parameter.
- * @param array $options list of options in format: optionName => optionValue.
- * @return int the number of documents updated.
- */
- public static function updateAll($attributes, $condition = [], $options = [])
- {
- return static::getCollection()->update($condition, $attributes, $options);
- }
- /**
- * Updates all documents in the collection using the provided counter changes and conditions.
- * For example, to increment all customers' age by 1,
- *
- * ```php
- * 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 array $condition description of the objects to update.
- * Please refer to [[Query::where()]] on how to specify this parameter.
- * @param array $options list of options in format: optionName => optionValue.
- * @return int the number of documents updated.
- */
- public static function updateAllCounters($counters, $condition = [], $options = [])
- {
- return static::getCollection()->update($condition, ['$inc' => $counters], $options);
- }
- /**
- * Deletes documents in the collection using the provided conditions.
- * WARNING: If you do not specify any condition, this method will delete documents rows in the collection.
- *
- * For example, to delete all customers whose status is 3:
- *
- * ```php
- * Customer::deleteAll(['status' => 3]);
- * ```
- *
- * @param array $condition description of the objects to delete.
- * Please refer to [[Query::where()]] on how to specify this parameter.
- * @param array $options list of options in format: optionName => optionValue.
- * @return int the number of documents deleted.
- */
- public static function deleteAll($condition = [], $options = [])
- {
- return static::getCollection()->remove($condition, $options);
- }
- /**
- * {@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 Mongo collection associated with this AR class.
- *
- * Collection name can be either a string or array:
- * - if string considered as the name of the collection inside the default database.
- * - if array - first element considered as the name of the database, second - as
- * name of collection inside that database
- *
- * By default this method returns the class name as the collection name by calling [[Inflector::camel2id()]].
- * For example, 'Customer' becomes 'customer', and 'OrderItem' becomes
- * 'order_item'. You may override this method if the collection is not named after this convention.
- * @return string|array the collection name
- */
- public static function collectionName()
- {
- return Inflector::camel2id(StringHelper::basename(get_called_class()), '_');
- }
- /**
- * Return the Mongo collection instance for this AR class.
- * @return Collection collection instance.
- */
- public static function getCollection()
- {
- return static::getDb()->getCollection(static::collectionName());
- }
- /**
- * Returns the primary key name(s) for this AR class.
- * The default implementation will return ['_id'].
- *
- * Note that an array should be returned even for a collection with single primary key.
- *
- * @return string[] the primary keys of the associated Mongo collection.
- */
- public static function primaryKey()
- {
- return ['_id'];
- }
- /**
- * Returns the list of all attribute names of the model.
- * This method must be overridden by child classes to define available attributes.
- * Note: primary key attribute "_id" should be always present in returned array.
- * For example:
- *
- * ```php
- * public function attributes()
- * {
- * return ['_id', 'name', 'address', 'status'];
- * }
- * ```
- *
- * @throws \yii\base\InvalidConfigException if not implemented
- * @return array list of attribute names.
- */
- public function attributes()
- {
- throw new InvalidConfigException('The attributes() method of mongodb ActiveRecord has to be implemented by child classes.');
- }
- /**
- * Inserts a row into the associated Mongo collection 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 collection. 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 primary key is null during insertion, it will be populated with the actual
- * value after insertion.
- *
- * For example, to insert a customer record:
- *
- * ```php
- * $customer = new Customer();
- * $customer->name = $name;
- * $customer->email = $email;
- * $customer->insert();
- * ```
- *
- * @param bool $runValidation whether to perform validation before saving the record.
- * If the validation fails, the record will not be inserted into the collection.
- * @param array $attributes list of attributes that need to be saved. Defaults to null,
- * meaning all attributes that are loaded will be saved.
- * @return bool 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)) {
- return false;
- }
- $result = $this->insertInternal($attributes);
- return $result;
- }
- /**
- * @see ActiveRecord::insert()
- */
- protected function insertInternal($attributes = null)
- {
- if (!$this->beforeSave(true)) {
- return false;
- }
- $values = $this->getDirtyAttributes($attributes);
- if (empty($values)) {
- $currentAttributes = $this->getAttributes();
- foreach ($this->primaryKey() as $key) {
- if (isset($currentAttributes[$key])) {
- $values[$key] = $currentAttributes[$key];
- }
- }
- }
- $newId = static::getCollection()->insert($values);
- if ($newId !== null) {
- $this->setAttribute('_id', $newId);
- $values['_id'] = $newId;
- }
- $changedAttributes = array_fill_keys(array_keys($values), null);
- $this->setOldAttributes($values);
- $this->afterSave(true, $changedAttributes);
- return true;
- }
- /**
- * @see ActiveRecord::update()
- * @throws StaleObjectException
- */
- protected function updateInternal($attributes = null)
- {
- if (!$this->beforeSave(false)) {
- return false;
- }
- $values = $this->getDirtyAttributes($attributes);
- if (empty($values)) {
- $this->afterSave(false, $values);
- return 0;
- }
- $condition = $this->getOldPrimaryKey(true);
- $lock = $this->optimisticLock();
- if ($lock !== null) {
- if (!isset($values[$lock])) {
- $values[$lock] = $this->$lock + 1;
- }
- $condition[$lock] = $this->$lock;
- }
- // We do not check the return value of update() because it's possible
- // that it doesn't change anything and thus returns 0.
- $rows = static::getCollection()->update($condition, $values);
- if ($lock !== null && !$rows) {
- throw new StaleObjectException('The object being updated is outdated.');
- }
- if (isset($values[$lock])) {
- $this->$lock = $values[$lock];
- }
- $changedAttributes = [];
- foreach ($values as $name => $value) {
- $changedAttributes[$name] = $this->getOldAttribute($name);
- $this->setOldAttribute($name, $value);
- }
- $this->afterSave(false, $changedAttributes);
- return $rows;
- }
- /**
- * Deletes the document corresponding to this active record from the collection.
- *
- * 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 document from the collection;
- * 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 int|bool the number of documents deleted, or false if the deletion is unsuccessful for some reason.
- * Note that it is possible the number of documents 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()
- {
- $result = false;
- if ($this->beforeDelete()) {
- $result = $this->deleteInternal();
- $this->afterDelete();
- }
- return $result;
- }
- /**
- * @see ActiveRecord::delete()
- * @throws StaleObjectException
- */
- protected function deleteInternal()
- {
- // 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 = static::getCollection()->remove($condition);
- if ($lock !== null && !$result) {
- throw new StaleObjectException('The object being deleted is outdated.');
- }
- $this->setOldAttributes(null);
- 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 collection 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 bool whether the two active records refer to the same row in the same Mongo collection.
- */
- public function equals($record)
- {
- if ($this->isNewRecord || $record->isNewRecord) {
- return false;
- }
- return $this->collectionName() === $record->collectionName() && (string) $this->getPrimaryKey() === (string) $record->getPrimaryKey();
- }
- /**
- * {@inheritdoc}
- */
- public function toArray(array $fields = [], array $expand = [], $recursive = true)
- {
- $data = parent::toArray($fields, $expand, false);
- if (!$recursive) {
- return $data;
- }
- return $this->toArrayInternal($data);
- }
- /**
- * Converts data to array recursively, converting MongoDB BSON objects to readable values.
- * @param mixed $data the data to be converted into an array.
- * @return array the array representation of the data.
- * @since 2.1
- */
- private function toArrayInternal($data)
- {
- if (is_array($data)) {
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $data[$key] = $this->toArrayInternal($value);
- }
- if (is_object($value)) {
- if ($value instanceof Type) {
- $data[$key] = $this->dumpBsonObject($value);
- } else {
- $data[$key] = ArrayHelper::toArray($value);
- }
- }
- }
- return $data;
- } elseif (is_object($data)) {
- return ArrayHelper::toArray($data);
- }
- return [$data];
- }
- /**
- * Converts MongoDB BSON object to readable value.
- * @param Type $object MongoDB BSON object.
- * @return array|string object dump value.
- * @since 2.1
- */
- private function dumpBsonObject(Type $object)
- {
- if ($object instanceof Binary) {
- return $object->getData();
- }
- if (method_exists($object, '__toString')) {
- return $object->__toString();
- }
- return ArrayHelper::toArray($object);
- }
- }
|