| 1 |
<?php |
| 2 |
namespace Averta\WordPress\Database\Entity; |
| 3 |
|
| 4 |
use Averta\WordPress\Database\ORM\Connection; |
| 5 |
use Averta\WordPress\Database\ORM\Query; |
| 6 |
use TypeRocket\Models\Model as BaseModel; |
| 7 |
|
| 8 |
class Model extends BaseModel |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Determines what fields should be updated automatically. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $autoFill = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* Determines add table fields |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
protected $columnNames = []; |
| 23 |
|
| 24 |
/** |
| 25 |
* Update resource fields |
| 26 |
* |
| 27 |
* @param array $fields |
| 28 |
* |
| 29 |
* @return mixed |
| 30 |
*/ |
| 31 |
public function update( $fields = [] ) |
| 32 |
{ |
| 33 |
$fields = $this->formatProperties( $fields ); |
| 34 |
return parent::update( $fields ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Create resource by fields |
| 39 |
* |
| 40 |
* When a resource is created the Model ID should be set to the |
| 41 |
* resource's ID. |
| 42 |
* |
| 43 |
* @param array $fields |
| 44 |
* |
| 45 |
* @return mixed |
| 46 |
*/ |
| 47 |
public function create( $fields = [] ) |
| 48 |
{ |
| 49 |
$fields = $this->formatProperties( $fields ); |
| 50 |
return parent::create( $fields ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Format properties |
| 55 |
* |
| 56 |
* @param array $fields |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function formatProperties( $fields ) |
| 61 |
{ |
| 62 |
$fields = array_merge( $this->autoFill, $fields ); |
| 63 |
|
| 64 |
foreach( $fields as $name => $value ) { |
| 65 |
if( ! empty( $this->format[ $name ] ) ){ |
| 66 |
if( method_exists( $this, $this->format[ $name ] ) ){ |
| 67 |
$fields[ $name ] = call_user_func( [ $this, $this->format[ $name ] ], $value ); |
| 68 |
} elseif( is_callable( $this->format[ $name ] ) ) { |
| 69 |
$fields[ $name ] = call_user_func( $this->format[ $name ], $value ); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $fields; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Get Date Time |
| 80 |
* |
| 81 |
* @return bool|string |
| 82 |
*/ |
| 83 |
public function getDateTime() |
| 84 |
{ |
| 85 |
return gmdate('Y-m-d H:i:s', time()); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Establish WPDB connection |
| 90 |
* |
| 91 |
* @return \wpdb |
| 92 |
*/ |
| 93 |
protected function establishConnection() |
| 94 |
{ |
| 95 |
$connection = Connection::getFromContainer(); |
| 96 |
$name = $this->connection; |
| 97 |
|
| 98 |
if(!$name) { |
| 99 |
return $connection->default(); |
| 100 |
} |
| 101 |
|
| 102 |
return $connection->get( $name ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Setup WP query connection |
| 108 |
* |
| 109 |
* @param \wpdb $wpdb |
| 110 |
* |
| 111 |
* @return Query |
| 112 |
*/ |
| 113 |
public function setupQueryConnectionForModel(\wpdb $wpdb) |
| 114 |
{ |
| 115 |
return (new Query)->setWpdb($wpdb); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get all table columns |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function getTableColumns(){ |
| 124 |
if( ! $this->columnNames ){ |
| 125 |
$this->columnNames = array_unique( array_merge( $this->getGuardFields(), $this->getBuiltinFields() ) ); |
| 126 |
} |
| 127 |
return $this->columnNames; |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|