public function removeCountry(AreaRemoveCountryEvent $event) { CountryAreaQuery::create()->filterByCountryId($event->getCountryId())->filterByAreaId($event->getAreaId())->delete(); if (null !== ($area = AreaQuery::create()->findPk($event->getAreaId()))) { $event->setArea($area); } }
public function buildModelCriteria() { $search = CountryQuery::create(); /* manage translations */ $this->configureI18nProcessing($search); $id = $this->getId(); if (null !== $id) { $search->filterById($id, Criteria::IN); } $area = $this->getArea(); if (null !== $area) { $search->useCountryAreaQuery('with_area')->filterByAreaId($area, Criteria::IN)->endUse(); } $excludeArea = $this->getExcludeArea(); if (null !== $excludeArea) { // FIXME : did not find a way to do this in a single request :( // select * from country where id not in (select country_id from country_area where area in (...)) $countries = CountryAreaQuery::create()->filterByAreaId($excludeArea, Criteria::IN)->select(['country_id'])->find(); $search->filterById($countries->toArray(), Criteria::NOT_IN); } $withArea = $this->getWithArea(); if (true === $withArea) { $search->joinCountryArea('with_area', Criteria::LEFT_JOIN)->where('`with_area`.country_id ' . Criteria::ISNOTNULL); } elseif (false === $withArea) { $search->joinCountryArea('with_area', Criteria::LEFT_JOIN)->where('`with_area`.country_id ' . Criteria::ISNULL); } $exclude = $this->getExclude(); if (!is_null($exclude)) { $search->filterById($exclude, Criteria::NOT_IN); } $search->addAscendingOrderByColumn('i18n_TITLE'); return $search; }
/** * This method ensure backward compatibility to Thelia 2.1, where a country belongs to one and * only one shipping zone. * * @deprecated a country may belong to several Areas (shipping zones). Use CountryArea queries instead */ public function getAreaId() { $firstAreaCountry = CountryAreaQuery::create()->findOneByCountryId($this->getId()); if (null !== $firstAreaCountry) { return $firstAreaCountry->getAreaId(); } return null; }
/** * Check if a delivery module is suitable for the given country. * * @param Country $country * @param Module $module * @return null|AreaDeliveryModule * @throws \Propel\Runtime\Exception\PropelException */ public function findByCountryAndModule(Country $country, Module $module) { $response = null; $countryInAreaList = CountryAreaQuery::create()->filterByCountryId($country->getId())->find(); foreach ($countryInAreaList as $countryInArea) { $response = $this->filterByAreaId($countryInArea->getAreaId())->filterByModule($module)->findOne(); if ($response !== null) { break; } } return $response; }
protected function migrateShippingZones(MigrateCountryEvent $event) { $con = Propel::getWriteConnection(CountryAreaTableMap::DATABASE_NAME); $con->beginTransaction(); try { $updatedRows = CountryAreaQuery::create()->filterByCountryId($event->getCountry())->update(['CountryId' => $event->getNewCountry(), 'StateId' => $event->getNewState()]); $con->commit(); return $updatedRows; } catch (PropelException $e) { $con->rollback(); throw $e; } }
public function buildModelCriteria() { $search = CountryAreaQuery::create(); $id = $this->getId(); if (count($id)) { $search->filterById($id, Criteria::IN); } $areas = $this->getArea(); if (count($areas)) { $search->filterByAreaId($areas, Criteria::IN); } $countries = $this->getCountry(); if (count($countries)) { $search->filterByCountryId($countries, Criteria::IN); } $states = $this->getState(); if (count($states)) { $search->filterByStateId($states, Criteria::IN); } $orders = $this->getOrder(); foreach ($orders as $order) { switch ($order) { case "id": $search->orderById(Criteria::ASC); break; case "id_reverse": $search->orderById(Criteria::DESC); break; case "area": $search->orderByAreaId(Criteria::ASC); break; case "area_reverse": $search->orderByAreaId(Criteria::DESC); break; case "country": $search->orderByCountryId(Criteria::ASC); break; case "country_reverse": $search->orderByCountryId(Criteria::DESC); break; case "state": $search->orderByStateId(Criteria::ASC); break; case "state_reverse": $search->orderByStateId(Criteria::DESC); break; } } return $search; }
public function buildModelCriteria() { $search = CountryQuery::create(); /* manage translations */ $this->configureI18nProcessing($search); $id = $this->getId(); if (null !== $id) { $search->filterById($id, Criteria::IN); } $area = $this->getArea(); if (null !== $area) { $search->useCountryAreaQuery('with_area')->filterByAreaId($area, Criteria::IN)->endUse(); } $excludeArea = $this->getExcludeArea(); if (null !== $excludeArea) { // FIXME : did not find a way to do this in a single request :( // select * from country where id not in (select country_id from country_area where area in (...)) $countries = CountryAreaQuery::create()->filterByAreaId($excludeArea, Criteria::IN)->select(['country_id'])->find(); $search->filterById($countries->toArray(), Criteria::NOT_IN); } $withArea = $this->getWithArea(); if (true === $withArea) { $search->distinct()->joinCountryArea('with_area', Criteria::LEFT_JOIN)->where('`with_area`.country_id ' . Criteria::ISNOTNULL); } elseif (false === $withArea) { $search->joinCountryArea('with_area', Criteria::LEFT_JOIN)->where('`with_area`.country_id ' . Criteria::ISNULL); } $exclude = $this->getExclude(); if (!is_null($exclude)) { $search->filterById($exclude, Criteria::NOT_IN); } $hasStates = $this->getHasStates(); if ($hasStates !== BooleanOrBothType::ANY) { $search->filterByHasStates($hasStates ? 1 : 0); } $visible = $this->getVisible(); if ($visible !== BooleanOrBothType::ANY) { $search->filterByVisible($visible ? 1 : 0); } $orders = $this->getOrder(); foreach ($orders as $order) { switch ($order) { case "id": $search->orderById(Criteria::ASC); break; case "id_reverse": $search->orderById(Criteria::DESC); break; case "alpha": $search->addAscendingOrderByColumn('i18n_TITLE'); break; case "alpha_reverse": $search->addDescendingOrderByColumn('i18n_TITLE'); break; case "visible": $search->orderByVisible(Criteria::ASC); break; case "visible_reverse": $search->orderByVisible(Criteria::DESC); break; case "random": $search->clearOrderByColumns(); $search->addAscendingOrderByColumn('RAND()'); break 2; break; } } return $search; }
/** * @param AreaModel $area * @depends testAddCountry * @return AreaModel */ public function testRemoveCountry(AreaModel $area) { $country = CountryQuery::create()->filterByArea($area)->find()->getFirst(); $event = new AreaRemoveCountryEvent($area->getId(), $country->getId()); $areaAction = new Area(); $areaAction->removeCountry($event); $updatedCountry = CountryAreaQuery::create()->filterByCountryId($country->getId())->filterByStateId(null)->filterByAreaId($area->getId())->findOne(); $updatedArea = $event->getArea(); $this->assertInstanceOf('Thelia\\Model\\Area', $updatedArea); $this->assertNull($updatedCountry); return $event->getArea(); }
/** * Removes this object from datastore and sets delete attribute. * * @param ConnectionInterface $con * @return void * @throws PropelException * @see CountryArea::setDeleted() * @see CountryArea::isDeleted() */ public function delete(ConnectionInterface $con = null) { if ($this->isDeleted()) { throw new PropelException("This object has already been deleted."); } if ($con === null) { $con = Propel::getServiceContainer()->getWriteConnection(CountryAreaTableMap::DATABASE_NAME); } $con->beginTransaction(); try { $deleteQuery = ChildCountryAreaQuery::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; } }
/** * @param AreaModel $area * @depends testAddCountry */ public function testRemoveCountry(AreaModel $area) { $country = CountryQuery::create()->filterByArea($area)->find()->getFirst(); $event = new AreaRemoveCountryEvent($area->getId(), $country->getId()); $event->setDispatcher($this->getMock("Symfony\\Component\\EventDispatcher\\EventDispatcherInterface")); $areaAction = new Area(); $areaAction->removeCountry($event); $updatedCountry = CountryAreaQuery::create()->filterByCountryId($country->getId())->filterByAreaId($area->getId())->findOne(); $updatedArea = $event->getArea(); $this->assertInstanceOf('Thelia\\Model\\Area', $updatedArea); $this->assertNull($updatedCountry); return $event->getArea(); }
/** * Performs an INSERT on the database, given a CountryArea or Criteria object. * * @param mixed $criteria Criteria or CountryArea object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(CountryAreaTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from CountryArea object } // Set the correct dbName $query = CountryAreaQuery::create()->mergeWith($criteria); try { // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) $con->beginTransaction(); $pk = $query->doInsert($con); $con->commit(); } catch (PropelException $e) { $con->rollBack(); throw $e; } return $pk; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Area is new, it will return * an empty collection; or if this Area has previously * been saved, it will retrieve related CountryAreas 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 Area. * * @param Criteria $criteria optional Criteria object to narrow the query * @param ConnectionInterface $con optional connection object * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) * @return Collection|ChildCountryArea[] List of ChildCountryArea objects */ public function getCountryAreasJoinCountry($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN) { $query = ChildCountryAreaQuery::create(null, $criteria); $query->joinWith('Country', $joinBehavior); return $this->getCountryAreas($query, $con); }