我有一个网络服务来连接应用程序和网站。它应该只同步已更改的数据。
这是函数的代码:
public function iphoneexportAction()
{
echo "Start of IphoneExport<br/>";
//Basic configuration
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$this->_response->setHeader('Content-Type', 'application/json');
$data = $this->_request->getParam('udid');
echo "UDID:".$data;
if (!$data || $data == '')
{//no data received
echo json_encode(array('status' => 7, 'message' => "No UDID value received"));
$logModel = new Application_Model_WsLog();
$logDate = array(
'call_type' => $this->getRequest()->getActionName(),
'output' => json_encode(array('status' => 7, 'message' => "No UDID value received")),
//'input' => print_r($data,true),
'input' => print_r($this->_request,true),);
$logModel->insert($logDate);
return true;
}
//Taking the user info from the database
$userModel = new Application_Model_User();
$user = $userModel->findByEmail($data);
$userLastSync = $user[0]['sync_ts'];
echo "<br/>Date from database:".$userLastSync."<br>";
if (!isset($user[0]))
{//No user exists with that email
echo json_encode(array('status' => 1, 'message' => "user not found."));
$logModel = new Application_Model_WsLog();
$logDate = array(
'call_type' => $this->getRequest()->getActionName(), 'output' => json_encode(array('status' => 1, 'message' => "user not found.")),
//'input' => print_r($data,true),
'input' => print_r($this->_request,true),
);
$logModel->insert($logDate);
return true;
}
if ($user[0]['user_type'] != USERTYPE_BRIDE)
{//User is a bride
echo json_encode(array('status' => 1, 'message' => "User type not bride"));
$logModel = new Application_Model_WsLog();
$logDate = array(
'call_type' => $this->getRequest()->getActionName(), 'output' => json_encode(array('status' => 1, 'message' => "User type not bride")),
//'input' => print_r($data,true),
'input' => print_r($this->_request,true),
);
$logModel->insert($logDate);
return true;
}
//Adds user data to the return values
$return['udid'] = $data;
/*
* SETTINGS
*/
$brideModel = new Application_Model_Bride();
$bride = $brideModel->findComplete($user[0]['user_id']);
if($bride[0]['bride_changeTs'] > $userLastSync)
{
if(is_null($bride[0]['bride_location_id']))
$settings['weddingRegion'] = $bride[0]['bride_region_id'];
else
$settings['weddingLocation'] = $bride[0]['bride_location_id'];
$settings['weddingDate'] = is_null(strtotime($bride[0]['bride_weddingDate'])) ? '' : strtotime($bride[0]['bride_weddingDate']);
$settings['brideName'] = is_null($bride[0]['user_name']) ? '' : $bride[0]['user_name'];
$settings['bride_groom'] = is_null($bride[0]['bride_groom']) ? '' : $bride[0]['bride_groom'];
$settings['email'] = is_null($bride[0]['user_email']) ? '' : $bride[0]['user_email'];
$settings['vendorsCanEmail'] = is_null($bride[0]['bride_vendorsCanEmail']) ? '' : $bride[0]['bride_vendorsCanEmail'];
$settings['budget'] = is_null($bride[0]['bride_weddingBudget']) ? 0 : $bride[0]['bride_weddingBudget'] / 100;
$return['settings'] = $settings;
}
$mappingModel = new Application_Model_Mapping();
/*
* TASKS
*/
echo "\nStart of tasks";
$taskModel = new Application_Model_TaskList();
$tasks = $taskModel->findAllMe($bride[0]['bride_id']);
$tasktotal = count($tasks);
$tasknum = 0;
$taskSend = array();
foreach ($tasks as $task)
{
echo "<br />Task-".$tasknum."---->";
echo $task['taskList_changeTs'].' < '.$userLastSync.'';
if($task['taskList_changeTs'] > $userLastSync)
{
echo "--UPDATE";
$created_on_app = 0;
/*$mapping = $mappingModel->fetchRow(
$mappingModel->select()
->where('mapping_siteID = :siteID')
->where('mapping_type = :type')
->where('mapping_userID = :userID')
->bind(array(
'siteID' => $task['taskList_id'],
'type' => MAP_TASK,
'userID' => $user[0]['user_id']
))
);*/
if(empty($mapping))
$task['taskList_id'] = $task['taskList_id'];
else
{
$task['taskList_id'] = $mapping->mapping_appID;
$created_on_app = 1;
}
$taskActual = array(
'id' => is_null($task['taskList_id']) ? '' : $task['taskList_id'],
'name' => is_null($task['taskList_name']) ? '' : $task['taskList_name'],
'categoryId' => is_null($task['taskList_taskCategory_id']) ? '' : $task['taskList_taskCategory_id']
);
switch ($task['taskList_status'])
{
case TASKLISTSTATUS_COMPLETED:
$taskActual['status'] = 1;
break;
case TASKLISTSTATUS_TODO:
$taskActual['status'] = 0;
break;
case TASKLISTSTATUS_DELETED:
$taskActual['status'] = 2;
break;
case NULL:
$taskActual['status'] = 0;
break;
}
//$taskActual['dueDate'] = is_null($task['taskList_date']) ? '' : $task['taskList_date'];
$taskActual['dueDate'] = $task['taskList_date'];
$taskActual['notes'] = $task['taskList_comments'];
$taskActual['created_on_app'] = $created_on_app;
$taskSend[] = $taskActual;
//EOF Update if
}
$tasknum++;
//EOF foreach
}
if( ! empty($taskSend))
{
$return['tasks'] = $taskSend;
$taskEnded=true;
}
echo"<br />";
/*
* UPDATE PART
*/
echo json_encode($return);
//echo count($return);
//if(count($return) > 1)
echo "Task total:".$tasktotal." Tasknum:".$tasknum."<br/>";
if ($tasktotal == $tasknum&&$taskEnded)
{
$sync_ts_time = time();
$sync_ts = date('Y-m-d H:i:s', $sync_ts_time);
$userModel->update(array('sync_ts'=> $sync_ts), array('user_id'=>$user[0]['user_id']));
echo "UPDATED in the database<br/>";
echo $sync_ts;
}
$logModel = new Application_Model_WsLog();
$logDate = array(
'call_type' => $this->getRequest()->getActionName(),
'output' => json_encode($return),
'input' => print_r($data,true),
);
$logModel->insert($logDate);
return true;
}
在函数的末尾有一行是
$userModel->update(array('sync_ts'=> $sync_ts), array('user_id'=>$user[0]['user_id']));
这条线导致它不起作用。因此,如果我评论它,它会遍历每项任务并比较时间,如果数据库中的时间较新,它会将 json 数据创建回应用程序。问题是当我取消注释时,它甚至在开始 foreach 之前就更新了数据库时间戳。
这是评论时的输出
Start of IphoneExport
UDID:xxxxx@gmail.com
Date from database:2013-09-16 10:49:10
Start of tasks
Task-0---->2013-09-10 13:28:21 < 2013-09-16 10:49:10
Task-80---->2013-09-10 13:28:21 < 2013-09-16 10:49:10
Task-81---->2013-09-18 11:39:15 < 2013-09-16 10:49:10--UPDATE
Task-82---->2013-09-20 14:22:58 < 2013-09-16 10:49:10--UPDATE
{"udid":"xxxxx@gmail.com","settings":{bride settings},"tasks": [{"id":"22635",task data}, {"id":"22636",task data}]}
Task total:83 Tasknum:83
UPDATE in the database
2013-09-23 10:51:00
您可以看到它说 Data from database 16 September 并且只更新了两个任务。在数据库中说 UPDATE 的末尾是注释行,它将最后一次同步更新到数据库。
Start of IphoneExport
UDID:xxxxx@gmail.com
Date from database:2013-09-23 11:52:32
Start of tasks
Task-0---->2013-09-10 13:28:21 < 2013-09-23 11:52:32
Task-81---->2013-09-18 11:39:15 < 2013-09-23 11:52:32
Task-82---->2013-09-20 14:22:58 < 2013-09-23 11:52:32
{"udid":"xxxx@gmail.com"}
Task total:83 Tasknum:83
您可以看到“数据库中的日期”是当前日期,但这是不可能的,因为它是在开始时从数据库中获取的,我手动将数据库中的日期更改为 9 月 16 日。
因此,它甚至在没有到达更新语句的情况下就更改了数据库,因此对于新日期,应用程序中没有任何内容需要更新。
//Taking the user info from the database
$userModel = new Application_Model_User();
$user = $userModel->findByEmail($data);
$userLastSync = $user[0]['sync_ts'];
echo "<br/>Date from database:".$userLastSync."<br>";
我完全困惑,我找不到为什么会这样