/** * Returns a new BookingQuery object. * * @param string $modelAlias The alias of a model in the query * @param BookingQuery|Criteria $criteria Optional Criteria to build the query from * * @return BookingQuery */ public static function create($modelAlias = null, $criteria = null) { if ($criteria instanceof BookingQuery) { return $criteria; } $query = new BookingQuery(); if (null !== $modelAlias) { $query->setModelAlias($modelAlias); } if ($criteria instanceof Criteria) { $query->mergeWith($criteria); } return $query; }
public static function fromTransaction(Transaction $transaction, PropelPDO $con = null) { $transactionId = $transaction->getId(); $bookingData = array(); $bookings = BookingQuery::create()->filterByTransactionId($transactionId)->find($con); foreach ($bookings as $booking) { $bookingData[] = self::from($booking, $con); } $clockingData = array(); $clockings = ClockingQuery::create()->joinTransactionClocking()->joinWith('ClockingType')->add(TransactionClockingPeer::TRANSACTION_ID, $transactionId)->find($con); foreach ($clockings as $clocking) { $clockingData[] = self::from($clocking, $con) + array('Type' => self::from($clocking->getClockingType($con), $con)); } return array('Start' => $transaction->getStart('U'), 'End' => $transaction->getEnd('U'), 'Bookings' => $bookingData, 'Clockings' => $clockingData, 'User' => self::from($transaction->getUserRelatedByUserId($con), $con)) + $transaction->toArray(); }
/** * Removes this object from datastore and sets delete attribute. * * @param PropelPDO $con * @return void * @throws PropelException * @throws Exception * @see BaseObject::setDeleted() * @see BaseObject::isDeleted() */ public function delete(PropelPDO $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getConnection(BookingPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } $con->beginTransaction(); try { $deleteQuery = BookingQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
/** * Creates a new transaction. * * @param array $transactionData An associative array with the properties * "UserId", "Start", "End" and "Comment". * @param array $bookingData An array of booking data. Existing bookings are * specified by ID, new bookings must be specified as objects: * <code>{ "BookingTypeId": ..., "Label": ..., "Value": ... }</code> * @param array $clockingIds Optional. An array of clocking IDs to assign * to the new transaction. Default is NULL. * @throws Exception * @return bool */ public function do_create(array $transactionData = null, array $bookingData = null, array $clockingIds = null) { $con = Propel::getConnection(); if (!$con->beginTransaction()) { throw new Exception('Could not start transaction.'); } try { $user = $this->requireUser(); if (!$user->isAdmin()) { throw new Exception('Non-administrative user "' . $user->getFQN($con) . '" cannot create transactions.'); } $account = $user->getAccount($con); if ($account === null) { throw new Exception('Could not determine the account the authenticated user "' . $user->getName() . '" #' . $user->getId() . ' belongs to.'); } if (empty($bookingData)) { throw new Exception('Transaction must contain at least one booking.'); } $transaction = new Transaction(); $transaction->fromArray(array_intersect_key($transactionData, array('UserId' => true, 'Start' => true, 'End' => true, 'Comment' => true))); $employeeId = $transaction->getUserId(); $employee = $this->findUserById($employeeId, $con); if ($employee === null) { throw new Exception('Employee with ID ' . $employeeId . ' could not be found.'); } // Separate new and existing bookings (the latter are specified by ID) $bookingIds = array(); foreach ($bookingData as $bookingIndex => $bookingItem) { if (!is_array($bookingItem)) { $bookingIds[$bookingItem] = $bookingItem; unset($bookingData[$bookingIndex]); } } $bookingTypeIds = BookingTypeQuery::create()->findByAccountId($account->getId(), $con)->getArrayCopy('Id'); $newBookings = array_values($this->createBookings($bookingData, $bookingTypeIds, $con)); // Load existing bookings and check for missing records $bookingsById = BookingQuery::create()->joinBookingType()->add(BookingTypePeer::ACCOUNT_ID, $user->getAccountId())->findPks($bookingIds)->getArrayCopy('Id'); $missingBookingIds = array_diff_key($bookingIds, $bookingsById); if (count($missingBookingIds) > 0) { throw new Exception('Could not find bookings with the following IDs: ' . implode(', ', array_keys($missingBookingIds))); } if (empty($clockingIds)) { $clockings = array(); } else { $clockings = ClockingQuery::create()->filterByUserRelatedByUserId($employee)->addAscendingOrderByColumn(ClockingPeer::START)->addAscendingOrderByColumn(ClockingPeer::END)->addAscendingOrderByColumn(ClockingPeer::ID)->findPks($clockingIds, $con); $clockingsById = $clockings->getArrayCopy('Id'); $missingClockingIds = array_diff_key(array_fill_keys($clockingIds, true), $clockingsById); if (count($missingClockingIds) > 0) { throw new Exception('Could not find clockings of user "' . $employee->getName() . '" #' . $employeeId . ' with the following IDs: ' . implode(', ', array_keys($missingClockingIds))); } } $start = isset($transactionData['Start']) ? $transactionData['Start'] : null; if ((string) $start === '') { throw new Exception('Start date must be specified for transaction.'); } $end = isset($transactionData['End']) ? $transactionData['End'] : null; if ((string) $end === '') { throw new Exception('End date must be specified for transaction.'); } $transaction->save($con); // Link bookings to transaction foreach (array_merge(array_values($bookingsById), $newBookings) as $booking) { $booking->setTransaction($transaction)->save($con); } // Link clockings $this->linkTransactionClockings($transaction, $clockings, $con); } catch (Exception $e) { $con->rollBack(); throw $e; } if (!$con->commit()) { throw new Exception('Could not commit transaction.'); } return true; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Transaction is new, it will return * an empty collection; or if this Transaction has previously * been saved, it will retrieve related Bookings from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in Transaction. * * @param Criteria $criteria optional Criteria object to narrow the query * @param PropelPDO $con optional connection object * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return PropelObjectCollection|Booking[] List of Booking objects */ public function getBookingsJoinBookingType($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN) { $query = BookingQuery::create(null, $criteria); $query->joinWith('BookingType', $join_behavior); return $this->getBookings($query, $con); }