Adds new method to validate model on request

Cleans up code formatting
This commit is contained in:
Vlad
2017-07-24 11:20:43 -04:00
parent 4c898b76b2
commit cf00813c7f

View File

@ -1,4 +1,5 @@
<?php
namespace MailPoet\Models;
if(!defined('ABSPATH')) exit;
@ -78,14 +79,12 @@ class Model extends \Sudzy\ValidModel {
static function bulkTrash($orm) {
$model = get_called_class();
$count = self::bulkAction($orm, function($ids) use($model) {
$count = self::bulkAction($orm, function($ids) use ($model) {
$model::rawExecute(join(' ', array(
'UPDATE `'.$model::$_table.'`',
'UPDATE `' . $model::$_table . '`',
'SET `deleted_at` = NOW()',
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
)),
$ids
);
'WHERE `id` IN (' . rtrim(str_repeat('?,', count($ids)), ',') . ')'
)), $ids);
});
return array('count' => $count);
@ -93,7 +92,7 @@ class Model extends \Sudzy\ValidModel {
static function bulkDelete($orm) {
$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();
});
@ -106,14 +105,12 @@ class Model extends \Sudzy\ValidModel {
static function bulkRestore($orm) {
$model = get_called_class();
$count = self::bulkAction($orm, function($ids) use($model) {
$count = self::bulkAction($orm, function($ids) use ($model) {
$model::rawExecute(join(' ', array(
'UPDATE `'.$model::$_table.'`',
'UPDATE `' . $model::$_table . '`',
'SET `deleted_at` = NULL',
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
)),
$ids
);
'WHERE `id` IN (' . rtrim(str_repeat('?,', count($ids)), ',') . ')'
)), $ids);
});
return array('count' => $count);
@ -124,7 +121,7 @@ class Model extends \Sudzy\ValidModel {
if($total === 0) return false;
$rows = $orm->select(static::$_table.'.id')
$rows = $orm->select(static::$_table . '.id')
->offset(null)
->limit(null)
->findArray();
@ -138,7 +135,8 @@ class Model extends \Sudzy\ValidModel {
}
// get number of affected rows
return $orm->get_last_statement()->rowCount();
return $orm->get_last_statement()
->rowCount();
}
function duplicate($data = array()) {
@ -185,8 +183,17 @@ class Model extends \Sudzy\ValidModel {
public static function __callStatic($method, $parameters) {
try {
return parent::__callStatic($method, $parameters);
} catch (\PDOException $e) {
} catch(\PDOException $e) {
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;
}
}