Skip to content

Commit

Permalink
chore: better model class #64
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Oct 8, 2024
1 parent 7dc7cf5 commit 2fdc692
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 229 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ public function inits(): Collection
}

/**
* @param mixed[][] $sources
* @param array<mixed>|\Traversable<mixed> $sources
* @return Collection
*/
public function merge(iterable...$sources): Collection
public function merge(iterable ...$sources): Collection
{
return new self($this->collection->merge(...$sources));
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public function last(): Collection
*/
public function getIterator(): Iterator
{
yield from$this->collection->getIterator();
yield from $this->collection->getIterator();
}
/**
* @param int $index
Expand Down
16 changes: 4 additions & 12 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,17 @@ private function saveForeignOto(\Scrawler\Arca\Model $model): void
*/
private function saveForeignOtm(\Scrawler\Arca\Model $model, mixed $id): void
{
foreach ($model->getForeignModels('otm') as $foreigns) {
foreach ($foreigns as $foreign) {
foreach ($model->getForeignModels('otm') as $foreign) {
$key = $model->getName().'_id';
$foreign->$key = $id;
$this->createTables($foreign);
}
}
$this->connection->beginTransaction();
try {
foreach ($model->getForeignModels('otm') as $foreigns) {
foreach ($foreigns as $foreign) {
foreach ($model->getForeignModels('otm') as $foreign) {
$this->createRecords($foreign);
$foreign->cleanModel();
$foreign->setLoaded();
}
}
$this->connection->commit();
} catch (\Exception $e) {
Expand All @@ -242,8 +238,7 @@ private function saveForeignOtm(\Scrawler\Arca\Model $model, mixed $id): void
*/
private function saveForeignMtm(\Scrawler\Arca\Model $model, mixed $id): void
{
foreach ($model->getForeignModels('mtm') as $foreigns) {
foreach ($foreigns as $foreign) {
foreach ($model->getForeignModels('mtm') as $foreign) {
$model_id = $model->getName().'_id';
$foreign_id = $foreign->getName().'_id';
$relational_table = $this->create($model->getName().'_'.$foreign->getName());
Expand All @@ -256,12 +251,10 @@ private function saveForeignMtm(\Scrawler\Arca\Model $model, mixed $id): void
}
$this->createTables($relational_table);
$this->createTables($foreign);
}
}
$this->connection->beginTransaction();
try {
foreach ($model->getForeignModels('mtm') as $foreigns) {
foreach ($foreigns as $foreign) {
foreach ($model->getForeignModels('mtm') as $foreign) {
$rel_id = $this->createRecords($foreign);
$foreign->cleanModel();
$foreign->setLoaded();
Expand All @@ -271,7 +264,6 @@ private function saveForeignMtm(\Scrawler\Arca\Model $model, mixed $id): void
$relational_table->$model_id = $id;
$relational_table->$foreign_id = $rel_id;
$this->createRecords($relational_table);
}
}
$this->connection->commit();
} catch (\Exception $e) {
Expand Down
Loading

0 comments on commit 2fdc692

Please sign in to comment.