【问题标题】:Unwanted prefix is added to my column names on queries不需要的前缀被添加到我的查询列名中
【发布时间】:2019-04-24 16:50:18
【问题描述】:

正如标题所说,我在查询和 Laravel 方面遇到了问题。由于某些原因,前缀“dev_”被添加到我的列名中(只有列名,而不是表或其他)。 这当然会导致以下错误,因为该列没有“dev_”前缀

Column names error

问题发生在运行 Apache 和 Laravel 5.0.18 的 Ubuntu 服务器上。 我对其进行了设置,以便它处理多个数据库(一个用于生产,一个用于开发)。 这是我的 config/database.php 的连接

...

'default' => 'mysql',

'connections' => [

        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => storage_path().'/database.sqlite',
            'prefix'   => '',
        ],

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'mysqldev' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DEV_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

    ]

...

生产数据库(mysql)和开发数据库(mysqldev)在表、列等方面是相同的...... 我通过雄辩的模型在我的api中使用它们(每次一个模型用于prod,一个模型用于dev)我为我的dev api设置了一个路由组前缀,它与prod api具有相同的端点,但正在使用开发模型。 它对于 prod API 工作得非常好,但在 dev API 上会发生上述问题。 这是我的模型, 用户:

<?php namespace pgc;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    public function saves()
    {
        return $this->hasMany('pgc\Save');
    }

    public function savespgc()
    {
        return $this->hasMany('pgc\Save')->where('bya','=','0');
    }

    public function savesbya()
    {
        return $this->hasMany('pgc\Save')->where('bya','=','1')->orderby('name','ASC');
    }


    public function screenshots()
    {
        return $this->hasMany('pgc\Screenshot');
    }

    public function screenshotsbya()
    {
        return $this->hasMany('pgc\Screenshot')->where('bya','=','1')->where('hidden','=','0');
    }
    public function screenshotspgc()
    {
        return $this->hasMany('pgc\Screenshot')->where('bya','=','0')->where('hidden','=','0');
    }



    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

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

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token','duid','updated_at','created_at'];

}

开发用户:

<?php namespace pgc;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class DevUser extends Model implements AuthenticatableContract, CanResetPasswordContract {

    public function saves()
    {
        return $this->hasMany('pgc\DevSave');
    }

    public function savespgc()
    {
        return $this->hasMany('pgc\DevSave')->where('bya','=','0');
    }

    public function savesbya()
    {
        return $this->hasMany('pgc\DevSave')->where('bya','=','1')->orderby('name','ASC');
    }


    public function screenshots()
    {
        return $this->hasMany('pgc\DevScreenshot');
    }

    public function screenshotsbya()
    {
        return $this->hasMany('pgc\DevScreenshot')->where('bya','=','1')->where('hidden','=','0');
    }
    public function screenshotspgc()
    {
        return $this->hasMany('pgc\DevScreenshot')->where('bya','=','0')->where('hidden','=','0');
    }



    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
    protected $connection = 'mysqldev';

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

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token','duid','updated_at','created_at'];

}

保存:

<?php namespace pgc;

use Illuminate\Database\Eloquent\Model;


class Save extends Model {

    public function user()
    {
        return $this->belongsTo('pgc\User');
    }

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'saves';

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

}

开发保存:

<?php namespace pgc;

use Illuminate\Database\Eloquent\Model;


class DevSave extends Model {

    public function user()
    {
        return $this->belongsTo('pgc\DevUser');
    }

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'saves';
    protected $connection = 'mysqldev';

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

}

这就是开发路由组前缀的设置方式:

Route::group(['prefix' => 'devs'], function () 
{
    ...

    Route::post('getpreconfig', function()
    {
    $bya = false;
    if (!is_null(Input::get('type')))
    {
        if (Input::get('type') == "bya") 
            $bya = true;
    }

    $user = pgc\DevUser::where('name', '=', "admin")->first();

    if(is_null($user)) 
        return ("error:user not found or logged in!");

    if ($bya)
        $allsaves = $user->savesbya;
    else
        $allsaves = $user->savespgc;

    if (is_null($allsaves))     
        return ("empty");


    //echo ($allsaves);

    return $allsaves->toJson();
    });

    ...

});

对于生产端,它是相同的后端点函数,但使用的是用户模型。 (就像我上面所说的,它在生产方面工作得很好)。

【问题讨论】:

  • 从发生这种情况的服务器打开php artisan tinker,然后打开config('database')。看看那里是否设置了prefix。如果是,请使用php artisan config:clear 清除配置缓存。你有没有故意使用前缀?
  • 没有设置前缀,也没有我从来没有故意使用前缀。

标签: php laravel laravel-5


【解决方案1】:

您的模型名称是DevUser,但您在数据库中使用的是user_id。如果你没有明确指定用于关系的列,Laravel 会尝试猜测它,这就是 dev_user_id 的来源。如下定义您的关系:

DevSave.php:

public function user(){
    return $this->belongsTo('pgc\DevUser', 'user_id');
}

或者考虑在关系名称中使用dev,例如public function devUser()

您的代码在生产中有效的原因是您使用的是 User,而不是 DevUser,因此 Laravel 正确猜测 user_id 是您要在关系中使用的列。

【讨论】:

  • 确实是这个问题,谢谢。因为我不想重命名我的列,所以我想要第一个解决方案,并且还必须对我的 DevUser Functions 进行以下更改:public function saves() { return $this-&gt;hasMany('pgc\DevSave', 'user_id'); }
猜你喜欢
  • 2015-12-05
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多