Skip to content

Commit b70bd03

Browse files
authored
Merge pull request #306 from bowphp/fix/bugs
feat(barry): add relative create method for barry model
2 parents 95fbe62 + 32e6854 commit b70bd03

File tree

7 files changed

+59
-92
lines changed

7 files changed

+59
-92
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"ramsey/uuid": "^4.7"
1919
},
2020
"require-dev": {
21-
"pda/pheanstalk": "^4.0",
21+
"pda/pheanstalk": "^4.0.5",
2222
"phpunit/phpunit": "^9",
2323
"monolog/monolog": "^1.22",
2424
"twig/twig": "^3",

src/Database/Barry/Relation.php

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99

1010
abstract class Relation
1111
{
12+
/**
13+
* The foreign key of the parent model.
14+
*
15+
* @var string
16+
*/
17+
protected string $foreign_key;
18+
19+
/**
20+
* The associated key on the parent model.
21+
*
22+
* @var string
23+
*/
24+
protected string $local_key;
25+
1226
/**
1327
* The parent model instance
1428
*
@@ -54,6 +68,7 @@ public function __construct(Model $related, Model $parent)
5468
{
5569
$this->parent = $parent;
5670
$this->related = $related;
71+
5772
$this->query = $this->related::query();
5873

5974
// Build the constraint effect
@@ -62,20 +77,6 @@ public function __construct(Model $related, Model $parent)
6277
}
6378
}
6479

65-
/**
66-
* Set the base constraints on the relation query.
67-
*
68-
* @return void
69-
*/
70-
abstract public function addConstraints(): void;
71-
72-
/**
73-
* Get the results of the relationship.
74-
*
75-
* @return mixed
76-
*/
77-
abstract public function getResults(): mixed;
78-
7980
/**
8081
* Get the parent model.
8182
*
@@ -113,4 +114,31 @@ public function __call(string $method, array $args)
113114

114115
return $result;
115116
}
117+
118+
/**
119+
* Create a new row of the related
120+
*
121+
* @param array $attributes
122+
* @return Model
123+
*/
124+
public function create(array $attributes): Model
125+
{
126+
$attributes[$this->foreign_key] = $this->parent->getKeyValue();
127+
128+
return $this->related->create($attributes);
129+
}
130+
131+
/**
132+
* Get the results of the relationship.
133+
*
134+
* @return mixed
135+
*/
136+
abstract public function getResults(): mixed;
137+
138+
/**
139+
* Set the base constraints on the relation query.
140+
*
141+
* @return void
142+
*/
143+
abstract public function addConstraints(): void;
116144
}

src/Database/Barry/Relations/BelongsTo.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@
1010

1111
class BelongsTo extends Relation
1212
{
13-
/**
14-
* The foreign key of the parent model.
15-
*
16-
* @var string
17-
*/
18-
protected $foreign_key;
19-
20-
/**
21-
* The associated key on the parent model.
22-
*
23-
* @var string
24-
*/
25-
protected $local_key;
26-
2713
/**
2814
* Create a new belongs to relationship instance.
2915
*
@@ -52,6 +38,7 @@ public function __construct(
5238
public function getResults(): ?Model
5339
{
5440
$key = $this->query->getTable() . ":belongsto:" . $this->related->getTable() . ":" . $this->foreign_key;
41+
5542
$cache = Cache::store('file')->get($key);
5643

5744
if (!is_null($cache)) {

src/Database/Barry/Relations/BelongsToMany.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@
1010

1111
class BelongsToMany extends Relation
1212
{
13-
/**
14-
* The foreign key of the parent model.
15-
*
16-
* @var string
17-
*/
18-
protected $foreign_key;
19-
20-
/**
21-
* The associated key on the parent model.
22-
*
23-
* @var string
24-
*/
25-
protected $local_key;
26-
2713
/**
2814
* Create a new belongs to relationship instance.
2915
*
@@ -47,7 +33,6 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
4733
*/
4834
public function getResults(): Collection
4935
{
50-
// TODO: Cache the result
5136
return $this->query->get();
5237
}
5338

@@ -58,8 +43,14 @@ public function getResults(): Collection
5843
*/
5944
public function addConstraints(): void
6045
{
61-
if (static::$has_constraints) {
62-
// Todo
46+
if (!static::$has_constraints) {
47+
return;
6348
}
49+
50+
// For belongs to relationships, which are essentially the inverse of has one
51+
// or has many relationships, we need to actually query on the primary key
52+
// of the related models matching on the foreign key that's on a parent.
53+
$foreign_key_value = $this->parent->getAttribute($this->foreign_key);
54+
$this->query->where($this->local_key, '=', $foreign_key_value);
6455
}
6556
}

src/Database/Barry/Relations/HasMany.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@
1010

1111
class HasMany extends Relation
1212
{
13-
/**
14-
* The foreign key of the parent model.
15-
*
16-
* @var string
17-
*/
18-
protected string $foreign_key;
19-
20-
/**
21-
* The associated key on the parent model.
22-
*
23-
* @var string
24-
*/
25-
protected string $local_key;
26-
2713
/**
2814
* Create a new belongs to relationship instance.
2915
*
@@ -50,7 +36,6 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
5036
*/
5137
public function getResults(): Collection
5238
{
53-
// TODO: Cache the result
5439
return $this->query->get();
5540
}
5641

@@ -61,8 +46,6 @@ public function getResults(): Collection
6146
*/
6247
public function addConstraints(): void
6348
{
64-
if (static::$has_constraints) {
65-
// Todo
66-
}
49+
//
6750
}
6851
}

src/Database/Barry/Relations/HasOne.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@
1010

1111
class HasOne extends Relation
1212
{
13-
/**
14-
* The foreign key of the parent model.
15-
*
16-
* @var string
17-
*/
18-
protected string $foreign_key;
19-
20-
/**
21-
* The associated key on the parent model.
22-
*
23-
* @var string
24-
*/
25-
protected string $local_key;
26-
2713
/**
2814
* Create a new belongs to relationship instance.
2915
*
@@ -38,8 +24,6 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
3824

3925
$this->local_key = $local_key;
4026
$this->foreign_key = $foreign_key;
41-
42-
$this->query = $this->query->where($this->foreign_key, $this->parent->$local_key);
4327
}
4428

4529
/**
@@ -50,6 +34,7 @@ public function __construct(Model $related, Model $parent, string $foreign_key,
5034
public function getResults(): ?Model
5135
{
5236
$key = $this->query->getTable() . ":hasone:" . $this->related->getTable() . ":" . $this->foreign_key;
37+
5338
$cache = Cache::store('file')->get($key);
5439

5540
if (!is_null($cache)) {
@@ -74,8 +59,10 @@ public function getResults(): ?Model
7459
*/
7560
public function addConstraints(): void
7661
{
77-
if (static::$has_constraints) {
78-
// Todo
62+
if (!static::$has_constraints) {
63+
return;
7964
}
65+
66+
$this->query = $this->query->where($this->foreign_key, $this->parent->getAttribute($this->local_key));
8067
}
8168
}

src/Database/Barry/Traits/CanSerialized.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,12 @@ trait CanSerialized
1313
*
1414
* @return string
1515
*/
16-
public function __sleep()
16+
public function __sleep(): array
1717
{
1818
if ($this instanceof Model) {
1919
return ['attributes' => $this->attributes];
2020
}
2121

2222
return ['attributes' => $this->toArray()];
2323
}
24-
25-
/**
26-
* __wakeup
27-
*
28-
* @return string
29-
*/
30-
public function __wakeup()
31-
{
32-
}
3324
}

0 commit comments

Comments
 (0)