/** * Get the associated BookingType object * * @param PropelPDO $con Optional Connection object. * @param $doQuery Executes a query to get the object if required * @return BookingType The associated BookingType object. * @throws PropelException */ public function getBookingType(PropelPDO $con = null, $doQuery = true) { if ($this->aBookingType === null && $this->booking_type_id !== null && $doQuery) { $this->aBookingType = BookingTypeQuery::create()->findPk($this->booking_type_id, $con); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aBookingType->addBookings($this); */ } return $this->aBookingType; }
/** * 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; }
/** * Returns the number of related BookingType objects. * * @param Criteria $criteria * @param boolean $distinct * @param PropelPDO $con * @return int Count of related BookingType objects. * @throws PropelException */ public function countBookingTypes(Criteria $criteria = null, $distinct = false, PropelPDO $con = null) { $partial = $this->collBookingTypesPartial && !$this->isNew(); if (null === $this->collBookingTypes || null !== $criteria || $partial) { if ($this->isNew() && null === $this->collBookingTypes) { return 0; } if ($partial && !$criteria) { return count($this->getBookingTypes()); } $query = BookingTypeQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByAccount($this)->count($con); } return count($this->collBookingTypes); }
/** * 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(BookingTypePeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } $con->beginTransaction(); try { $deleteQuery = BookingTypeQuery::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; } }