beyondcode / invoker-community

74 stars 2 forks source link

Opening Models with enums and other resources gives this errors #367

Open rzv-me opened 10 months ago

rzv-me commented 10 months ago

OS: darwin Invoker Version: 2.13.2 Laravel Version: 9.52.16 Local project: true PHP Binary: /opt/local/bin/php Route: /project/index?class=App%5CProject&time=1699447934372&project=local-pRqsxUxr4R3JiW2v2nbauD

Error:

ErrorException 

  Undefined array key 1

  at phar:/Applications/Invoker.app/Contents/Resources/invoker.phar/src/Factories/AttributeItem.php:39
     35▕     public static function getValues(\Illuminate\Database\Eloquent\Model $model, $column)
     36▕     {
     37▕         $type = $model->getConnection()->select("SHOW COLUMNS FROM `{$model->getTable()}` WHERE Field = '{$column->getName()}'")[0]->Type;
     38▕         \preg_match("/^enum\\(\\'(.*)\\'\\)\$/", $type, $matches);
  ➜  39▕         $values = \explode("','", $matches[1]);
     40▕         return $values;
     41▕     }
     42▕     public static function getCastFromColumn(\Illuminate\Database\Eloquent\Model $model, $column) : ?string
     43▕     {

  1   phar:/Applications/Invoker.app/Contents/Resources/invoker.phar/src/Factories/AttributeItem.php:39
      Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap\{closure}("Undefined array key 1", "phar:///Applications/Invoker.app/Contents/Resources/invoker.phar/src/Factories/AttributeItem.php")

  2   phar:/Applications/Invoker.app/Contents/Resources/invoker.phar/src/Factories/AttributeItem.php:25
      _HumbugBoxeb1c6394dc9c\Invoker\Factories\AttributeItem::getValues(Object(App\Project), Object(Doctrine\DBAL\Schema\Column))

Probably the reason this is broken is because the query isn't working completely on SingleStore. The fix is fairly easy in src/Factories/AttributeItem.php

@@ -34,7 +34,7 @@
     }
     public static function getValues(\Illuminate\Database\Eloquent\Model $model, $column)
     {
+        $type = $model->getConnection()->select("SHOW COLUMNS FROM `{$model->getTable()}` LIKE '{$column->getName()}'")[0]->Type;
-        $type = $model->getConnection()->select("SHOW COLUMNS FROM `{$model->getTable()}` WHERE Field = '{$column->getName()}'")[0]->Type;

         \preg_match("/^enum\\(\\'(.*)\\'\\)\$/", $type, $matches);
         $values = \explode("','", $matches[1]);