/** * 同步指定项目的指定插件 * * @param string $project_id * 项目编号 * @param string $plugin_id * 插件编号 * @param string $collectionName * 集合名称 * @return true false */ public function syncPluginCollection($project_id, $plugin_id, $collectionName) { $pluginCollectionInfo = $this->findOne(array('plugin_id' => $plugin_id, 'alias' => $collectionName)); if ($pluginCollectionInfo == null) { fb('$pluginCollectionInfo is null', 'LOG'); return false; } // 同步数据结构 $syncPluginStructure = function ($plugin_id, $collection_id) use($pluginCollectionInfo) { if ($collection_id instanceof \MongoId) { $collection_id = $collection_id->__toString(); } $this->_structure->physicalRemove(array('collection_id' => $collection_id)); // 插入新的数据结构 $cursor = $this->_plugin_structure->find(array('plugin_id' => $plugin_id, 'plugin_collection_id' => $pluginCollectionInfo['_id']->__toString())); while ($cursor->hasNext()) { $row = $cursor->getNext(); array_unset_recursive($row, array('_id', 'collection_id', '__CREATE_TIME__', '__MODIFY_TIME__', '__REMOVED__')); $row['collection_id'] = $collection_id; $this->_structure->update(array('collection_id' => $collection_id, 'field' => $row['field']), array('$set' => $row), array('upsert' => true)); } return true; }; // 添加映射关系 $createMapping = function ($collection_id, $collectionName) use($project_id, $plugin_id) { if ($collection_id instanceof \MongoId) { $collection_id = $collection_id->__toString(); } $projectPluginInfo = $this->_project_plugin->findOne(array('project_id' => $project_id, 'plugin_id' => $plugin_id)); if ($projectPluginInfo !== null) { $source_project_id = $projectPluginInfo['source_project_id']; if (!empty($source_project_id)) { $collectionInfo = $this->_collection->findOne(array('project_id' => $source_project_id, 'plugin_id' => $plugin_id, 'alias' => $collectionName)); $this->_mapping->update(array('project_id' => $project_id, 'collection_id' => $collection_id), array('$set' => array('collection' => 'idatabase_collection_' . myMongoId($collectionInfo['_id']), 'database' => DEFAULT_DATABASE, 'cluster' => DEFAULT_CLUSTER, 'active' => true)), array('upsert' => true)); return true; } } return false; }; if ($pluginCollectionInfo != null) { unset($pluginCollectionInfo['_id']); $collectionInfo = $pluginCollectionInfo; $collectionInfo['project_id'] = array($project_id); $check = $this->_collection->findOne(array('project_id' => $project_id, 'alias' => $collectionName)); if ($check == null) { $this->_collection->insertRef($collectionInfo); $syncPluginStructure($plugin_id, $collectionInfo['_id']); $createMapping($collectionInfo['_id'], $collectionName); return $collectionInfo; } else { $this->_collection->update(array('_id' => $check['_id']), array('$set' => $collectionInfo)); $syncPluginStructure($plugin_id, $check['_id']); $createMapping($check['_id'], $collectionName); } return $check; } return false; }
/** * 复制插件集合默认数据 * * @param string $plugin_collection_id * @param string $target_collection_id * @return boolean */ public function copy($plugin_collection_id, $target_collection_id) { $source = $this->findOne(array('plugin_collection_id' => $plugin_collection_id)); if ($source == null) { return false; } if (!empty($source['data_collection_id'])) { $data_collection_id = $source['data_collection_id']; $this->_sourceData->setCollection(iCollectionName($data_collection_id)); $this->_sourceData->setReadPreference(\MongoClient::RP_SECONDARY); $this->_targetData->setCollection(iCollectionName($target_collection_id)); $cursor = $this->_sourceData->find(array()); while ($cursor->hasNext()) { $row = $cursor->getNext(); array_unset_recursive($row, array('_id', '__CREATE_TIME__', '__MODIFY_TIME__')); $this->_targetData->update($row, array('$set' => $row), array('upsert' => true)); } return true; } }
/** * 批量修改密钥 * * @author young * @name 批量修改密钥 * @version 2013.11.14 young * @return JsonModel */ public function saveAction() { $updateInfos = $this->params()->fromPost('updateInfos', null); try { $updateInfos = Json::decode($updateInfos, Json::TYPE_ARRAY); } catch (\Exception $e) { return $this->msg(false, '无效的json字符串'); } if (!is_array($updateInfos)) { return $this->msg(false, '更新数据无效'); } foreach ($updateInfos as $row) { $_id = $row['_id']; unset($row['_id']); if ($row['name'] == null) { return $this->msg(false, '请填写密钥名称'); } if ($row['key'] == null) { return $this->msg(false, '请填写密钥'); } if (strlen($row['key']) < 8) { return $this->msg(false, '密钥长度不少于8位'); } if ($row['desc'] == null) { return $this->msg(false, '请填写密钥描述'); } if ($row['expire'] == null) { return $this->msg(false, '请设定密钥过期时间'); } $row['expire'] = intval(strtotime($row['expire'])); if ($row['expire'] === 0) { return $this->msg(false, '无效的日期格式'); } $row['expire'] = new \MongoDate($row['expire']); array_unset_recursive($row, array('_id', 'project_id')); $this->_keys->update(array('_id' => myMongoId($_id), 'project_id' => $this->_project_id), array('$set' => $row)); } return $this->msg(true, '更新密钥成功'); }
/** * 递归方法unset数组里面的元素 * * @param array $array * @param array|string $fields * @param boolean $remove * true表示删除数组$array中的$fields属性 false表示保留数组$array中的$fields属性 */ function array_unset_recursive(&$array, $fields, $remove = true) { if (!is_array($fields)) { $fields = array($fields); } foreach ($array as $key => &$value) { if ($remove) { if (in_array($key, $fields, true)) { unset($array[$key]); } else { if (is_array($value)) { array_unset_recursive($value, $fields, $remove); } } } else { if (!in_array($key, $fields, true)) { unset($array[$key]); } else { if (is_array($value)) { array_unset_recursive($value, $fields, $remove); } } } } }
/** * 同步当前数据结构,作为插件的数据结构。 */ public function syncToPluginAction() { $plugin_collection_id = $this->params()->fromPost('__PLUGIN_COLLECTION_ID__', null); if ($plugin_collection_id == null) { throw new \Exception('插件集合id不存在'); } $cursorStructure = $this->_structure->find(array('collection_id' => $this->_collection_id)); while ($cursorStructure->hasNext()) { $row = $cursorStructure->getNext(); array_unset_recursive($row, array('_id', 'plugin_collection_id')); $row['plugin_collection_id'] = $plugin_collection_id; $rst = $this->_plugin_structure->update(array('plugin_collection_id' => $plugin_collection_id, 'field' => $row['field']), array('$set' => $row), array('upsert' => true)); } return $this->msg(true, '同步当前集合数据结构->插件数据结构操作已经成功'); }
/** * 更新指定范围的数据 * * @param array $criteria * @param array $object * @param array $options */ public function update($criteria, $object, array $options = NULL) { if (!is_array($criteria)) { throw new \Exception('$criteria is array'); } if (empty($object)) { throw new \Exception('$object is empty'); } $keys = array_keys($object); foreach ($keys as $key) { // $key = strtolower($key); if (!in_array($key, $this->_updateHaystack, true)) { throw new \Exception('$key must contain ' . join(',', $this->_updateHaystack)); } } $default = array('upsert' => self::upsert, 'multiple' => self::multiple, 'fsync' => self::fsync); $options = $options === NULL ? $default : array_merge($default, $options); $criteria = $this->appendQuery($criteria); array_unset_recursive($object, array('_id', '__CREATE_TIME__', '__MODIFY_TIME__', '__REMOVED__')); if (parent::count($criteria) == 0) { if (isset($options['upsert']) && $options['upsert']) { $criteria = $this->addSharedKeyToQuery($criteria); parent::update($criteria, array('$set' => array('__CREATE_TIME__' => new \MongoDate(), '__MODIFY_TIME__' => new \MongoDate(), '__REMOVED__' => false)), $options); } } else { unset($options['upsert']); parent::update($criteria, array('$set' => array('__MODIFY_TIME__' => new \MongoDate())), $options); } return parent::update($criteria, $object, $options); }