-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forked from yii2-base, some cleanup made
- Loading branch information
1 parent
426d8dc
commit e21c839
Showing
13 changed files
with
127 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea/ | ||
.idea/ | ||
/vendor | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# yii2-base | ||
# yii2-tools-yarcode | ||
Some yii2 extensions we use in our work. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
namespace yarcode\base\behaviors; | ||
namespace YarCode\Yii2\Behaviors; | ||
|
||
use yii\db\Expression; | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,22 @@ | |
/** | ||
* @author Alexey Samoylov <[email protected]> | ||
*/ | ||
namespace yarcode\base\traits; | ||
|
||
namespace YarCode\Yii2\Traits; | ||
|
||
use Carbon\Carbon; | ||
use yii\base\Model; | ||
|
||
/** | ||
* Class CarbonModelTrait | ||
* @package common\components\traits | ||
* @package YarCode\Yii2\Traits | ||
* | ||
* @mixin Model | ||
*/ | ||
trait CarbonModelTrait | ||
{ | ||
/** | ||
* Returns attribute value as a Carbon instance | ||
* @param $attribute | ||
* @return Carbon | ||
*/ | ||
|
@@ -28,15 +30,19 @@ public function getCarbonAttribute($attribute) | |
} | ||
if (is_numeric($value)) { | ||
return Carbon::createFromTimestamp($value); | ||
} | ||
elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) { | ||
} elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) { | ||
return Carbon::createFromFormat('Y-m-d', $value)->startOfDay(); | ||
} else { | ||
return Carbon::createFromFormat($this->getCarbonFormat($attribute), $value); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns datetime format for a specified attribute | ||
* @param string $attribute | ||
* @return string | ||
*/ | ||
public function getCarbonFormat($attribute) | ||
{ | ||
return 'Y-m-d H:i:s'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* @author Alexey Samoylov <[email protected]> | ||
*/ | ||
|
||
namespace YarCode\Yii2\Traits; | ||
|
||
use yii\db\ActiveRecord; | ||
use yii\db\Exception; | ||
use yii\helpers\VarDumper; | ||
|
||
/** | ||
* Trait FragileActiveRecordTrait | ||
* @package YarCode\Yii2\Traits | ||
* | ||
* @mixin ActiveRecord | ||
*/ | ||
trait FragileModelTrait | ||
{ | ||
/** | ||
* @param bool $runValidation | ||
* @param array|null $attributeNames | ||
* @return true | ||
* @throws Exception | ||
* @throws \LogicException | ||
*/ | ||
public function trySave($runValidation = true, $attributeNames = null) | ||
{ | ||
if (false === $this->save($runValidation, $attributeNames)) { | ||
if ($this->hasErrors()) { | ||
throw new \LogicException('Model save failed due to validation errors: ' . VarDumper::dumpAsString($this->getErrors())); | ||
} else { | ||
$pdo = static::getDb()->pdo; | ||
throw new Exception('Model save failed', $pdo->errorInfo(), $pdo->errorCode()); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @param bool $runValidation | ||
* @param array|null $attributeNames | ||
* @return true | ||
* @throws Exception | ||
* @throws \LogicException | ||
*/ | ||
public function tryUpdate($runValidation = true, $attributeNames = null) | ||
{ | ||
if (false === $this->update($runValidation, $attributeNames)) { | ||
if ($this->hasErrors()) { | ||
throw new \LogicException('Model update failed due to validation errors: ' . VarDumper::dumpAsString($this->getErrors())); | ||
} else { | ||
$pdo = static::getDb()->pdo; | ||
throw new Exception('Model update failed', $pdo->errorInfo(), $pdo->errorCode()); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @param bool $runValidation | ||
* @param array|null $attributeNames | ||
* @return true | ||
* @throws Exception | ||
* @throws \LogicException | ||
*/ | ||
public function tryInsert($runValidation = true, $attributeNames = null) | ||
{ | ||
if (false === $this->insert($runValidation, $attributeNames)) { | ||
if ($this->hasErrors()) { | ||
throw new \LogicException('Model insert failed due to validation errors: ' . VarDumper::dumpAsString($this->getErrors())); | ||
} else { | ||
$pdo = static::getDb()->pdo; | ||
throw new Exception('Model insert failed', $pdo->errorInfo(), $pdo->errorCode()); | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,14 @@ | |
/** | ||
* @author Alexey Samoylov <[email protected]> | ||
*/ | ||
namespace yarcode\base\traits; | ||
namespace YarCode\Yii2\Traits; | ||
|
||
use yii\db\ActiveRecord; | ||
use yii\helpers\ArrayHelper; | ||
|
||
/** | ||
* Class ListDataTrait | ||
* @package yarcode\base\traits | ||
* @package YarCode\Yii2\Traits | ||
* | ||
* @mixin ActiveRecord | ||
*/ | ||
|
Oops, something went wrong.