/** * Overriding doBind method * * TODO: I am breaking the validations. How could I do this in a right way? */ protected function doBind(array $values) { $officeAds = OfficeAdsTable::getInstance()->findByOfficeId($this->getObject()->getOfficeId()); //Scheduled for delete //If the object was previously in the database. if (!$this->getObject()->isNew()) { //The Doctrine Collection must not be null because the object was previously in the data base. //We must search the whole Doctrine Collection in order to find out if the object was in the database //and it has been chosen for the user to stay in the database. foreach ($officeAds as $index => $officeAd) { //If the user chose something to associate with. if (isset($values['ad_id'])) { foreach ($values['ad_id'] as $value) { //That ad has been found in the database, so it is not going to be removed. if ($officeAd->getAdId() == $value) { continue 2; } } } //The user did not choose this ad which was previously in the database, so we have to remove it. $this->scheduledForDeletion[$index] = $officeAd; } } //Scheduled for save if (isset($values['ad_id'])) { //We must search the whole array in order to find out if the chosen ad is in the database or not. //If the object was not previously in the database we must save it, otherwise we do nothing. foreach ($values['ad_id'] as $index => $value) { //If the object was previously in the database. if (!$this->getObject()->isNew()) { foreach ($officeAds as $officeAd) { //The ad has been found in the database, it is not going to be saved again. if ($officeAd->getAdId() == $value) { continue 2; } } } //The user chose another ad to be stored in the database. $newOfficeAds = new OfficeAds(); $newOfficeAds->office_id = $this->getObject()->getOfficeId(); $newOfficeAds->ad_id = $value; $this->scheduledForSaving[$index] = $newOfficeAds; } } }
public function executeUpdateLink(sfWebRequest $request) { $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); $this->forward404Unless($officeAds = Doctrine_Core::getTable('OfficeAds')->find(array($request->getParameter('id'))), sprintf('Object office ads does not exist (%s).', $request->getParameter('id'))); //Get user Id $userId = $this->getUser()->getGuardUser()->getId(); //Get company owned by that user and insert value in form $companyUserId = CompanyTable::getInstance()->findOneByUserId($userId)->getId(); //Get id number sent by the user (never trust the users) $officeAdsId = $request->getParameter('id'); $companyOfficeId = OfficeAdsTable::getInstance()->findOneById($officeAdsId)->getOffice()->getCompanyId(); $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id'))); $this->form = new OfficeAdsForm($officeAds, array('companyId' => $companyOfficeId)); $this->sort = $request->getParameter('sort', 'id'); $this->page = $request->getParameter('page', 1); $this->officeId = $officeAds->getOfficeId(); $this->processAdsForm($request, $this->form, $this->sort, $this->page); $this->setTemplate('link'); }