使用文本文件中的cakephp比较日期

时间:2014-01-24 01:20:26

标签: sql date cakephp

我有这个警告,在我的数据库中只有日期时间和id插入

Warning (512): SQL Error: 1048: Column 'employee_id' cannot be null [CORE\cake\libs\model\datasources\dbo_source.php, line 684]

我希望将日期作为日期时间输入和输出保存,条件是如果持有时间的id将比较日期,如果更大的时间将是其日期时间和另一个将是日期时间。我正在使用像这样的文本文件

No  TMNo    EnNo    Name    GMNo    Mode    DateTime
0   1   1       1   30  2013-11-25 08:00:00
1   1   1       1   30  2013-11-25 17:00:00
2   1   27      1   30  2013-11-25 09:02:34
3   1   27      1   30  2013-11-25 18:04:10
4   1   28      1   30  2013-11-25 10:02:34
5   1   28      1   30  2013-11-25 13:04:10

我将这个上传到程序中。谢谢,我希望能帮到我。

 function logs()
    {   

    App::import('Helper', 'Time');
    $time = new TimeHelper();

    /***Start Read Text File***/
    if(!empty($this->data)){
        $file = fopen($this->data['BiometricLog']['log_file']['tmp_name'], "r");
        $members = array();

        while (!feof($file)) {
           $members[] = fgets($file);
        }

        fclose($file);
        foreach($members as $indexKey=>$value){
            $line_string = preg_replace('/\t/', '||', $value);
            $column_string = explode("||", $line_string);
            //debug($column_string);
            if($indexKey > 0){
                $data_needed['Employee']['employee_id'] =  (isset($column_string['2']))?preg_replace('/(\0|\r\n|\r|\n)+/', '', $column_string['2']):'';
                $data_needed['Employee']['datetime_in'] =  (isset($column_string['6']))?preg_replace('/(\0|\r\n|\r|\n)+/', '', $column_string['6']):'';
                $DateTime_in = $data_needed['Employee']['datetime_in'];
               //set time string into numeric
                $DateTime_in=strtotime($DateTime_in);

                $employee_pid = $data_needed['Employee']['employee_id'];

                $DateTime_in_1='';

                $DateTime_out='';

                //GET THE DATE ONLY
                 for ($i=0; $i<11; $i++){
                        $DateTime_in_1 = $DateTime_in_1.''.$DateTime_in[$i];

                    if($employee_pid == $employee_pid[$i]){

                          if(strtotime($DateTime_in) > strtotime($DateTime_in[$i])){

                            $DateTime_out = $DateTime_in_1.''.$DateTime_in[$i];

                          }
                    }



        }

                debug( $DateTime_out);

                $this->loadModel('BiometricLog');
                $this->BiometricLog->create();
                if($this->BiometricLog->save($data_needed['Employee'])){

                }else{
                    debug("Not Save");
                }

                /*$this->BiometricLog->save($this->$data_needed['Employee']);
                $this->redirect(array('action'=>'index'));*/
            }
        }
    }

    }

2 个答案:

答案 0 :(得分:1)

首先检查一下你有什么:

$employee_pid = $data_needed['Employee']['employee_id'];
debug($employee_pid);die;

我认为你的话在这里:

$data_needed['Employee']['employee_id'] =  (isset($column_string['2']))?preg_replace('/(\0|\r\n|\r|\n)+/', '', $column_string['2']):'';

你可能没有$ column_string ['2'],它会转到:''。尝试进行测试以更改''with'999'以查看它是否正常,如此

$data_needed['Employee']['employee_id'] =  (isset($column_string['2']))?preg_replace('/(\0|\r\n|\r|\n)+/', '', $column_string['2']):'999';

答案 1 :(得分:0)

尝试替换它:

if($this->BiometricLog->save($data_needed['Employee'])){

用这个:

if($this->BiometricLog->save(
    array(
        $this->BiometricLog->alias => $data_needed['Employee']
    )
)){