laravel-ide-helper icon indicating copy to clipboard operation
laravel-ide-helper copied to clipboard

Database fields not written to _ide_helper_models.php

Open ionesculiviucristian opened this issue 3 years ago • 10 comments

Versions:

  • ide-helper Version: 2.8.2
  • Laravel Version: 8.18.1
  • PHP Version: 8.0.0
  • MySQL Version: 8.0.22

(using Docker and Laravel Passport, if it matters)

Description:

When running php artisan ide-helper:models --nowrite the model User.php located in app/Models doesn't write the database fields, only the default properties/methods.

    'model_locations' => [
        'app/Models',
    ],

In Barryvdh\LaravelIdeHelper\Console\ModelsCommand.php the lines:

        if (strpos($table, '.')) {
            [$database, $table] = explode('.', $table);
        }

expect the $table to have the database name in but for me $table is 'users' and as such returns here

        $columns = $schema->listTableColumns($table, $database);

        if (!$columns) {
            return;
        }

ionesculiviucristian avatar Dec 08 '20 23:12 ionesculiviucristian

@ionesculiviucristian is this a recent regression for your? Or did it newer work / new project?

If a regression, can you check out older ide-helper / Laravel versions?

thank you!

mfn avatar Dec 09 '20 04:12 mfn

Tried with 2.8.1 (lowest with Laravel 8 support) and 2.8.2. Same result.

After chopping the code/configs I ended up with php 7.3.25, Laravel 7.30.0 and laravel-ide-helper 2.7.0. I get the same generated file (deleted it first), without the database fields.

My _ide_helper_models.php file:

<?php

// @formatter:off
/**
 * A helper file for your Eloquent Models
 * Copy the phpDocs from this file to the correct Model,
 * And remove them from this file, to prevent double declarations.
 *
 * @author Barry vd. Heuvel <[email protected]>
 */


namespace App\Models{
/**
 * App\Models\User
 *
 * @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Passport\Client[] $clients
 * @property-read int|null $clients_count
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read int|null $notifications_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Passport\Token[] $tokens
 * @property-read int|null $tokens_count
 * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|User query()
 */
	class User extends \Eloquent {}
}

My User.php file:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

My ide-helper.php file:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Filename & Format
    |--------------------------------------------------------------------------
    |
    | The default filename (without extension) and the format (php or json)
    |
    */

    'filename'  => '_ide_helper',
    'format'    => 'php',

    /*
    |--------------------------------------------------------------------------
    | Where to write the PhpStorm specific meta file
    |--------------------------------------------------------------------------
    |
    | PhpStorm also supports the directory `.phpstorm.meta.php/` with arbitrary
    | files in it, should you need additional files for your project; e.g.
    | `.phpstorm.meta.php/laravel_ide_Helper.php'.
    |
    */
    'meta_filename' => '.phpstorm.meta.php',

    /*
    |--------------------------------------------------------------------------
    | Fluent helpers
    |--------------------------------------------------------------------------
    |
    | Set to true to generate commonly used Fluent methods
    |
    */

    'include_fluent' => true,

    /*
    |--------------------------------------------------------------------------
    | Factory Builders
    |--------------------------------------------------------------------------
    |
    | Set to true to generate factory generators for better factory()
    | method auto-completion.
    |
    */

    'include_factory_builders' => false,

    /*
    |--------------------------------------------------------------------------
    | Write Model Magic methods
    |--------------------------------------------------------------------------
    |
    | Set to false to disable write magic methods of model
    |
    */

    'write_model_magic_where' => true,

    /*
    |--------------------------------------------------------------------------
    | Write Model relation count properties
    |--------------------------------------------------------------------------
    |
    | Set to false to disable writing of relation count properties to model DocBlocks.
    |
    */

    'write_model_relation_count_properties' => true,

    /*
    |--------------------------------------------------------------------------
    | Write Eloquent Model Mixins
    |--------------------------------------------------------------------------
    |
    | This will add the necessary DocBlock mixins to the model class
    | contained in the Laravel Framework. This helps the IDE with
    | auto-completion.
    |
    | Please be aware that this setting changes a file within the /vendor directory.
    |
    */

    'write_eloquent_model_mixins' => false,

    /*
    |--------------------------------------------------------------------------
    | Helper files to include
    |--------------------------------------------------------------------------
    |
    | Include helper files. By default not included, but can be toggled with the
    | -- helpers (-H) option. Extra helper files can be included.
    |
    */

    'include_helpers' => false,

    'helper_files' => [
        base_path() . '/vendor/laravel/framework/src/Illuminate/Support/helpers.php',
    ],

    /*
    |--------------------------------------------------------------------------
    | Model locations to include
    |--------------------------------------------------------------------------
    |
    | Define in which directories the ide-helper:models command should look
    | for models.
    |
    | glob patterns are supported to easier reach models in sub-directories,
    | e.g. `app/Services/* /Models` (without the space)
    |
    */

    'model_locations' => [
        'app/Models',
    ],

    /*
    |--------------------------------------------------------------------------
    | Models to ignore
    |--------------------------------------------------------------------------
    |
    | Define which models should be ignored.
    |
    */

    'ignored_models' => [

    ],

    /*
    |--------------------------------------------------------------------------
    | Extra classes
    |--------------------------------------------------------------------------
    |
    | These implementations are not really extended, but called with magic functions
    |
    */

    'extra' => [
        'Eloquent' => ['Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'],
        'Session' => ['Illuminate\Session\Store'],
    ],

    'magic' => [],

    /*
    |--------------------------------------------------------------------------
    | Interface implementations
    |--------------------------------------------------------------------------
    |
    | These interfaces will be replaced with the implementing class. Some interfaces
    | are detected by the helpers, others can be listed below.
    |
    */

    'interfaces' => [

    ],

    /*
    |--------------------------------------------------------------------------
    | Support for custom DB types
    |--------------------------------------------------------------------------
    |
    | This setting allow you to map any custom database type (that you may have
    | created using CREATE TYPE statement or imported using database plugin
    | / extension to a Doctrine type.
    |
    | Each key in this array is a name of the Doctrine2 DBAL Platform. Currently valid names are:
    | 'postgresql', 'db2', 'drizzle', 'mysql', 'oracle', 'sqlanywhere', 'sqlite', 'mssql'
    |
    | This name is returned by getName() method of the specific Doctrine/DBAL/Platforms/AbstractPlatform descendant
    |
    | The value of the array is an array of type mappings. Key is the name of the custom type,
    | (for example, "jsonb" from Postgres 9.4) and the value is the name of the corresponding Doctrine2 type (in
    | our case it is 'json_array'. Doctrine types are listed here:
    | http://doctrine-dbal.readthedocs.org/en/latest/reference/types.html
    |
    | So to support jsonb in your models when working with Postgres, just add the following entry to the array below:
    |
    | "postgresql" => array(
    |       "jsonb" => "json_array",
    |  ),
    |
    */
    'custom_db_types' => [

    ],

    /*
     |--------------------------------------------------------------------------
     | Support for camel cased models
     |--------------------------------------------------------------------------
     |
     | There are some Laravel packages (such as Eloquence) that allow for accessing
     | Eloquent model properties via camel case, instead of snake case.
     |
     | Enabling this option will support these packages by saving all model
     | properties as camel case, instead of snake case.
     |
     | For example, normally you would see this:
     |
     |  * @property \Illuminate\Support\Carbon $created_at
     |  * @property \Illuminate\Support\Carbon $updated_at
     |
     | With this enabled, the properties will be this:
     |
     |  * @property \Illuminate\Support\Carbon $createdAt
     |  * @property \Illuminate\Support\Carbon $updatedAt
     |
     | Note, it is currently an all-or-nothing option.
     |
     */
    'model_camel_case_properties' => false,

    /*
    |--------------------------------------------------------------------------
    | Property Casts
    |--------------------------------------------------------------------------
    |
    | Cast the given "real type" to the given "type".
    |
    */
    'type_overrides' => [
        'integer' => 'int',
        'boolean' => 'bool',
    ],

    /*
    |--------------------------------------------------------------------------
    | Include DocBlocks from classes
    |--------------------------------------------------------------------------
    |
    | Include DocBlocks from classes to allow additional code inspection for
    | magic methods and properties.
    |
    */
    'include_class_docblocks' => false,

    /*
    |--------------------------------------------------------------------------
    | Force FQN usage
    |--------------------------------------------------------------------------
    |
    | Use the fully qualified (class) name in docBlock,
    | event if class exists in a given file
    | or there is an import (use className) of a given class
    |
    */
    'force_fqn' => false,
];

ionesculiviucristian avatar Dec 09 '20 20:12 ionesculiviucristian

Your config looks exactly like mine, I'm on PHP 7.4.13 with Laravel 8.17.2 and ide-helper 2.8.1/2.8.2 but work with and without --nowrite.

mfn avatar Dec 09 '20 22:12 mfn

Do you have doctrine/dbal installed?

mfn avatar Dec 09 '20 22:12 mfn

@mfn No. i don't but for whatever reason, today, after doing a composer update, it just worked. I guess the issue can be closed.

I see this package requires "doctrine/dbal": "~2.3". Is that not for getting the database fields?

ionesculiviucristian avatar Dec 14 '20 19:12 ionesculiviucristian

It is, basically doctrine/dbal is required for the feature you expected to work :}

Do you still feel we can close this? I'm not sure I entirely follow how it was suddenly fixed for your and we may still have something to improve?

mfn avatar Dec 15 '20 15:12 mfn

I'm having the same problem. The database fields are not written. I use docker

Versions: ide-helper Version: 2.10 Lumen Version: 8.3.1 PHP Version: 8.0.0 Postgre Version: 12 doctrine/dbal: 2.13.4

I'm using multiple schemas on postgre, like: Schema |---Table 1 |-- Table 2 Schema 2 |---Table 3 |-- Table 4 ...

bianchi avatar Oct 25 '21 12:10 bianchi

Just found out that if I comment the explode line, it works:

public function getPropertiesFromTable($model)
    {
        $table = $model->getConnection()->getTablePrefix() . $model->getTable();
        $schema = $model->getConnection()->getDoctrineSchemaManager();
        $databasePlatform = $schema->getDatabasePlatform();
        $databasePlatform->registerDoctrineTypeMapping('enum', 'string');

        $platformName = $databasePlatform->getName();
        $customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", []);
        foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
            $databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
        }

        $database = null;
//        if (strpos($table, '.')) {
//            [$database, $table] = explode('.', $table);
//        }

        $columns = $schema->listTableColumns($table, $database);

        if (!$columns) {
            return;
        }

        foreach ($columns as $column) {
...

bianchi avatar Oct 25 '21 12:10 bianchi

I can confirm this works - it doesn't support tables that are in a different schema than dbo

In model: $table = 'sc.MyTable';

Thanks for the workaround @bianchi

ossicoinc avatar Jan 05 '22 01:01 ossicoinc

https://github.com/barryvdh/laravel-ide-helper/pull/1349 should fix the schema issue.

SimplyCorey avatar May 06 '22 15:05 SimplyCorey

Closing:

mfn avatar Feb 20 '23 14:02 mfn