i have team member table and team table. In team member table have , three columns are there teamid, staff_id and stafftype(leader or technician). Only one leader (staff_type column value) will comes in one team. So when i insert data to team member table i need to check whether any leader is there in same team.
How to show an error message that "already have leader in this team"?Team member table looks like,
team_id staff_id stafftype
1 2 leader //see here already have leader for team_id 1
2 1 other
3 8 other
1 5 Technician
2 3 Other
1 4 Leader //This should not come. becoz already teamid-1 have Leader
When trying to save from frond end, need to show error message ie;"already have leader in this team"
MODAL
public function addteam_memb($datas) {
$this->db->select('*');
$this->db->from('team_members');
$querySS = $this->db->get()->result();
if(array_search('1', array_column($querySS, 'team_id')) !== false) {
return 1;
}
else {
$insert_id = 0;
if ( ! empty($datas)) {
$this->db->insert('team_members', $datas);
$insert_id = $this->db->insert_id();
}
}
}
CONTROLLER
public function editteammember() {
$getaddstafftype = $this->input->post( 'getaddstafftype' );
$getaddstaffname = $this->input->post( 'getaddstaffname' );
$getteamid = $this->input->post('getteamid');
$getstatus = $this->input->post('getstatus');
//if ( ! empty($getaddstaffname) )
if ( ! empty($getaddstaffname) && ! empty($getaddstafftype) ) {
foreach ($getaddstaffname as $key => $value ){
$data['Staff_id'] = $value;
$data['Staff_type'] = $getaddstafftype[$key];
$data['team_id'] = $getteamid[$key];
$data['status'] = "active";
$value = $this->mastertable_model->addteam_memb($data);
}
if($value == 1) {
echo "Already have leader in this team";
} else {
//$this->load->view('team_memb_creation');
}
}
}
I'm not entirely sure what you're trying to do with the existing array_search, but I'm guessing that it's an initial attempt at getting the leader check working? If that's not the case let me know
You're on the right general track, querying the table first to check for conflicts, just not quite the right execution.
What you need to do is
Try something like the below. The input should be an array with keys corresponding to database columns. EG:
$datas = [
'team_id' => 1,
'staff_id' => 3,
'stafftype' => 'leader'
]
// Its a good idea to use constants for things like this
const STAFFTYPE_LEADER = 'leader';
/**
* Add a team member to the database
*
* Input array must be an array representing a team member to be added,
* with key names that match database column names.
*
* @param array $datas
* @throws Exception If adding a leader when one already exists
*/
public function addteam_memb($datas) {
// Lower case conversion just for consistency.
$staffType = strtolower($datas['Staff_type']);
// Only run the query if we're trying to add a team lead
// Otherwise there's no need to worry
if ($staffType === self::STAFFTYPE_LEADER) {
$this->db->select('*');
$this->db->from('team_members');
$this->db->where('team_id', $datas['team_id'])
$this->db->where('stafftype', self::STAFFTYPE_LEADER)
if ($this->db->count_all_results() >= 1) {
// Team leader already exists, handle however you would like
// I would typically recommend an exception, but thats up to you!
throw new Exception('Team leader exists');
}
// Reset query before moving on to the insert
$this->db->reset_query();
}
// Either we're not adding a leader, or there is no current leader
// so go ahead with the insert
$insert_id = 0;
if ( ! empty($datas)) {
$this->db->insert('team_members', $datas);
$insert_id = $this->db->insert_id();
}
}
It's worth noting that your code so far seems to have a bit of a mismatch between editing
and adding
that hasn't been addressed here. This function assumes you're only adding new members, not editing existing ones. you'll need something more for that case.