Skip to content

Commit

Permalink
Added ErrorResponseException, HiResException~>HiArtException, added g…
Browse files Browse the repository at this point in the history
…lobal checking of error responses, other minor
  • Loading branch information
SilverFire committed Aug 25, 2015
1 parent 3d87c1a commit 93c054e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 62 deletions.
1 change: 1 addition & 0 deletions ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace hiqdev\hiart;

use common\components\Err;
use hipanel\base\Re;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
Expand Down
30 changes: 5 additions & 25 deletions ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public function isScenarioDefault()
* Please refer to the [elasticsearch documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html)
* for more details on these options.
* @return null|static The record instance or null if it was not found.
* @throws HiResException
*/
public static function get($primaryKey, $options = [])
{
Expand All @@ -80,9 +79,7 @@ public static function get($primaryKey, $options = [])
}
$command = static::getDb()->createCommand();
$result = $command->get(static::type(), $primaryKey, $options);
if (Err::isError($result)) {
throw new HiResException('Hiresource method: get', Err::getError($result));
}

if ($result) {
$model = static::instantiate($result);
static::populateRecord($model, $result);
Expand Down Expand Up @@ -245,15 +242,12 @@ public function insert($runValidation = true, $attributes = null, $options = [])
$command = $this->getScenarioCommand('create');
$data = array_merge($values, $options, ['id' => $this->getOldPrimaryKey()]);

$response = static::getDb()->createCommand()->perform($command, $data);
$result = static::getDb()->createCommand()->perform($command, $data);

if (Err::isError($response)) {
throw new HiResException('Hiresource method: Insert -- ' . Json::encode($response), Err::getError($response));
}
$pk = static::primaryKey()[0];
$this->$pk = $response['id'];
$this->$pk = $result['id'];
if ($pk != 'id') {
$values[$pk] = $response['id'];
$values[$pk] = $result['id'];
}
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
Expand All @@ -273,10 +267,6 @@ public function delete($options = [])

$result = static::getDb()->createCommand()->perform($command, $data);

if (Err::isError($result)) {
throw new HiResException('Hiresource method: Delete -- ' . Json::encode($result), Err::getError($result));
}

$this->setOldAttributes(null);
$this->afterDelete();

Expand Down Expand Up @@ -315,7 +305,6 @@ protected function updateInternal($attributes = null, $options = [])
$data = array_merge($values, $options, ['id' => $this->getOldPrimaryKey()]);

$result = static::getDb()->createCommand()->perform($command, $data);

$changedAttributes = [];
foreach ($values as $name => $value) {
$changedAttributes[$name] = $this->getOldAttribute($name);
Expand All @@ -324,11 +313,7 @@ protected function updateInternal($attributes = null, $options = [])

$this->afterSave(false, $changedAttributes);

if ($result === false || Err::isError($result)) {
throw new HiResException('Hiresource method: update', Err::getError($result));
} else {
return $result;
}
return $result;
}

/**
Expand All @@ -338,16 +323,11 @@ protected function updateInternal($attributes = null, $options = [])
* @param array $options
* @param bool $bulk
* @return array
* @throws HiResException
*/
public static function perform($action, $options = [], $bulk = false)
{
$action = ($bulk == true) ? static::index() . $action : static::modelName() . $action;
$result = static::getDb()->createCommand()->perform($action, $options);
if (Err::isError($result)) {
throw new HiResException('Hiresource method: ' . $action, Err::getError($result));
}

return $result;
}

Expand Down
11 changes: 0 additions & 11 deletions Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class Collection extends Component
* @param Model|array $model if the model is an instance of [[Model]] - sets it, otherwise - creates the model
* using given options array
* @return object|Model
* @throws InvalidConfigException
*/
public function setModel ($model) {
if ($model instanceof Model) {
Expand Down Expand Up @@ -282,7 +281,6 @@ public function set ($models) {
* @param array $options the array of options that will be passed to [[insert]] or [[update]] methods to override
* model parameters.
* @return boolean whether the saving succeeds
* @throws HiResException
*/
public function save ($runValidation = true, $attributes = null, $options = []) {
if ($this->isEmpty()) {
Expand Down Expand Up @@ -311,11 +309,6 @@ public function insert ($runValidation = true, $attributes = null, $options = []
$command = $this->first->getScenarioCommand('create', true);

$results = $this->first->getDb()->createCommand()->perform($command, $data);

if (Err::isError($results)) {
throw new HiResException(Err::getError($results), Json::encode($results));
}

$pk = $this->first->primaryKey()[0];
foreach ($this->models as $key => $model) {
/* @var $model ActiveRecord */
Expand Down Expand Up @@ -351,10 +344,6 @@ public function update ($runValidation = true, $attributes = null, $options = []
$command = $this->first->getScenarioCommand('update', true);
$results = $this->first->getDb()->createCommand()->perform($command, $data);

if ($results === false || Err::isError($results)) {
throw new HiResException(Err::getError($results), Json::encode($results));
}

foreach ($this->models as $key => $model) {
$changedAttributes = [];
$values = &$data[$key];
Expand Down
26 changes: 8 additions & 18 deletions Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace hiqdev\hiart;

use common\components\Err;
use yii\base\Component;
use yii\base\InvalidCallException;
use yii\helpers\ArrayHelper;
Expand Down Expand Up @@ -41,36 +42,30 @@ class Command extends Component
* Sends a request to the _search API and returns the result
* @param array $options
* @return mixed
* @throws ErrorResponseException
*/
public function search($options = [])
{
$query = $this->queryParts;
// if (empty($query)) {
// $query = '{}';
// }
// if (is_array($query)) {
// $query = Json::encode($query);
// }

$options = array_merge($query, $options);
$url = $this->index . ArrayHelper::remove($options, 'scenario', 'Search');

return $this->db->get($url, $options);
$result = $this->db->get($url, $options);
return $result;
}

public function getList($options = [])
{
$options = array_merge($this->queryParts, $options);
return $this->db->get($this->index . 'GetList', $options);
$command = $this->index . 'GetList';
$result = $this->db->get($command, $options);
return $result;
}


public function insert($action, $data, $id = null, $options = [])
{
// if (empty($data)) {
// $body = '{}';
// } else {
// $body = is_array($data) ? Json::encode($data) : $data;
// }
$options = array_merge($data, $options);

if ($id !== null) {
Expand All @@ -80,11 +75,6 @@ public function insert($action, $data, $id = null, $options = [])
}
}

// public function get()
// {
// unset($this->queryParts['limit']);
// return $this->db->get($this->type.'GetInfo', $this->queryParts);
// }
public function get($modelName, $primaryKey, $options)
{
return $this->db->get($modelName . 'GetInfo', ArrayHelper::merge(['id' => $primaryKey], $options));
Expand Down
19 changes: 19 additions & 0 deletions ErrorResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @link http://hiqdev.com/yii2-hiart
* @copyright Copyright (c) 2015 HiQDev
* @license http://hiqdev.com/yii2-hiart/license
*/

namespace hiqdev\hiart;

use common\components\Err;

/**
* Class ErrorResponseException
* Exception
* @package hiqdev\hiart
*/
class ErrorResponseException extends HiArtException
{
}
10 changes: 2 additions & 8 deletions HiResException.php → HiArtException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@

namespace hiqdev\hiart;

/**
* Exception represents an exception that is caused by elasticsearch-related operations.
*
* @author Carsten Brandt <[email protected]>
* @since 2.0
*/
class HiResException extends \yii\db\Exception
class HiArtException extends \yii\db\Exception
{
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return 'HiActiveResource Database Exception';
return 'HiPanel active record exception';
}
}

0 comments on commit 93c054e

Please sign in to comment.