Adds new method to validate model on request
Cleans up code formatting
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace MailPoet\Models;
|
namespace MailPoet\Models;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
@@ -78,14 +79,12 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
|
|
||||||
static function bulkTrash($orm) {
|
static function bulkTrash($orm) {
|
||||||
$model = get_called_class();
|
$model = get_called_class();
|
||||||
$count = self::bulkAction($orm, function($ids) use($model) {
|
$count = self::bulkAction($orm, function($ids) use ($model) {
|
||||||
$model::rawExecute(join(' ', array(
|
$model::rawExecute(join(' ', array(
|
||||||
'UPDATE `'.$model::$_table.'`',
|
'UPDATE `' . $model::$_table . '`',
|
||||||
'SET `deleted_at` = NOW()',
|
'SET `deleted_at` = NOW()',
|
||||||
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
|
'WHERE `id` IN (' . rtrim(str_repeat('?,', count($ids)), ',') . ')'
|
||||||
)),
|
)), $ids);
|
||||||
$ids
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return array('count' => $count);
|
return array('count' => $count);
|
||||||
@@ -93,7 +92,7 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
|
|
||||||
static function bulkDelete($orm) {
|
static function bulkDelete($orm) {
|
||||||
$model = get_called_class();
|
$model = get_called_class();
|
||||||
$count = self::bulkAction($orm, function($ids) use($model) {
|
$count = self::bulkAction($orm, function($ids) use ($model) {
|
||||||
$model::whereIn('id', $ids)->deleteMany();
|
$model::whereIn('id', $ids)->deleteMany();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -106,14 +105,12 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
|
|
||||||
static function bulkRestore($orm) {
|
static function bulkRestore($orm) {
|
||||||
$model = get_called_class();
|
$model = get_called_class();
|
||||||
$count = self::bulkAction($orm, function($ids) use($model) {
|
$count = self::bulkAction($orm, function($ids) use ($model) {
|
||||||
$model::rawExecute(join(' ', array(
|
$model::rawExecute(join(' ', array(
|
||||||
'UPDATE `'.$model::$_table.'`',
|
'UPDATE `' . $model::$_table . '`',
|
||||||
'SET `deleted_at` = NULL',
|
'SET `deleted_at` = NULL',
|
||||||
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
|
'WHERE `id` IN (' . rtrim(str_repeat('?,', count($ids)), ',') . ')'
|
||||||
)),
|
)), $ids);
|
||||||
$ids
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return array('count' => $count);
|
return array('count' => $count);
|
||||||
@@ -124,7 +121,7 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
|
|
||||||
if($total === 0) return false;
|
if($total === 0) return false;
|
||||||
|
|
||||||
$rows = $orm->select(static::$_table.'.id')
|
$rows = $orm->select(static::$_table . '.id')
|
||||||
->offset(null)
|
->offset(null)
|
||||||
->limit(null)
|
->limit(null)
|
||||||
->findArray();
|
->findArray();
|
||||||
@@ -138,7 +135,8 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get number of affected rows
|
// get number of affected rows
|
||||||
return $orm->get_last_statement()->rowCount();
|
return $orm->get_last_statement()
|
||||||
|
->rowCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
function duplicate($data = array()) {
|
function duplicate($data = array()) {
|
||||||
@@ -146,7 +144,7 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
$model_data = array_merge($this->asArray(), $data);
|
$model_data = array_merge($this->asArray(), $data);
|
||||||
unset($model_data['id']);
|
unset($model_data['id']);
|
||||||
|
|
||||||
$duplicate = $model::create();
|
$duplicate = $model::create();
|
||||||
$duplicate->hydrate($model_data);
|
$duplicate->hydrate($model_data);
|
||||||
$duplicate->set_expr('created_at', 'NOW()');
|
$duplicate->set_expr('created_at', 'NOW()');
|
||||||
$duplicate->set_expr('updated_at', 'NOW()');
|
$duplicate->set_expr('updated_at', 'NOW()');
|
||||||
@@ -171,10 +169,10 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHP 5.3 fix for incorrectly returned model results when using asArray() function.
|
* PHP 5.3 fix for incorrectly returned model results when using asArray() function.
|
||||||
* Jira reference: https://goo.gl/UZaMj5
|
* Jira reference: https://goo.gl/UZaMj5
|
||||||
* TODO: remove after phasing out PHP 5.3 support
|
* TODO: remove after phasing out PHP 5.3 support
|
||||||
*/
|
*/
|
||||||
function asArray() {
|
function asArray() {
|
||||||
return call_user_func_array('parent::as_array', func_get_args());
|
return call_user_func_array('parent::as_array', func_get_args());
|
||||||
}
|
}
|
||||||
@@ -185,8 +183,17 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
public static function __callStatic($method, $parameters) {
|
public static function __callStatic($method, $parameters) {
|
||||||
try {
|
try {
|
||||||
return parent::__callStatic($method, $parameters);
|
return parent::__callStatic($method, $parameters);
|
||||||
} catch (\PDOException $e) {
|
} catch(\PDOException $e) {
|
||||||
throw new \Exception($e->getMessage());
|
throw new \Exception($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public function validate() {
|
||||||
|
$success = true;
|
||||||
|
foreach(array_keys($this->_validations) as $field) {
|
||||||
|
$success = $success && $this->validateField($field, $this->$field);
|
||||||
|
}
|
||||||
|
$this->setError($this->getValidationErrors());
|
||||||
|
return $success;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user