/** * Updates an entry in the db from a status * @param Entity $entity the status that should be created * @return Entity|null * @throws \InvalidArgumentException if entity has no id */ public function update(Entity $entity) { // if entity wasn't changed it makes no sense to run a db query $properties = $entity->getUpdatedFields(); if (count($properties) === 0) { return $entity; } // entity needs an id $fileId = $entity->getFileId(); if ($fileId === null) { throw new \InvalidArgumentException('Entity which should be updated has no fileId'); } // get updated fields to save, fields have to be set using a setter to // be saved // don't update the fileId field unset($properties['fileId']); $columns = ''; $params = array(); // build the fields $i = 0; foreach ($properties as $property => $updated) { $column = $entity->propertyToColumn($property); $getter = 'get' . ucfirst($property); $columns .= '`' . $column . '` = ?'; // only append colon if there are more entries if ($i < count($properties) - 1) { $columns .= ','; } array_push($params, $entity->{$getter}()); $i++; } $sql = 'UPDATE `' . $this->tableName . '` SET ' . $columns . ' WHERE `fileid` = ?'; array_push($params, $fileId); $this->execute($sql, $params); }
public function insertUnique(Entity $entity) { // First check $entity is already in DB $sql = <<<SQL \t\t\tSELECT \t\t\t\t`user` \t\t\tFROM \t\t\t\t`*PREFIX*chat_och_users_in_conversation` \t\t\tWHERE \t\t\t\t`user` = ? \t\t\tAND \t\t\t\t`conversation_id` = ? SQL; try { $result = $this->findOneQuery($sql, array($entity->getUser(), $entity->getConversationId())); // The user already joined the conv -> nothing to do } catch (\Exception $exception) { // The user isn't in this conversation -> add it $sql = <<<SQL \t\t\tINSERT \t\t\tINTO `*PREFIX*chat_och_users_in_conversation` \t\t\t( \t\t\t\t`user`, \t\t\t\t`conversation_id`, \t\t\t\t`joined` \t\t\t) VALUES ( \t\t\t\t?, \t\t\t\t?, \t\t\t\t? \t\t\t) SQL; $this->execute($sql, array($entity->getUser(), $entity->getConversationId(), $entity->getJoined())); } }
/** * Transform a property to a database column name * * @param string $property the name of the property * @return string the column name */ public function propertyToColumn($property) { if ($property === 'name') { return 'category'; } elseif ($property === 'owner') { return 'uid'; } else { return parent::propertyToColumn($property); } }
public function setBody($body) { // FIXME: this should not happen if the target="_blank" is already // on the link parent::setBody(str_replace('<a', '<a target="_blank" rel="noreferrer"', $body)); }
public function setLink($url) { $url = trim($url); if (strpos($url, 'http') === 0) { parent::setLink($url); } }
/** * Get the (encrypted) login password * * @return string */ public function getPassword() { return parent::getPassword(); }
/** * Get the timestamp of the last password check * * @param int $time */ public function setLastCheck($time) { return parent::setLastCheck($time); }
/** * Transform a database column name to a property * @param string $columnName the name of the column * @return string the property name */ public function columnToProperty($columnName) { if ($columnName === 'fileid') { $property = 'fileId'; } else { $property = parent::columnToProperty($columnName); } return $property; }
/** * @param string $name * @param mixed $args * @return $this * @throws \BadFunctionCallException */ protected function setter($name, $args) { if (array_key_exists($name, $this->_advancedFieldTypes)) { $type = $this->_advancedFieldTypes[$name]; if ($args[0] !== null && !$args[0] instanceof $type) { throw new \BadFunctionCallException('Argument 1 must be an object of class ' . $type); } } parent::setter($name, $args); return $this; }