1 回答
TA贡献1876条经验 获得超7个赞
我不完全确定你想用现有的 array_search 做什么,但我猜这是让领导检查工作的初步尝试?如果不是这样,请告诉我
您处于正确的一般轨道上,首先查询表以检查冲突,只是不太正确的执行。
你需要做的是
如果您要添加领导者,请检查该团队是否已经有领导者
如果是这样,请以某种方式处理它
否则继续插入
尝试类似下面的内容。输入应该是一个数组,其键对应于数据库列。例如:
$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();
}
}
值得一提的是你的代码到目前为止似乎有一点不匹配的editing和adding尚未在此解决。此功能假定您仅添加新成员,而不是编辑现有成员。对于这种情况,您将需要更多东西。
- 1 回答
- 0 关注
- 148 浏览
添加回答
举报