Skip to content

Commit

Permalink
Fixed bug that the relation comments cannot be created by gen:model
Browse files Browse the repository at this point in the history
…. (#6561)



Co-authored-by: 李铭昕 <715557344@qq.com>
  • Loading branch information
szutoutou and limingxinleo committed Mar 7, 2024
1 parent 9528e1d commit 2788b80
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Commands/Ast/ModelUpdateVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,11 @@ protected function initPropertiesFromMethods()
continue;
}

$return = end($methodStmt->stmts);
if ($return instanceof Node\Stmt\Return_) {
foreach ($methodStmt->stmts as $return) {
if (! $return instanceof Node\Stmt\Return_) {
continue;
}

$expr = $return->expr;
if (
$expr instanceof Node\Expr\MethodCall
Expand Down Expand Up @@ -310,6 +313,7 @@ protected function initPropertiesFromMethods()
}
}
}
break;
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/GenModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public function testGenModelWithEnum()
* @property \\HyperfTest\\Database\\Stubs\\Model\\Gender \$gender
* @property \\Carbon\\Carbon \$created_at
* @property \\Carbon\\Carbon \$updated_at
* @property-read null|Book \$book
*/
class UserEnum extends Model
{
Expand All @@ -155,6 +156,12 @@ class UserEnum extends Model
* The attributes that should be cast to native types.
*/
protected array \$casts = ['id' => 'integer', 'gender' => Gender::class, 'created_at' => 'datetime', 'updated_at' => 'datetime'];
public function book()
{
var_dump(1);
return \$this->hasOne(Book::class, 'user_id', 'id');
// ignore
}
}", $code);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/Stubs/Model/UserEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ class UserEnum extends Model
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'gender' => Gender::class, 'created_at' => 'datetime', 'updated_at' => 'datetime'];

public function book()
{
var_dump(1);
return $this->hasOne(Book::class, 'user_id', 'id'); // ignore
}
}

0 comments on commit 2788b80

Please sign in to comment.