OcotberCMS - " SQLSTATE [42S22]:未找到列:1054未知列' users.application_id'

时间:2017-02-14 18:08:10

标签: php laravel eloquent octobercms

我收到关系中的错误消息,但我无法找到解决此错误所需的操作。

我有一个带有控制器和组件的应用程序模型。在后端,我可以添加一个新的应用程序记录。但要查看我收到错误。

  "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.application_id' in 'where clause' (SQL: select * from `users` where `users`.`deleted_at` is null and `users`.`application_id` = 1 and `users`.`application_id` is not null limit 1)" on line 662 of /srv/users/sssandwich/apps/website/public/vendor/laravel/framework/src/Illuminate/Database/Connection.php

当我在fields.yaml中添加此代码时,我隔离了错误 极致\模型\应用\ fields.yaml。

# ===================================
#  Form Field Definitions
# ===================================

fields:         
    client:
        label: Client
        type: relation
        disabled: true
        select: concat(name, ' ', surname)
        span: left

Acme\Models\Application.php

我将关系设置为

/**
     * @var array Relations
     */
    public $hasOne = [
    'client' => ['RainLab\User\Models\User', 'table' => 'users'],
    'cv' => ['Acme\Acme\Models\Cv']
    ];
    public $hasMany = [];
    public $belongsTo = [
    'owner' => ['Backend\Models\User']
    ];

以下是数据库结构......

public function up()
{
    Schema::create('acme_acme_applications', function(Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('jobs_id')->nullable()->index();
        $table->string('application_id')->nullable()->index();
        $table->string('owner_id')->nullable()->index();
        $table->string('user_id')->nullable()->index();
        $table->string('vacancy_id')->nullable()->index();
        $table->string('ref_no', 50)->nullable();
        $table->char('status_update')->nullable();
        $table->timestamps();
    });
}

我有grep' application_id'它只出现在db结构中。 users表是Rainlab用户插件,但我不确定为什么它试图在该表中找到application_id。

1 个答案:

答案 0 :(得分:3)

 "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.application_id'...

我认为这个错误是不言自明的,假设应用程序模型具有application_id外键,因为您定义了一对一关系;

如果应用程序有一个用户,则用户的表格应包含application_id列:

 public $hasOne = [
    'client' => [
    'RainLab\User\Models\User',
    'table' => 'users',
    'key' => 'application_id', // the foreign key in User's table
    'otherKey' => 'id', // Model record id
        ],
    ]; 

您应该在用户模型中定义反向关系,用户belongsTo

 public $belongsTo = [
    'application' => [
    'Acme\Models\Application',
    'table' => 'acme_acme_applications',
    'key' => 'application_id',
    'otherKey' => 'id',
        ],
    ];  

如果您不想直接编辑RainLab用户的插件(更新将在更新期间丢失),请查看here如何扩展插件并添加多个列到数据库。