if ($contact == null) { return err_general_error($response, "Contact Id " . $id . " not found"); } $contact->fromArray($parsedBody); if ($contact->validate()) { $contact->save(); } else { return err_general_error($response, "Validation failed"); } /* response with the saves contact */ $response->getBody()->write($contact->toJSON()); return $response; }); $app->delete('/contacts/{id}', function ($request, $response, $args) { $id = $request->getAttribute('id'); $contact = ContactQuery::create()->findPK($id); if ($contact == null) { return err_general_error($response, "Contact Id {$id} not found"); } $contact->delete(); return success($response, "Contact deleted"); }); $app->get('/contacts/{id}/gigs', function ($request, $response, $args) { $id = $request->getAttribute('id'); $con = Propel::getWriteConnection('productionSource'); $sql = "SELECT * FROM Gig WHERE id IN " . "(SELECT ContactAssignmentToGig.GigId FROM ContactAssignmentToGig" . " WHERE ContactAssignmentToGig.ContactId = :contactid)"; $stmt = $con->prepare($sql); $stmt->execute(array('contactid' => $id)); $formatter = new ObjectFormatter(); $formatter->setClass('\\Gig'); //full qualified class name
/** * Returns a new ContactQuery object. * * @param string $modelAlias The alias of a model in the query * @param ContactQuery|Criteria $criteria Optional Criteria to build the query from * * @return ContactQuery */ public static function create($modelAlias = null, $criteria = null) { if ($criteria instanceof ContactQuery) { return $criteria; } $query = new ContactQuery(); if (null !== $modelAlias) { $query->setModelAlias($modelAlias); } if ($criteria instanceof Criteria) { $query->mergeWith($criteria); } return $query; }
/** * 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(ContactPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } $con->beginTransaction(); try { $deleteQuery = ContactQuery::create()->filterByPrimaryKey($this->getPrimaryKey()); $ret = $this->preDelete($con); // symfony_behaviors behavior foreach (sfMixer::getCallables('BaseContact:delete:pre') as $callable) { if (call_user_func($callable, $this, $con)) { $con->commit(); return; } } if ($ret) { $deleteQuery->delete($con); $this->postDelete($con); // symfony_behaviors behavior foreach (sfMixer::getCallables('BaseContact:delete:post') as $callable) { call_user_func($callable, $this, $con); } $con->commit(); $this->setDeleted(true); } else { $con->commit(); } } catch (Exception $e) { $con->rollBack(); throw $e; } }
$formatter = new ObjectFormatter(); $formatter->setClass('\\Contact'); //full qualified class name $contacts = $formatter->format($con->getDataFetcher($stmt)); $response->getBody()->write(parseToJSONArray($contacts)); return $response; }); $app->post('/venues/{id}/contacts', function ($request, $response, $args) { $venueid = $request->getAttribute('id'); $parsedBody = $request->getParsedBody(); if ($parsedBody == null) { return err_general_error($response, "Provide a body to assign a contact to this venues"); } $contact = new Contact(); $contact->fromArray($parsedBody); $contact = ContactQuery::create()->findPK($contact->getId()); if ($contact == null) { return err_general_error($response, "Venue Id " . $id . " not found"); } $con = Propel::getWriteConnection('productionSource'); $sql = "INSERT INTO ContactAssignmentToVenue(VenueId, ContactId) VALUES(:venueid, :contactid)"; $stmt = $con->prepare($sql); $stmt->execute(array(':venueid' => $venueid, ':contactid' => $contact->getId())); return success($response, "Contact assigned to venue"); }); $app->delete('/venues/{venueid}/contacts/{contactid}', function ($request, $response, $args) { $venueid = $request->getAttribute('venueid'); $contactid = $request->getAttribute('contactid'); $assignment = ContactAssignmentToVenueQuery::create()->filterByContactId($contactid)->filterByVenueId($venueid); if ($assignment == null) { return err_general_error($response, "No assignment with Venue Id {$id} and Contact Id {$contactid} found.");
/** * Returns the number of related Contact objects. * * @param Criteria $criteria * @param boolean $distinct * @param PropelPDO $con * @return int Count of related Contact objects. * @throws PropelException */ public function countContacts(Criteria $criteria = null, $distinct = false, PropelPDO $con = null) { if (null === $this->collContacts || null !== $criteria) { if ($this->isNew() && null === $this->collContacts) { return 0; } else { $query = ContactQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByState($this)->count($con); } } else { return count($this->collContacts); } }
/** * Get the associated Contact object * * @param PropelPDO $con Optional Connection object. * @return Contact The associated Contact object. * @throws PropelException */ public function getContact(PropelPDO $con = null) { if ($this->aContact === null && $this->contact_id !== null) { $this->aContact = ContactQuery::create()->findPk($this->contact_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->aContact->addUsers($this); */ } return $this->aContact; }