完整性约束违规1048 PHP MySQL

时间:2015-10-14 14:13:17

标签: php mysql cakephp referential-integrity

我有一个基于CakePHP 2.7框架构建的应用程序,当我去加载登录页面时,它给出了以下错误:Integrity constraint violation: 1048 Column 'userid' cannot be null。我知道我必须在我的用户模型中将其设置为不为null但是我会更改以确保它只是''而不是NULL?我在下面列出了我的用户模型。任何帮助将不胜感激!

用户模型

<?php

class User extends AppModel {

    var $name = 'User';
    var $primaryKey = 'userid';
    var $displayField = 'username';

  var $hasAndBelongsToMany = array(
    'Team' => array(
      'className' => 'Team',
      'joinTable' => 'teamAssignments',
      'foreignKey' => 'userid',
      'associationForeignKey' => 'teamid',
      'unique' => 'true'
    )
  );

  var $hasMany = array(
    'Assessment' => array(
      'className' => 'Assessment',
      'foreignKey' => 'student_id'
    ),
    'Assessment' => array(
      'className' => 'Assessment',
      'foreignKey' => 'teacher_id'
    ),
    'AssessmentComment' => array(
      'className' => 'AssessmentComment',
      'foreignKey' => 'student_id'
    ),
    'AssessmentComment' => array(
      'className' => 'AssessmentComment',
      'foreignKey' => 'teacher_id'
    ),
    'Tour' => array(
      'className' => 'Tour',
      'foreignKey' => 'userid'
    ),
    'Wiki' => array(
      'className' => 'Wiki',
      'foreignKey' => 'user_id'
    )
  );

  function archive($id) {
    if (!$id) {
      return false;
    }else{
      $this->recursive = -1;
      $user = $this->read(null, $id);
      $user['User']['active'] = false;
      $this->save($user);
      return true;
    }
  }

  function getInfo($id, $team_id, $token) {
    $user = $this->read(null, $id);
    // Make sure the token is valid
    $this->Tour->recursive = 2;
    $tour = $this->Tour->findByToken($token);
    if ($team_id == '_definst_') {
      // This is the global chat application or some other exception
      $user = $this->read(null, $id);
      $info['userid'] = $user['User']['userid'];
      $info['firstName'] = $user['User']['firstName'];
      $info['lastName'] = $user['User']['lastName'];
      $info['emailAddress'] = $user['User']['emailAddress'];
      $info['logincount'] = $user['User']['logincount'];
      $info['lastlogin'] = $user['User']['lastlogin'];
      $info['mentor'] = $user['User']['mentor'];
      return http_build_query($info);
    }else if ($tour) {
      // Make sure this user has permission to look at this team
      $on_team = false;
      if ($tour['User']['mentor'] == 1) {
        $on_team = true;
      }else{
        foreach ($tour['User']['Team'] as $team) {
          if ($team['id'] == $team_id) {
            $on_team = true;
            break;
          }
        }
      }
      if ($on_team) {
        // Make sure the requested user is on the requested team
        $user = $this->read(null, $id);
        $on_team = false;
        if ($tour['User']['mentor'] == 1) {
          $on_team = true;
        }else{
          foreach ($user['Team'] as $team) {
            if ($team['id'] == $team_id) {
              $on_team = true;
              break;
            }
          }
        }
        if ($on_team) {
          $team = $this->Team->read(null, $team_id);
          $info['userid'] = $user['User']['userid'];
          $info['username'] = $user['User']['username'];
          $info['firstName'] = $user['User']['firstName'];
          $info['lastName'] = $user['User']['lastName'];
          $info['emailAddress'] = $user['User']['emailAddress'];
          $info['teacher'] = $user['User']['teacher'];
          $info['logincount'] = $user['User']['logincount'];
          $info['lastlogin'] = $user['User']['lastlogin'];
          $info['mentor'] = $user['User']['mentor'];
          $info['teamid'] = $team['Team']['id'];
          $info['teamName'] = $team['Team']['teamName'];
          $info['instancename'] = $team['Team']['instanceName'];
          return http_build_query($info);
        }
      }
      return '';
    }
  }

  function validate($token, $team_id) {
    $this->Tour->recursive = 2;
    $tour = $this->Tour->findByToken($token);
    if ($team_id == '_definst_') {
      // This is the global chat application or some other exception
      return $tour['Tour']['userid'];
    }else if ($tour) {
      // Make sure this user is on this team or is a mentor
      $on_team = false;
      if ($tour['User']['mentor'] == 1) {
        $on_team = true;
      }else{
        foreach ($tour['User']['Team'] as $team) {
          if ($team['id'] == $team_id) {
            $on_team = true;
            break;
          }
        }
      }
      if ($on_team) {
        return $tour['Tour']['userid'];
      }
    }
    return '';
  }

  function wsKillSession($token) {
    // Delete any tours
    $this->Tour->recursive = -1;
    $tour = $this->Tour->findByToken($token);
    $this->Tour->deleteAll(array('Tour.userid' => $tour['Tour']['userid']));
    return 1;
  }

  function getUsers($user_id, $token, $team_name) {
    $user = $this->read(null, $user_id);
    $team = $this->Team->find('first', array('conditions' => array('Team.teamName' => $team_name)));
    $users = array();
    foreach ($team['User'] as $user) {
      $users[] = array('userid' => $user['userid'], 'username' => $user['username'], 'firstName' => $user['firstName'], 'lastName' => $user['lastName'], 'emailAddress' => $user['emailAddress'], 'teacher' => $user['teacher'], 'logincount' => $user['logincount'], 'lastlogin' => $user['lastlogin'], 'mentor' => $user['mentor'], 'teamid' => $team['Team']['id'], 'teamName' => $team['Team']['teamName'], 'instanceName' => $team['Team']['instanceName']);
    }
    return serialize($users);
  }

}

?>

0 个答案:

没有答案