/** * Find object by primary key using raw SQL to go fast. * Bypass doSelect() and the object formatter by using generated code. * * @param mixed $key Primary key to use for the query * @param PropelPDO $con A connection object * * @return SystemLog A model object, or null if the key is not found * @throws PropelException */ protected function findPkSimple($key, $con) { $sql = 'SELECT `id`, `user_id`, `index`, `entity`, `service`, `code`, `message`, `data` FROM `system_log` WHERE `id` = :p0'; try { $stmt = $con->prepare($sql); $stmt->bindValue(':p0', $key, PDO::PARAM_INT); $stmt->execute(); } catch (Exception $e) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), $e); } $obj = null; if ($row = $stmt->fetch(PDO::FETCH_NUM)) { $obj = new SystemLog(); $obj->hydrate($row); SystemLogPeer::addInstanceToPool($obj, (string) $key); } $stmt->closeCursor(); return $obj; }
/** * Populates the object using an array. * * This is particularly useful when populating an object from one of the * request arrays (e.g. $_POST). This method goes through the column * names, checking to see whether a matching key exists in populated * array. If so the setByName() method is called for that column. * * You can specify the key type of the array by additionally passing one * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM. * The default key type is the column's BasePeer::TYPE_PHPNAME * * @param array $arr An array to populate the object from. * @param string $keyType The type of keys the array uses. * @return void */ public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME) { $keys = SystemLogPeer::getFieldNames($keyType); if (array_key_exists($keys[0], $arr)) { $this->setId($arr[$keys[0]]); } if (array_key_exists($keys[1], $arr)) { $this->setUserId($arr[$keys[1]]); } if (array_key_exists($keys[2], $arr)) { $this->setIndex($arr[$keys[2]]); } if (array_key_exists($keys[3], $arr)) { $this->setEntity($arr[$keys[3]]); } if (array_key_exists($keys[4], $arr)) { $this->setService($arr[$keys[4]]); } if (array_key_exists($keys[5], $arr)) { $this->setCode($arr[$keys[5]]); } if (array_key_exists($keys[6], $arr)) { $this->setMessage($arr[$keys[6]]); } if (array_key_exists($keys[7], $arr)) { $this->setData($arr[$keys[7]]); } }
/** * Retrieve multiple objects by pkey. * * @param array $pks List of primary keys * @param PropelPDO $con the connection to use * @return SystemLog[] * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function retrieveByPKs($pks, PropelPDO $con = null) { if ($con === null) { $con = Propel::getConnection(SystemLogPeer::DATABASE_NAME, Propel::CONNECTION_READ); } $objs = null; if (empty($pks)) { $objs = array(); } else { $criteria = new Criteria(SystemLogPeer::DATABASE_NAME); $criteria->add(SystemLogPeer::ID, $pks, Criteria::IN); $objs = SystemLogPeer::doSelect($criteria, $con); } return $objs; }
/** * Method to invalidate the instance pool of all tables related to user * by a foreign key with ON DELETE CASCADE */ public static function clearRelatedInstancePool() { // Invalidate objects in ClockingPeer instance pool, // since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule. ClockingPeer::clearInstancePool(); // Invalidate objects in ClockingPeer instance pool, // since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule. ClockingPeer::clearInstancePool(); // Invalidate objects in PropertyValuePeer instance pool, // since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule. PropertyValuePeer::clearInstancePool(); // Invalidate objects in SystemLogPeer instance pool, // since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule. SystemLogPeer::clearInstancePool(); // Invalidate objects in TransactionPeer instance pool, // since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule. TransactionPeer::clearInstancePool(); // Invalidate objects in TransactionPeer instance pool, // since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule. TransactionPeer::clearInstancePool(); }
/** * Creates or updates a clocking. * * @param int $id * @param array $data * @return int The clocking ID */ public function do_update($id, $data) { $con = Propel::getConnection(); if (!$con->beginTransaction()) { throw new Exception('Could not start transaction.'); } $clocking = null; try { $authUser = $this->requireUser(); // Validate input data $validator = new KickstartValidator(); $locale = Localizer::getInstance(); // Cut off seconds to get time in full minutes if (isset($data['Start']) and is_numeric($data['Start'])) { $data['Start'] -= date('s', $data['Start']); } if (isset($data['End']) and is_numeric($data['End'])) { $data['End'] -= date('s', $data['End']); } $warnings = $validator->filterErrors($data, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { return array('result' => false, 'warnings' => $warnings); } if ((string) $id === '') { $event = 'create'; $clocking = new Clocking(); } else { $event = 'update'; $clocking = $this->getClockingById($id, $con); if ($clocking->getBooked() or $clocking->getFrozen()) { throw new Exception('Cannot change clocking entry #' . $id . ' because it already has bookings or is locked for booking.'); } } $isAdmin = $authUser->getIsAdmin(); $allowedColumns = array('TypeId' => true, 'Start' => true, 'End' => true, 'Breaktime' => true, 'Comment' => true); if ($isAdmin) { $allowedColumns['ApprovalStatus'] = true; } $clocking->fromArray(array_intersect_key($data, array('UserId' => true) + $allowedColumns)); $clockingUser = $clocking->getUserRelatedByUserId($con); $clockingUserId = $clocking->getUserId(); $authUserAccountId = $authUser->getAccountId(); // Check if authenticated user may access clocking's user if ($clockingUser === null or (string) $clockingUser->getAccountId() !== (string) $authUserAccountId or !$isAdmin and $clockingUser !== $authUser) { throw new Exception('Invalid user #' . $clockingUserId . ' specified for clocking or no permission to access that user\'s data.'); } $type = $clocking->getClockingType($con); if ($type === null) { throw new Exception('Clocking #' . $id . ' has no clocking type assigned.'); } $account = $authUser->getAccount($con); if ($account === null) { throw new Exception('Could not load account of user #' . $authUser->getId() . ' "' . $authUser->getFQN($con) . '".'); } // Check hard time limit for non-admin users if (!$isAdmin) { $this->validateTimeLimits($account, $authUser, $clocking, $con); } $isNew = $clocking->isNew(); // Save first to obtain an ID which may be referenced by a plugin $clocking->save($con); $clockingData = EntityArray::from($clocking, $con) + array('IsNew' => $isNew, 'Type' => EntityArray::from($type, $con)); if (!$isAdmin and ($type->getApprovalRequired() or $this->pastGraceTimeExceeded($type, min((int) $clocking->getStart('U'), (int) $clocking->getEnd('U'))))) { $clocking->setApprovalStatus(ClockingPeer::APPROVAL_STATUS_REQUIRED); } $clocking->fromArray(array_intersect_key(PluginPeer::fireEvent($clockingUser, 'clocking', $event, $clockingData, $con), $allowedColumns)); $type = $clocking->getClockingType($con); // Plugins may have changed this if ($type === null or (string) $type->getAccountId() !== (string) $authUserAccountId) { throw new Exception('Clocking #' . $id . ' has an invalid or unknown clocking type #' . $clocking->getTypeId() . ' assigned.'); } $start = (int) $clocking->getStart('U'); $end = (int) $clocking->getEnd('U'); if ($start > $end) { throw new APIException(self::ERROR_INTERVAL, 'Start time (' . $clocking->getStart('Y-m-d H:i:s') . ') must be before end time (' . $clocking->getEnd('Y-m-d H:i:s') . ').', array('start' => $start, 'end' => $end)); } elseif ($type->getWholeDay()) { // Set time of day for start and end to 00:00:00 $clocking->setStart(strtotime(date('Y-m-d 00:00:00', $start))); $clocking->setEnd(strtotime(date('Y-m-d 00:00:00', $end))); // Set break time to 0 $clocking->setBreaktime(0); } elseif ($start === $end) { // Create an open clocking entry (i.e. sign on for work). // Fail if there are other open entries. if (($openClocking = $this->getOpenClocking($authUser, $clockingUser, $clocking, $con)) !== null) { $openComment = $openClocking->getComment(); throw new APIException(self::ERROR_OPEN, 'Clocking #' . $openClocking->getId() . ((string) $openComment === '' ? '' : ' "' . $openComment . '"') . ' from ' . $openClocking->getStart('r') . ' to ' . $openClocking->getEnd('r') . ' is already open. Please close that entry first.' . $openClocking->getId() . ' ' . $clocking->getId(), $openClocking); } } elseif ($clocking->getTime() < $clocking->getBreaktime()) { throw new APIException(self::ERROR_BREAK, 'Break (' . $clocking->getBreaktime() / 60 . ' minutes) must be less than the specified work time (' . $clocking->getTime() . ' = ' . $clocking->getStart('Y-m-d H:i:s') . ' - ' . $clocking->getEnd('Y-m-d H:i:s') . ').'); } $futureGraceTime = $type->getFutureGraceTime(); if ($futureGraceTime !== null and $end > time() + $futureGraceTime) { throw new APIException(self::ERROR_FUTURE, 'Clocking type "' . $type->getIdentifier() . '" #' . $type->getId() . ' does not allow entries in the future (' . $clocking->getStart('Y-m-d H:i:s') . ' - ' . $clocking->getEnd('Y-m-d H:i:s') . ').'); } $clocking->save($con); $clocking->reload(false, $con); if ($clocking->getFrozen()) { throw new APIException(self::ERROR_LOCKED, 'The clocking #' . $clocking->getId() . ' is currently locked for booking.'); } // Check for other non-whole-day clockings with overlapping time if (!$type->getWholeDay()) { $firstConflict = self::createClockingQuery($authUser, $con)->filterById($clocking->getId(), Criteria::NOT_EQUAL)->filterByUserId($clockingUserId)->add(ClockingTypePeer::WHOLE_DAY, 0, Criteria::EQUAL)->filterByStart($end, Criteria::LESS_THAN)->filterByEnd($start, Criteria::GREATER_THAN)->filterByDeleted(0, Criteria::EQUAL)->findOne($con); if ($firstConflict !== null) { throw new APIException(self::ERROR_OVERLAP, $clocking->__toString() . ' overlaps with ' . $firstConflict->__toString() . '.', $firstConflict); } } SystemLogPeer::add('clocking.' . $event, $clocking, SystemLogPeer::CODE_SUCCESSFUL, null, $authUser, array('clocking' => $clocking->toArray()), $con); } catch (Exception $e) { $con->rollBack(); SystemLogPeer::add('clocking.' . $event, $clocking, SystemLogPeer::CODE_FAILED, $e->getMessage(), $authUser, array('exception' => $e->__toString(), 'clocking' => $clocking->toArray()), $con); throw $e; } if (!$con->commit()) { throw new Exception('Could not commit transaction.'); } return $clocking->getId(); }