Skip to content

Commit a0aa925

Browse files
author
Steeven Andrian
committed
update sql polymorphic
1 parent 7ad9d79 commit a0aa925

31 files changed

+1382
-459
lines changed

src/Framework.php

+3
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@
158158
* FRAMEWORK HELPERS
159159
*---------------------------------------------------------------
160160
*/
161+
require __DIR__ . '/Helpers/Array.php';
162+
require __DIR__ . '/Helpers/Object.php';
161163
require __DIR__ . '/Helpers/Framework.php';
164+
require __DIR__ . '/Helpers/Datetime.php';
162165

163166
/**
164167
* Class Framework

src/Http/Controllers/Restful.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,9 @@ public function index()
406406
$conditions = [];
407407

408408
foreach ($this->getValidationRules as $field => $rule) {
409-
$conditions[ $field ] = $get->offsetGet($field);
409+
if($get->offsetExists($field)) {
410+
$conditions[ $field ] = $get->offsetGet($field);
411+
}
410412
}
411413

412414
if (false !== ($result = $this->model->findWhere($conditions, $limit))) {
@@ -1012,7 +1014,7 @@ public function publish($id = null)
10121014
$this->updateRecordStatus(func_get_args(), 'publish');
10131015
}
10141016

1015-
// ------------------------------------------------------------------------
1017+
// ------------------------------------------------------------------------
10161018

10171019
/**
10181020
* Restful::unpublish
@@ -1026,7 +1028,7 @@ public function unpublish($id = null)
10261028
$this->updateRecordStatus(func_get_args(), 'unpublish');
10271029
}
10281030

1029-
// ------------------------------------------------------------------------
1031+
// ------------------------------------------------------------------------
10301032

10311033
/**
10321034
* Restful::archive
@@ -1035,10 +1037,8 @@ public function unpublish($id = null)
10351037
*
10361038
* @throws OutOfRangeException
10371039
*/
1038-
public
1039-
function archive(
1040-
$id = null
1041-
) {
1040+
public function archive($id = null)
1041+
{
10421042
$this->updateRecordStatus(func_get_args(), 'archive');
10431043
}
10441044

@@ -1092,7 +1092,7 @@ public function draft($id = null)
10921092
* @param int $code
10931093
* @param string|null $message
10941094
*/
1095-
public function sendError($code, $message = null)
1095+
protected function sendError($code, $message = null)
10961096
{
10971097
if ($this->ajaxOnly === false) {
10981098
output()->setContentType('application/json');
@@ -1121,7 +1121,7 @@ public function sendError($code, $message = null)
11211121
*
11221122
* @throws \Exception
11231123
*/
1124-
public function sendPayload($data, $longPooling = false)
1124+
protected function sendPayload($data, $longPooling = false)
11251125
{
11261126
if ($longPooling === false) {
11271127
if ($this->ajaxOnly) {

src/Models/Sql/DataObjects/Result.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(\O2System\Database\DataObjects\Result $result, Model
5656
if ( ! models()->has($this->model->getClass())) {
5757
models()->add($model, $this->model->getClass());
5858
}
59-
59+
6060
parent::__construct($result->toArray());
6161

6262
$this->info = $result->getInfo();

src/Models/Sql/DataObjects/Result/Row.php

+14
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,20 @@ private function updateRecordStatus($method)
260260

261261
// ------------------------------------------------------------------------
262262

263+
/**
264+
* Row::restore
265+
*
266+
* @return bool
267+
* @throws \O2System\Spl\Exceptions\RuntimeException
268+
* @throws \Psr\Cache\InvalidArgumentException
269+
*/
270+
public function restore()
271+
{
272+
return $this->updateRecordStatus('restore');
273+
}
274+
275+
// ------------------------------------------------------------------------
276+
263277
/**
264278
* Row::publish
265279
*

src/Models/Sql/Model.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use O2System\Framework\Models\Sql\Traits\ModifierTrait;
2222
use O2System\Framework\Models\Sql\Traits\RecordTrait;
2323
use O2System\Framework\Models\Sql\Traits\RelationTrait;
24+
use O2System\Spl\Exceptions\RuntimeException;
2425

2526
/**
2627
* Class Model
@@ -106,6 +107,19 @@ class Model
106107
*/
107108
public $visibleColumns = [];
108109

110+
/**
111+
* Model::$visibleRecordStatus
112+
*
113+
* @var array
114+
*/
115+
public $visibleRecordStatus = [
116+
'PUBLISH',
117+
'UNPUBLISH',
118+
'DRAFT',
119+
'ARCHIVED',
120+
'LOCKED'
121+
];
122+
109123
/**
110124
* Model::$appendColumns
111125
*
@@ -133,6 +147,13 @@ class Model
133147
*/
134148
public $primaryKeys = [];
135149

150+
/**
151+
* Model::$metadataForeignKey
152+
*
153+
* @var string
154+
*/
155+
public $metadataForeignKey;
156+
136157
/**
137158
* Model::$uploadedImageFilePath
138159
*
@@ -233,6 +254,10 @@ public function __construct()
233254
}
234255
}
235256

257+
if(empty($this->qb) and empty($this->db)) {
258+
throw new RuntimeException('E_DATABASE_CONNECTION_FAILED');
259+
}
260+
236261
// Set database table
237262
if (empty($this->table)) {
238263
$modelClassName = get_called_class();
@@ -271,7 +296,7 @@ final protected function fetchSubModels()
271296
$dirName = dirname($filePath) . DIRECTORY_SEPARATOR;
272297

273298
// Get sub models or siblings models
274-
if ($filename === 'Model' || $filename === modules()->top()->getDirName()) {
299+
if ($filename === 'Model') {
275300
$subModelsDirName = dirname($dirName) . DIRECTORY_SEPARATOR . 'Models' . DIRECTORY_SEPARATOR;
276301

277302
if (is_dir($subModelsDirName)) {

src/Models/Sql/Relations/Abstracts/AbstractRelation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ abstract class AbstractRelation
2828
/**
2929
* Relations Map
3030
*
31-
* @var Relations\Maps\Reference|Relations\Maps\Inverse|Relations\Maps\Intermediary|\O2System\Framework\Models\Sql\Relations\Maps\Through
31+
* @var Relations\Maps\Reference|Relations\Maps\Associate|Relations\Maps\Inverse|\O2System\Framework\Models\Sql\Relations\Maps\Polymorphic
3232
*/
3333
protected $map;
3434

3535
/**
3636
* Relations::__construct
3737
*
38-
* @param Relations\Maps\Reference|Relations\Maps\Inverse|Relations\Maps\Intermediary|\O2System\Framework\Models\Sql\Relations\Maps\Through $map
38+
* @param Relations\Maps\Reference|Relations\Maps\Associate|Relations\Maps\Inverse|\O2System\Framework\Models\Sql\Relations\Maps\Polymorphic $map
3939
*/
4040
public function __construct($map)
4141
{

src/Models/Sql/Relations/BelongsTo.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class BelongsTo extends Abstracts\AbstractRelation
3131
*/
3232
public function getResult()
3333
{
34-
if ($this->map->currentModel->row instanceof Sql\DataObjects\Result\Row) {
35-
$criteria = $this->map->currentModel->row->offsetGet($this->map->currentForeignKey);
36-
$field = $this->map->referenceTable . '.' . $this->map->referencePrimaryKey;
34+
if ($this->map->objectModel->row instanceof Sql\DataObjects\Result\Row) {
35+
$criteria = $this->map->objectModel->row->offsetGet($this->map->objectForeignKey);
36+
$field = $this->map->associateModel->table . '.' . $this->map->associateModel->primaryKey;
3737

38-
$this->map->referenceModel->result = null;
39-
$this->map->referenceModel->row = null;
38+
$this->map->associateModel->result = null;
39+
$this->map->associateModel->row = null;
4040

41-
if ($result = $this->map->referenceModel->find($criteria, $field, 1)) {
41+
if ($result = $this->map->associateModel->find($criteria, $field, 1)) {
4242
if($result instanceof Sql\DataObjects\Result\Row) {
4343
return $result;
4444
} elseif($result instanceof Sql\DataObjects\Result) {

src/Models/Sql/Relations/BelongsToMany.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ class BelongsToMany extends Sql\Relations\Abstracts\AbstractRelation
3232
*/
3333
public function getResult()
3434
{
35-
if ($this->map->currentModel->row instanceof Sql\DataObjects\Result\Row) {
36-
$criteria = $this->map->currentModel->row->offsetGet($this->map->currentForeignKey);
35+
if ($this->map->objectModel->row instanceof Sql\DataObjects\Result\Row) {
36+
$criteria = $this->map->objectModel->row->offsetGet($this->map->objectForeignKey);
3737
$condition = [
38-
$this->map->referenceTable . '.' . $this->map->currentForeignKey => $criteria,
38+
$this->map->associateTable . '.' . $this->map->objectForeignKey => $criteria,
3939
];
4040

41-
$this->map->referenceModel->result = null;
42-
$this->map->referenceModel->row = null;
41+
$this->map->associateModel->result = null;
42+
$this->map->associateModel->row = null;
4343

44-
if ($result = $this->map->referenceModel->findWhere($condition)) {
44+
if ($result = $this->map->associateModel->findWhere([$condition])) {
4545
return $result;
4646
}
4747
}

src/Models/Sql/Relations/BelongsToManyThrough.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,29 @@ class BelongsToManyThrough extends Sql\Relations\Abstracts\AbstractRelation
3232
*/
3333
public function getResult()
3434
{
35-
if ($this->map->currentModel->row instanceof Sql\DataObjects\Result\Row) {
36-
$criteria = $this->map->currentModel->row->offsetGet($this->map->currentPrimaryKey);
37-
$field = $this->map->currentTable . '.' . $this->map->currentPrimaryKey;
35+
if ($this->map->objectModel->row instanceof Sql\DataObjects\Result\Row) {
36+
$criteria = $this->map->objectModel->row->offsetGet($this->map->objectPrimaryKey);
37+
$conditions = [
38+
$this->map->objectTable . '.' . $this->map->objectPrimaryKey => $criteria,
39+
];
3840

39-
$this->map->referenceModel->qb
41+
$this->map->associateModel->qb
4042
->select([
41-
$this->map->referenceTable . '.*',
43+
$this->map->associateTable . '.*',
4244
])
43-
->join($this->map->currentTable, implode(' = ', [
44-
$this->map->currentTable . '.' . $this->map->currentPrimaryKey,
45-
$this->map->intermediaryTable . '.' . $this->map->intermediaryCurrentForeignKey,
45+
->join($this->map->objectTable, implode(' = ', [
46+
$this->map->objectTable . '.' . $this->map->objectPrimaryKey,
47+
$this->map->intermediaryTable . '.' . $this->map->intermediaryForeignKey,
4648
]))
47-
->join($this->map->referenceTable, implode(' = ', [
48-
$this->map->referenceTable . '.' . $this->map->referencePrimaryKey,
49-
$this->map->intermediaryTable . '.' . $this->map->intermediaryReferenceForeignKey,
49+
->join($this->map->associateTable, implode(' = ', [
50+
$this->map->associateTable . '.' . $this->map->associatePrimaryKey,
51+
$this->map->intermediaryTable . '.' . $this->map->intermediaryAssociateForeignKey,
5052
]));
5153

5254
$this->map->intermediaryModel->result = null;
5355
$this->map->intermediaryModel->row = null;
5456

55-
if ($result = $this->map->intermediaryModel->find($criteria, $field)) {
57+
if ($result = $this->map->intermediaryModel->findWhere($conditions)) {
5658
return $result;
5759
}
5860
}

src/Models/Sql/Relations/BelongsToThrough.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ class BelongsToThrough extends Sql\Relations\Abstracts\AbstractRelation
3131
*/
3232
public function getResult()
3333
{
34-
if ($this->map->currentModel->row instanceof Sql\DataObjects\Result\Row) {
35-
$criteria = $this->map->currentModel->row->offsetGet($this->map->currentPrimaryKey);
36-
$field = $this->map->currentTable . '.' . $this->map->currentPrimaryKey;
34+
if ($this->map->objectModel->row instanceof Sql\DataObjects\Result\Row) {
35+
$criteria = $this->map->objectModel->row->offsetGet($this->map->objectPrimaryKey);
36+
$field = $this->map->objectTable . '.' . $this->map->objectPrimaryKey;
3737

38-
$this->map->referenceModel->qb
38+
$this->map->associateModel->qb
3939
->select([
40-
$this->map->referenceTable . '.*',
40+
$this->map->associateTable . '.*',
4141
])
42-
->join($this->map->currentTable, implode(' = ', [
43-
$this->map->currentTable . '.' . $this->map->currentPrimaryKey,
44-
$this->map->intermediaryTable . '.' . $this->map->intermediaryCurrentForeignKey,
42+
->join($this->map->objectTable, implode(' = ', [
43+
$this->map->objectTable . '.' . $this->map->objectPrimaryKey,
44+
$this->map->intermediaryTable . '.' . $this->map->intermediaryPrimaryKey,
4545
]))
46-
->join($this->map->referenceTable, implode(' = ', [
47-
$this->map->referenceTable . '.' . $this->map->referencePrimaryKey,
48-
$this->map->intermediaryTable . '.' . $this->map->intermediaryReferenceForeignKey,
46+
->join($this->map->associateTable, implode(' = ', [
47+
$this->map->associateTable . '.' . $this->map->associatePrimaryKey,
48+
$this->map->intermediaryTable . '.' . $this->map->intermediaryAssociateForeignKey,
4949
]));
5050

5151
$this->map->intermediaryModel->result = null;

src/Models/Sql/Relations/HasMany.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ class HasMany extends Sql\Relations\Abstracts\AbstractRelation
3232
*/
3333
public function getResult()
3434
{
35-
if ($this->map->currentModel->row instanceof Sql\DataObjects\Result\Row) {
36-
$criteria = $this->map->currentModel->row->offsetGet($this->map->currentPrimaryKey);
35+
if ($this->map->objectModel->row instanceof Sql\DataObjects\Result\Row) {
36+
$criteria = $this->map->objectModel->row->offsetGet($this->map->objectPrimaryKey);
3737
$condition = [
38-
$this->map->referenceTable . '.' . $this->map->referenceForeignKey => $criteria,
38+
$this->map->associateTable . '.' . $this->map->associateForeignKey => $criteria,
3939
];
4040

41-
$this->map->referenceModel->result = null;
42-
$this->map->referenceModel->row = null;
41+
$this->map->associateModel->result = null;
42+
$this->map->associateModel->row = null;
4343

44-
if ($result = $this->map->referenceModel->findWhere($condition)) {
44+
if ($result = $this->map->associateModel->findWhere($condition)) {
4545
return $result;
4646
}
4747
}

src/Models/Sql/Relations/HasManyThrough.php

+19-10
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,36 @@ class HasManyThrough extends Sql\Relations\Abstracts\AbstractRelation
3232
*/
3333
public function getResult()
3434
{
35-
if ($this->map->currentModel->row instanceof Sql\DataObjects\Result\Row) {
36-
$criteria = $this->map->currentModel->row->offsetGet($this->map->currentPrimaryKey);
37-
$condition = [
38-
$this->map->intermediaryTable . '.' . $this->map->intermediaryCurrentForeignKey => $criteria,
35+
if ($this->map->objectModel->row instanceof Sql\DataObjects\Result\Row) {
36+
$criteria = $this->map->objectModel->row->offsetGet($this->map->objectPrimaryKey);
37+
$conditions = [
38+
$this->map->intermediaryTable . '.' . $this->map->intermediaryForeignKey => $criteria,
3939
];
4040

4141
$this->map->intermediaryModel->qb
4242
->select([
43-
$this->map->referenceTable . '.*',
43+
$this->map->associateTable . '.*',
4444
])
4545
->from($this->map->intermediaryTable)
46-
->join($this->map->referenceTable, implode(' = ', [
47-
$this->map->referenceTable . '.' . $this->map->referencePrimaryKey,
48-
$this->map->intermediaryTable . '.' . $this->map->intermediaryReferenceForeignKey,
46+
->join($this->map->associateTable, implode(' = ', [
47+
$this->map->associateTable . '.' . $this->map->associatePrimaryKey,
48+
$this->map->intermediaryTable . '.' . $this->map->intermediaryAssociateForeignKey,
4949
]));
5050

5151
$this->map->intermediaryModel->result = null;
5252
$this->map->intermediaryModel->row = null;
5353

54-
if ($result = $this->map->intermediaryModel->findWhere($condition)) {
55-
return $result;
54+
if ($result = $this->map->intermediaryModel->findWhere($conditions)) {
55+
if($result->count()) {
56+
$ids = [];
57+
foreach($result as $row) {
58+
$ids[$row->id] = $row->id;
59+
}
60+
61+
if($result = $this->map->associateModel->findIn($ids)) {
62+
return $result;
63+
}
64+
}
5665
}
5766
}
5867

src/Models/Sql/Relations/HasOne.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class HasOne extends Sql\Relations\Abstracts\AbstractRelation
3131
*/
3232
public function getResult()
3333
{
34-
if ($this->map->currentModel->row instanceof Sql\DataObjects\Result\Row) {
35-
$criteria = $this->map->currentModel->row->offsetGet($this->map->currentPrimaryKey);
36-
$field = $this->map->referenceTable . '.' . $this->map->referenceForeignKey;
34+
if ($this->map->objectModel->row instanceof Sql\DataObjects\Result\Row) {
35+
$criteria = $this->map->objectModel->row->offsetGet($this->map->objectPrimaryKey);
36+
$field = $this->map->associateTable . '.' . $this->map->associateForeignKey;
3737

38-
$this->map->referenceModel->result = null;
39-
$this->map->referenceModel->row = null;
38+
$this->map->associateModel->result = null;
39+
$this->map->associateModel->row = null;
4040

41-
if ($result = $this->map->referenceModel->find($criteria, $field, 1)) {
41+
if ($result = $this->map->associateModel->find($criteria, $field, 1)) {
4242
if($result instanceof Sql\DataObjects\Result\Row) {
4343
return $result;
4444
} elseif($result instanceof Sql\DataObjects\Result) {

0 commit comments

Comments
 (0)