Skip to content

Commit

Permalink
Merge pull request #306 from bowphp/fix/bugs
Browse files Browse the repository at this point in the history
feat(barry): add relative create method for barry model
  • Loading branch information
papac authored Dec 21, 2024
2 parents 95fbe62 + 32e6854 commit b70bd03
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 92 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ramsey/uuid": "^4.7"
},
"require-dev": {
"pda/pheanstalk": "^4.0",
"pda/pheanstalk": "^4.0.5",
"phpunit/phpunit": "^9",
"monolog/monolog": "^1.22",
"twig/twig": "^3",
Expand Down
56 changes: 42 additions & 14 deletions src/Database/Barry/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@

abstract class Relation
{
/**
* The foreign key of the parent model.
*
* @var string
*/
protected string $foreign_key;

/**
* The associated key on the parent model.
*
* @var string
*/
protected string $local_key;

/**
* The parent model instance
*
Expand Down Expand Up @@ -54,6 +68,7 @@ public function __construct(Model $related, Model $parent)
{
$this->parent = $parent;
$this->related = $related;

$this->query = $this->related::query();

// Build the constraint effect
Expand All @@ -62,20 +77,6 @@ public function __construct(Model $related, Model $parent)
}
}

/**
* Set the base constraints on the relation query.
*
* @return void
*/
abstract public function addConstraints(): void;

/**
* Get the results of the relationship.
*
* @return mixed
*/
abstract public function getResults(): mixed;

/**
* Get the parent model.
*
Expand Down Expand Up @@ -113,4 +114,31 @@ public function __call(string $method, array $args)

return $result;
}

/**
* Create a new row of the related
*
* @param array $attributes
* @return Model
*/
public function create(array $attributes): Model
{
$attributes[$this->foreign_key] = $this->parent->getKeyValue();

return $this->related->create($attributes);
}

/**
* Get the results of the relationship.
*
* @return mixed
*/
abstract public function getResults(): mixed;

/**
* Set the base constraints on the relation query.
*
* @return void
*/
abstract public function addConstraints(): void;
}
15 changes: 1 addition & 14 deletions src/Database/Barry/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@

class BelongsTo extends Relation
{
/**
* The foreign key of the parent model.
*
* @var string
*/
protected $foreign_key;

/**
* The associated key on the parent model.
*
* @var string
*/
protected $local_key;

/**
* Create a new belongs to relationship instance.
*
Expand Down Expand Up @@ -52,6 +38,7 @@ public function __construct(
public function getResults(): ?Model
{
$key = $this->query->getTable() . ":belongsto:" . $this->related->getTable() . ":" . $this->foreign_key;

$cache = Cache::store('file')->get($key);

if (!is_null($cache)) {
Expand Down
25 changes: 8 additions & 17 deletions src/Database/Barry/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@

class BelongsToMany extends Relation
{
/**
* The foreign key of the parent model.
*
* @var string
*/
protected $foreign_key;

/**
* The associated key on the parent model.
*
* @var string
*/
protected $local_key;

/**
* Create a new belongs to relationship instance.
*
Expand All @@ -47,7 +33,6 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
*/
public function getResults(): Collection
{
// TODO: Cache the result
return $this->query->get();
}

Expand All @@ -58,8 +43,14 @@ public function getResults(): Collection
*/
public function addConstraints(): void
{
if (static::$has_constraints) {
// Todo
if (!static::$has_constraints) {
return;
}

// For belongs to relationships, which are essentially the inverse of has one
// or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent.
$foreign_key_value = $this->parent->getAttribute($this->foreign_key);
$this->query->where($this->local_key, '=', $foreign_key_value);
}
}
19 changes: 1 addition & 18 deletions src/Database/Barry/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@

class HasMany extends Relation
{
/**
* The foreign key of the parent model.
*
* @var string
*/
protected string $foreign_key;

/**
* The associated key on the parent model.
*
* @var string
*/
protected string $local_key;

/**
* Create a new belongs to relationship instance.
*
Expand All @@ -50,7 +36,6 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
*/
public function getResults(): Collection
{
// TODO: Cache the result
return $this->query->get();
}

Expand All @@ -61,8 +46,6 @@ public function getResults(): Collection
*/
public function addConstraints(): void
{
if (static::$has_constraints) {
// Todo
}
//
}
}
23 changes: 5 additions & 18 deletions src/Database/Barry/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@

class HasOne extends Relation
{
/**
* The foreign key of the parent model.
*
* @var string
*/
protected string $foreign_key;

/**
* The associated key on the parent model.
*
* @var string
*/
protected string $local_key;

/**
* Create a new belongs to relationship instance.
*
Expand All @@ -38,8 +24,6 @@ public function __construct(Model $related, Model $parent, string $foreign_key,

$this->local_key = $local_key;
$this->foreign_key = $foreign_key;

$this->query = $this->query->where($this->foreign_key, $this->parent->$local_key);
}

/**
Expand All @@ -50,6 +34,7 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
public function getResults(): ?Model
{
$key = $this->query->getTable() . ":hasone:" . $this->related->getTable() . ":" . $this->foreign_key;

$cache = Cache::store('file')->get($key);

if (!is_null($cache)) {
Expand All @@ -74,8 +59,10 @@ public function getResults(): ?Model
*/
public function addConstraints(): void
{
if (static::$has_constraints) {
// Todo
if (!static::$has_constraints) {
return;
}

$this->query = $this->query->where($this->foreign_key, $this->parent->getAttribute($this->local_key));
}
}
11 changes: 1 addition & 10 deletions src/Database/Barry/Traits/CanSerialized.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,12 @@ trait CanSerialized
*
* @return string
*/
public function __sleep()
public function __sleep(): array
{
if ($this instanceof Model) {
return ['attributes' => $this->attributes];
}

return ['attributes' => $this->toArray()];
}

/**
* __wakeup
*
* @return string
*/
public function __wakeup()
{
}
}

0 comments on commit b70bd03

Please sign in to comment.