2019-11-22 11:40:20 1460次浏览 1条回答 0 悬赏 10 金钱

我想给数据库的部分表做一个统一的查删增改的接口,用了一个Api类继承的ActiveRecord,根据post数据动态确定表名,但是遇到了添加不进数据但是又获取不到errors的问题,请问能帮忙看看吗?

$this->post = json_decode(file_get_contents('php://input'), true) ?? json_decode('{"_csrf":"csrf","table":"question","tableOp":"questionManage","operate":"add","data":"{\"type\":\"不定项选择题\",\"title\":\"asdf\",\"image\":[],\"selectionList\":\"\",\"selection0\":\"\",\"selection1\":\"\",\"selection2\":\"\",\"selection3\":\"\",\"selection4\":\"\",\"selection5\":\"\",\"selection6\":\"\",\"selection7\":\"\",\"answer\":\"qerqe\",\"answerOrder\":true,\"ratio\":0}"}', true);//测试用数据
//0.table
$this->table = $this->post['table'];
$this->api = new Api();
$this->api::$tableName = $this->table;//动态确定表名

//1.data
$data = json_decode($this->post['data'], true);
$serverData = $this->translateDataB2S($data);//准备数据

//2.api
//$this->api = new Api();
//$this->api::$tableName = $table;

//3.operate and response
$this->api->attributes = $serverData;
if(!$this->api->save())
{
var_dump($this->api->getErrors());//问题就在这里,插入不了数据库,也不知都是什么原因,这一行打印出来是空数组
var_dump($this->api->save());//bool(false)
}
//echo json_encode($this->translateDataS2B($this->api->attributes));
补充于 2019-11-22 11:41

为什么我总是不能以代码格式提交代码

补充于 2019-11-22 12:32

嗯我找到原因了

我的一个字段 answerOrder 设置的是 bit 类型,用 Gii 生成的 rules 里面有一条:
[['answerOrder'], 'boolean'],
而我在进行块赋值的时候给 answerOrder 不管赋值为 1,0,true,false 都会造成插入失败,把数据表中 bit 类型改为 int,或者把 rules 里面上述语句删掉就可以了

但是仍然有问题不明白:

  1. bit类型应该怎么赋值?
  2. 为什么 getErrors()不提示错误信息?
  • 回答于 2019-11-23 11:08 举报
    //只能输入数字0或1
    ['selected', 'boolean'],
    //上句等效于下句:
    //只能在文本框'selected' 中输入数字0或1,输入字符串'false'或'true'无效('strict' => false)
    ['selected', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => false],
    
您需要登录后才可以回答。登录 | 立即注册
捣捣爸
见习主管

捣捣爸

注册时间:2019-05-23
最后登录:2022-04-26
在线时长:9小时29分
  • 粉丝0
  • 金钱105
  • 威望10
  • 积分295

热门问题