简体   繁体   中英

Comparing date using cakephp from text file

I have this warning and in my database only the date time in and id insert

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

and I want to save the date as the date time in and out according to the condition that if the the id holding the time will compare the date and if the larger time will be its date the time in and the other one will be the date time out.I'm using text file like this

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

and I upload this in the program.Thank you I hope can help me.

 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'));*/
            }
        }
    }

    }

first check what you have here:

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

I think your line here:

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

You probably don't have a $column_string['2'] and it goes to : ''. Try for testing purposes to change the '' with '999' to see if it works OK, like this

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

Try replacing this:

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

with this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM