From c9132b28a6fab5e76207ac653e842f584eb3759d Mon Sep 17 00:00:00 2001 From: edvordo Date: Tue, 18 Jun 2019 16:38:42 +0200 Subject: [PATCH] Chages to x2Many relationships detection & adds relationship_count docBlock comment (#783) * x to Many relationships are detected via a `Many` keyword in the relationship object class name * reason: example: `belongsTo` was incorrectly recognized as `belongsToMany` * adds `@property-read int|null $x2Many_relationship_method_name_count` model class docBlock comment * when model makes use of `withCount` or `loadCount` there is a property on the resource `$instance->relationship_method_count` that states the count of related models * can be `NULL` when `withCount` or `loadCount` aren't used --- src/Console/ModelsCommand.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 9c3ccbe..a07ed07 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -503,7 +503,7 @@ class ModelsCommand extends Command 'morphToMany', 'morphedByMany', ]; - if (in_array($relation, $relations)) { + if (strpos(get_class($relationObj), 'Many') !== false) { //Collection or array of models (because Collection is Arrayable) $this->setProperty( $method, @@ -511,6 +511,12 @@ class ModelsCommand extends Command true, null ); + $this->setProperty( + Str::snake($method) . '_count', + 'int|null', + true, + false + ); } elseif ($relation === "morphTo") { // Model isn't specified because relation is polymorphic $this->setProperty(