/** * case: * * $data = [ * 'grantIdentifier1' => 'scopeIdentifier1', * 'grantIdentifier2' => 'scopeIdentifier2' * ] * * @param array $data * @return boolean */ public function bindGrantScope(array $data) { $grantIdentifiers = array_keys($data); $scopeIdentifiers = array_values($data); $grantsInDb = ArrayHelper::getColumn(GrantsModel::findByGrantId($grantIdentifiers)->all(), 'id'); $scopesInDb = ArrayHelper::getColumn(ScopesModel::findByScopeId($scopeIdentifiers)->all(), 'id'); if (count(array_diff($grantIdentifiers, $grantsInDb)) !== 0 or count(array_diff($scopeIdentifiers, $scopesInDb)) !== 0) { return false; } $db = CommonModel::getDb(); $transaction = $db->beginTransaction(); try { foreach ($data as $grant => $scope) { $clientGrantModel = new GrantScopesModel(); $clientGrantModel->grant_id = $grant; $clientGrantModel->scope_id = $scope; $clientGrantModel->save(); } $transaction->commit(); return true; } catch (\Exception $e) { $transaction->rollBack(); return false; } }
/** * @param string $identifier * @return int */ public function removeScope($identifier) { /** @var \yii\db\ActiveRecord $obj */ $obj = ScopesModel::findOne(['id' => $identifier]); if ($obj) { return $obj->delete(); } return 0; }