我有一张表格如下:
| common_menus | CREATE TABLE `common_menus` (
`menu_id` tinyint(3) unsigned NOT NULL,
`branch_id` smallint(5) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`price` smallint(5) unsigned NOT NULL,
`type` tinyint(3) unsigned NOT NULL COMMENT '0 - 255',
`order` tinyint(3) unsigned NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
该表对应的模型如下:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Menu extends Model
{
use SoftDeletes;
const TABLE = 'common_menus';
const MENU_ID = 'menu_id';
const BRANCH_ID = 'branch_id';
const NAME = 'name';
const PRICE = 'price';
const TYPE = 'type';
const ORDER = 'order';
const TABLE_MENU_ID = self::TABLE . '.' . self::MENU_ID;
const TABLE_BRANCH_ID = self::TABLE . '.' . self::BRANCH_ID;
const TABLE_NAME = self::TABLE . '.' . self::NAME;
const TABLE_PRICE = self::TABLE . '.' . self::PRICE;
const TABLE_TYPE = self::TABLE . '.' . self::TYPE;
const TABLE_ORDER = self::TABLE . '.' . self::ORDER;
protected $table = self::TABLE;
protected $dates = ['deleted_at'];
protected $fillable = [self::MENU_ID, self::BRANCH_ID, self::NAME, self::PRICE, self::TYPE, self::ORDER];
const STANDARD_MENU = 1;
const COMBO_MENU = 2;
const OPTION_MENU = 3;
// static methods follows
}
当调用删除模型的方法时,我遇到了如下错误:
QueryException in Connection.php line 651:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `common_menus` set `deleted_at` = 2016-03-29 14:01:01 where `id` is null)
我发现只要表没有主键,我就应该声明如下。
protected $primaryKey = null;
好的,我也有不同但相似的表格如下:
| common_therapists | CREATE TABLE `common_therapists` (
`therapist_id` int(10) unsigned NOT NULL COMMENT '0 - 4294967295',
`branch_id` smallint(5) unsigned NOT NULL,
`name` varchar(63) COLLATE utf8mb4_unicode_ci NOT NULL,
`has_license` tinyint(1) unsigned DEFAULT NULL,
`lmt` varchar(5) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
该表对应的模型如下:
<?php
namespace App;
use DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Therapist extends Model
{
use SoftDeletes;
const TABLE = 'common_therapists';
// const KEY = 'therapist_id';
// const THERAPIST_ID = self::KEY;
const THERAPIST_ID = 'therapist_id';
const TABLE_THERAPIST_ID = self::TABLE . '.' . self::THERAPIST_ID;
const BRANCH_ID = 'branch_id';
const TABLE_BRANCH_ID = self::TABLE . '.' . self::BRANCH_ID;
const NAME = 'name';
const TABLE_THERAPIST_NAME = self::TABLE . '.' . self::NAME;
const DELETED_AT = 'deleted_at';
const MASSSAGE_THERAPIST = 0;
const LICENSED_MASSAGE_THERAPIST = 1;
protected $table = self::TABLE;
// protected $primaryKey = self::KEY;
protected $dates = ['deleted_at'];
protected $perPage = 10;
protected $fillable = ['therapist_id', 'branch_id', 'name', 'has_license', 'lmt'];
// static methods follows
}
在此模型和表具有主键之前,但由于需求更改而将其删除。
治疗师的表和模型完全有效,即使它没有主键声明。
我的问题是表格和菜单模型是否有问题,或治疗师的表格和模型是否有误?
请说明问题或我的误解。提前谢谢。