$model->;save()不';不使用实体
我正在慢慢学习CodeIgniter的第4版,我偶然发现了一个奇怪的行为: 当使用我的模型的$model->;save()不';不使用实体,model,entities,codeigniter-4,Model,Entities,Codeigniter 4,我正在慢慢学习CodeIgniter的第4版,我偶然发现了一个奇怪的行为: 当使用我的模型的save()方法时,它只保存在处创建的和在处更新的时间戳 下面是有用的代码 迁移 注意:工作正常,在数据库中正确创建了该表,如下所示 +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra
save()
方法时,它只保存在处创建的和在
处更新的时间戳
下面是有用的代码
迁移
注意:工作正常,在数据库中正确创建了该表,如下所示
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(9) | NO | PRI | NULL | auto_increment |
| title | varchar(127) | NO | | NULL | |
| content | text | NO | | NULL | |
| slug | varchar(127) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| deleted_at | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
实体
模型
调用模型
我尝试从两个不同的地方调用模型:播种机和控制器,使用下面的代码
$article = new \App\Entities\Article();
$article->title = 'Article Title';
$article->content = 'Article Content';
$model = new \App\Models\ArticleModel();
$model->save($article);
echo $model->getLastQuery();
两次结果都是一样的。echo
显示以下内容
INSERT INTO `articles` (`created_at`, `updated_at`) VALUES ('2019-10-09 02:25:56', '2019-10-09 02:25:56')
我不知道如何告诉我的模型(我认为这是造成不良行为的原因)考虑它的$allowedFields
来保存文章
你能看到问题吗?事实证明,在这种情况下,模型的save()
方法只接受类CodeIgniter\entity
的对象
好的做法是让文章
扩展该类:
namespace App\Entities;
class Article extends \CodeIgniter\Entitiy
{
}
这就解决了问题
$article = new \App\Entities\Article();
$article->title = 'Article Title';
$article->content = 'Article Content';
$model = new \App\Models\ArticleModel();
$model->save($article);
echo $model->getLastQuery();
INSERT INTO `articles` (`created_at`, `updated_at`) VALUES ('2019-10-09 02:25:56', '2019-10-09 02:25:56')
namespace App\Entities;
class Article extends \CodeIgniter\Entitiy
{
}