Skip to content

Commit 40ca9f8

Browse files
committed
Add suporte to name relationships
1 parent a356ef8 commit 40ca9f8

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
],
1111
"homepage": "https://github.com/phpzm/persistence",
1212
"license": "MIT",
13-
"version": "1.0.1",
13+
"version": "1.0.2",
1414
"type": "package",
1515
"authors": [
1616
{

src/Field.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,18 @@ public function validator($rule, $options = null, bool $clear = false): Field
217217
/**
218218
* @param string $class
219219
* @param string $target
220+
* @param string $name (null)
220221
* @return Field
221222
*/
222-
public function referencedBy(string $class, string $target): Field
223+
public function referencedBy(string $class, string $target, string $name = null): Field
223224
{
224-
$this->referenced[$target] = $class;
225+
if (!$name) {
226+
$name = get_class_short_name($class);
227+
}
228+
$this->referenced[$target] = [
229+
'name' => $name,
230+
'class' => $class
231+
];
225232
return $this;
226233
}
227234

@@ -230,16 +237,21 @@ public function referencedBy(string $class, string $target): Field
230237
*
231238
* @param string $class
232239
* @param string $referenced
233-
* @param bool $nullable
240+
* @param bool $nullable (false)
241+
* @param string $name (null)
234242
* @return Field
235243
* @throws SimplesRunTimeError
236244
*/
237-
public function referencesTo(string $class, string $referenced, bool $nullable = false): Field
245+
public function referencesTo(string $class, string $referenced, bool $nullable = false, string $name = null): Field
238246
{
239247
if (off($this->references, 'class')) {
240248
throw new SimplesRunTimeError("Relationship already defined to '{$this->references->class}'");
241249
}
250+
if (!$name) {
251+
$name = get_class_short_name($class);
252+
}
242253
$this->references = (object)[
254+
'name' => $name,
243255
'collection' => $this->getCollection(),
244256
'referenced' => $referenced,
245257
'class' => $class

0 commit comments

Comments
 (0)