{"id":16770900,"url":"https://github.com/craue/crauegeobundle","last_synced_at":"2025-04-05T11:08:49.013Z","repository":{"id":2035281,"uuid":"2972041","full_name":"craue/CraueGeoBundle","owner":"craue","description":"Doctrine functions for calculating geographical distances in your Symfony project.","archived":false,"fork":false,"pushed_at":"2023-06-26T20:19:19.000Z","size":231,"stargazers_count":125,"open_issues_count":8,"forks_count":18,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-01T02:11:40.982Z","etag":null,"topics":["bundle","php","symfony","symfony-bundle"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/craue.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-12-13T12:24:28.000Z","updated_at":"2024-08-17T13:05:31.000Z","dependencies_parsed_at":"2024-06-18T18:17:55.042Z","dependency_job_id":"1f277945-afb0-4e81-a00a-d69b4c46410f","html_url":"https://github.com/craue/CraueGeoBundle","commit_stats":{"total_commits":195,"total_committers":4,"mean_commits":48.75,"dds":0.01538461538461533,"last_synced_commit":"718761774d96271c22a823bf18fe64e89d20bfcb"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/craue%2FCraueGeoBundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/craue%2FCraueGeoBundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/craue%2FCraueGeoBundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/craue%2FCraueGeoBundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/craue","download_url":"https://codeload.github.com/craue/CraueGeoBundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247325693,"owners_count":20920714,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bundle","php","symfony","symfony-bundle"],"created_at":"2024-10-13T06:25:57.164Z","updated_at":"2025-04-05T11:08:48.992Z","avatar_url":"https://github.com/craue.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Information\n\n[![Build Status](https://app.travis-ci.com/craue/CraueGeoBundle.svg?branch=master)](https://app.travis-ci.com/craue/CraueGeoBundle)\n\nCraueGeoBundle provides Doctrine functions for your Symfony project which allow you to calculate geographical distances within database queries.\nThis bundle is independent of any web service, so once you got it running, it will keep running.\nThere are two Doctrine functions, which return a distance in km:\n\n- `GEO_DISTANCE` takes latitude + longitude for origin and destination\n- `GEO_DISTANCE_BY_POSTAL_CODE` takes country + postal code for origin and destination\n\n# Installation\n\n## Get the bundle\n\nLet Composer download and install the bundle by running\n\n```sh\ncomposer require craue/geo-bundle\n```\n\nin a shell.\n\n## Enable the bundle\n\nIf you don't use Symfony Flex, register the bundle manually:\n\n```php\n// in config/bundles.php\nreturn [\n\t// ...\n\tCraue\\GeoBundle\\CraueGeoBundle::class =\u003e ['all' =\u003e true],\n];\n```\n\nOr, for Symfony 3.4:\n\n```php\n// in app/AppKernel.php\npublic function registerBundles() {\n\t$bundles = [\n\t\t// ...\n\t\tnew Craue\\GeoBundle\\CraueGeoBundle(),\n\t];\n\t// ...\n}\n```\n\n## Prepare the table with geographical data needed for calculations\n\nThe `GEO_DISTANCE_BY_POSTAL_CODE` function, if you'd like to use it, relies on some data which has to be added to your\ndatabase first.\n\n### Create the table\n\nThe `GeoPostalCode` entity provided contains the structure for the geographical data. You import it by calling either\n\n```sh\n# in a shell\nphp bin/console doctrine:migrations:diff\nphp bin/console doctrine:migrations:migrate\n```\n\nor\n\n```sh\n# in a shell\nphp bin/console doctrine:schema:update\n```\n\nor however you like.\n\n### Import the geographical data\n\nThis is probably the most annoying step: Storing all postal codes with their geographical positions for the countries\nyou need. Fortunately, it's not that hard to get this information and import it into your database.\n\nGo to http://download.geonames.org/export/zip/ and download the archives for the countries you need. Let's just take\n`DE.zip`. Unzip the included `DE.txt` file, e.g. to `/tmp/DE.txt`.\n\nCreate a fixture class (in a separate folder to be able to load only this one) which extends the provided base class:\n\n```php\n// MyCompany/MyBundle/Doctrine/Fixtures/CraueGeo/MyGeonamesPostalCodeData.php\nnamespace MyCompany\\MyBundle\\Doctrine\\Fixtures\\CraueGeo;\n\nuse Craue\\GeoBundle\\Doctrine\\Fixtures\\GeonamesPostalCodeData;\nuse Doctrine\\Common\\Persistence\\ObjectManager;\n\nclass MyGeonamesPostalCodeData extends GeonamesPostalCodeData {\n\n\tpublic function load(ObjectManager $manager) {\n\t\t$this-\u003eclearPostalCodesTable($manager);\n\t\t$this-\u003eaddEntries($manager, '/tmp/DE.txt');\n\t}\n\n}\n```\n\nNow, backup your database! Don't blame anyone else for data loss if something goes wrong.\nThen import the fixture and remember to use the `--append` parameter.\n\nChoose the following steps depending on the version of DoctrineFixturesBundle you're using.\n\n\u003cdetails\u003e\n  \u003csummary\u003eDoctrineFixturesBundle \u003c 3.0\u003c/summary\u003e\n\n  Load the fixture(s) in the given folder.\n\n  ```sh\n  # in a shell\n  php bin/console doctrine:fixtures:load --append --fixtures=\"src/MyCompany/MyBundle/Doctrine/Fixtures/CraueGeo\"\n  ```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eDoctrineFixturesBundle \u003e= 3.1\u003c/summary\u003e\n\n  1. a) You first need to register the fixture as a service with a group of your choice.\n\n  ```yaml\n  # in app/config/config.yml\n  services:\n    my_geonames_postal_code_data:\n      class: MyCompany\\MyBundle\\Doctrine\\Fixtures\\CraueGeo\\MyGeonamesPostalCodeData\n      public: false\n      tags:\n       - { name: doctrine.fixture.orm, group: my_geo_data }\n  ```\n\n  1. b) It's also possible to register all classes in a specific folder as services.\n\n  ```yaml\n  # in app/config/config.yml\n  services:\n    MyCompany\\MyBundle\\Doctrine\\Fixtures\\CraueGeo\\:\n      resource: '../../src/MyCompany/MyBundle/Doctrine/Fixtures/CraueGeo/*'\n      public: false\n      tags:\n       - { name: doctrine.fixture.orm, group: my_geo_data }\n  ```\n\n  2. Then, load the fixture(s) of that group.\n\n  ```sh\n  # in a shell\n  php bin/console doctrine:fixtures:load --append --group=my_geo_data\n  ```\n\u003c/details\u003e\n\nThat's it.\n\nYou can also use other data sources you have access to, and write a custom fixture to import it.\n\nIf you have out of memory issues when importing a large number of entries try adding the `--no-debug` switch to avoid\nlogging every single Doctrine query.\n\n# Usage\n\nLet's say you have an entity `Poi` containing countries and postal codes. Now you wish to find all entities within a\nspecific geographical distance with a radius of `$radiusInKm` from a given postal code `$postalCode` in country\n`$country`, and order them by distance.\n\n```php\nuse MyCompany\\MyBundle\\Entity\\Poi;\n\n// example values which could come from a form, remember to validate/sanitize them first\n$country = 'DE';\n$postalCode = '10115';\n$radiusInKm = 10;\n\n// create a query builder\n$queryBuilder = $this-\u003egetDoctrine()-\u003egetEntityManager()-\u003egetRepository(Poi::class)-\u003ecreateQueryBuilder('poi');\n\n// build the query\n$queryBuilder\n\t-\u003eselect('poi, GEO_DISTANCE_BY_POSTAL_CODE(:country, :postalCode, poi.country, poi.postalCode) AS HIDDEN distance')\n\t-\u003ehaving('distance \u003c= :radius')\n\t-\u003esetParameter('country', $country)\n\t-\u003esetParameter('postalCode', $postalCode)\n\t-\u003esetParameter('radius', $radiusInKm)\n\t-\u003eorderBy('distance')\n;\n```\n\n# Advanced stuff\n\n## Using the Doctrine functions for a different database platform\n\nBy default, the Doctrine functions are automatically registered for usage with MySQL. But you can tell the bundle that\nyou want to use them with a different database platform by setting a `flavor`:\n\n```yaml\n# in app/config/config.yml\ncraue_geo:\n  flavor: postgresql\n```\n\nCurrently, the following flavors are supported:\n- `mysql`: MySQL (default value)\n- `postgresql`: PostgreSQL\n- `none`: prevents registration of the Doctrine functions in case you want to do it manually\n\nAs PostgreSQL doesn't support aliases in the HAVING clause and further requires `poi` to appear in the GROUP BY clause,\nyou need to adapt the query (from the usage example above):\n\n```php\n$queryBuilder\n\t-\u003eselect('poi, GEO_DISTANCE_BY_POSTAL_CODE(:country, :postalCode, poi.country, poi.postalCode) AS HIDDEN distance')\n\t-\u003ehaving('GEO_DISTANCE_BY_POSTAL_CODE(:country, :postalCode, poi.country, poi.postalCode) \u003c= :radius')\n\t-\u003esetParameter('country', $country)\n\t-\u003esetParameter('postalCode', $postalCode)\n\t-\u003esetParameter('radius', $radiusInKm)\n\t-\u003egroupBy('poi')\n\t-\u003eorderBy('distance')\n;\n```\n\n## Avoid creating the postal code table\n\nIf you want to avoid registering the `GeoPostalCode` entity (and as a result, avoid creating the `craue_geo_postalcode` table) at all, add\n\n```yaml\n# in app/config/config.yml\ncraue_geo:\n  enable_postal_code_entity: false\n```\n\nto your configuration.\n\n## Use custom names for the Doctrine functions\n\nIf you don't like the default names or need to avoid conflicts with other functions, you can set custom names:\n\n```yaml\n# in app/config/config.yml\ncraue_geo:\n  functions:\n    geo_distance: MY_VERY_OWN_GEO_DISTANCE_FUNCTION\n    geo_distance_by_postal_code: MY_VERY_OWN_GEO_DISTANCE_BY_POSTAL_CODE_FUNCTION\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcraue%2Fcrauegeobundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcraue%2Fcrauegeobundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcraue%2Fcrauegeobundle/lists"}