public function store() { // Update the bud record itself parent::store(); // Get the object's associated users $this->getUsers(); // Get the bud's users as stored in the DB $db_users = $this->fetchUsers(); $link_table = static::getTable('votes_bud')->name; // Add links to any users linked in object that are not in DB yet $db = static::connection(); $query = "\n INSERT INTO `{$link_table}` (user_id, bud_id)\n VALUES (:user_id, :bud_id);"; $stmt = $db->prepare($query); foreach ($this->_users as $user_id => $user) { // If relationship not in the DB, then add it if (!isset($db_users[$user_id])) { $sqlVars = [':user_id' => $user_id, ':bud_id' => $this->_id]; $stmt->execute($sqlVars); } } // Remove any links in DB that are no longer modeled in this object if ($db_users) { $db = static::connection(); $query = "\n DELETE FROM `{$link_table}`\n WHERE user_id = :user_id\n AND bud_id = :bud_id LIMIT 1"; $stmt = $db->prepare($query); foreach ($db_users as $user_id => $user) { if (!isset($this->_users[$user_id])) { $sqlVars = [':user_id' => $user_id, ':bud_id' => $this->_id]; $stmt->execute($sqlVars); } } } // Store function should always return the id of the object return $this->_id; }
/** * @see DatabaseInterface */ public function store() { // If this is being set as the default primary group, then any other group must be demoted to default group if ($this->is_default == GROUP_DEFAULT_PRIMARY) { $db = static::connection(); $query = "\n UPDATE `{$this->_table->name}`\n SET is_default = " . GROUP_DEFAULT . " WHERE is_default = " . GROUP_DEFAULT_PRIMARY . ";"; $stmt = $db->prepare($query); $stmt->execute(); } // Now store this group parent::store(); // Store function should always return the id of the object return $this->_id; }
public function store($force_create = false) { // Initialize timestamps for new Users. Should this be done here, or somewhere else? if (!isset($this->_id) || $force_create) { $this->sign_up_stamp = date("Y-m-d H:i:s"); $this->activation_token = UserLoader::generateActivationToken(); $this->last_activation_request = date("Y-m-d H:i:s"); } // Update the user record itself parent::store(); // Get the User object's current groups $this->getGroups(); // Get the User's groups as stored in the DB $db_groups = $this->fetchGroups(); $link_table = static::getTableGroupUser(); // Add any groups in object that are not in DB yet $db = static::connection(); $query = "\n INSERT INTO `{$link_table}` (user_id, group_id)\n VALUES (:user_id, :group_id);"; foreach ($this->_groups as $group_id => $group) { $stmt = $db->prepare($query); if (!isset($db_groups[$group_id])) { $sqlVars = [':group_id' => $group_id, ':user_id' => $this->_id]; $stmt->execute($sqlVars); } } // Remove any group links in DB that are no longer modeled in this object if ($db_groups) { $db = static::connection(); $query = "\n DELETE FROM `{$link_table}`\n WHERE group_id = :group_id\n AND user_id = :user_id LIMIT 1"; $stmt = $db->prepare($query); foreach ($db_groups as $group_id => $group) { if (!isset($this->_groups[$group_id])) { $sqlVars = [':group_id' => $group_id, ':user_id' => $this->_id]; $stmt->execute($sqlVars); } } } // Store function should always return the id of the object return $this->_id; }