1

我使用苗条的框架并使用Illumination/database

我想插入不重复 function_number 但如果不同的 product_id function_number 可以重复的数据,例如

在此处输入图像描述

这是我的模式

$app->post('/create_function', function($request, $reponse, $args) {
   $query = $this->db->table('functions')->insert([
            'project_id' => $request->getParam('project_id'),
            'function_number' => $request->getParam('function_number'), 
            'function_text' => $request->getParam('function_text'),
            'created' => date('Y-m-d')
         ]);
   if($query) {
     echo "Function was created";
   }
});
4

1 回答 1

2

根据您的要求,您希望这对夫妇(project_id, function_number)是独一无二的。为此,您需要按如下方式查询您的数据库:

//Check for duplicate  
$exists = $this->db->table('functions')->where('project_id', '=', $request->getParam('project_id'))->where('function_numbe', '=', $request->getParam('function_number'))->exists();

if(!$exists) {
    //Your insert code here 
}
于 2016-07-02T05:57:28.117 回答