---
title: Create spatial entities
description: Doctrine Spatial 5.x historical entity mapping examples.
canonical_html: ./entity.html
canonical_markdown: ./entity.md
---

[View HTML version](./entity.html)

> **Older version** — This documentation describes an older major version of `doctrine-spatial`.
> [View the library roadmap](../index.md)

> Historical Doctrine ORM annotation mapping example for Doctrine Spatial 5.x with Doctrine ORM 2.

Doctrine ORM 3 removed annotation metadata support. This example therefore requires
the ORM 2 annotation driver; it does not apply to Doctrine Spatial 6.x, whose ORM
baseline is 3.6. Use PHP attributes or XML mapping with ORM 3. See the
[Doctrine attributes reference](https://www.doctrine-project.org/projects/doctrine-orm/en/3.6/reference/attributes-reference.html).

It is a good practice to use the most adapted column to store you geometric or geographic data. If your entity have only to store points, do not use a
"geometry" type, but a "point" type. Use a geometry column only if your entity can store different types (points and lines as example)

## Example1: Entity with a spatial point

Below, you will find an example to declare an entity with a `point`. Before you need to declare the point type as described in the [configuration
section](../v6/configuration.md).

```php
<?php

// We declare the class of the type we want to use.
use LongitudeOne\Spatial\PHP\Types\Geometry\Point;
// We declare the Mapping as usual
use Doctrine\ORM\Mapping as ORM;

/**
 * Point entity example.
 *
 * As you can see we do not change anything in Entity and Table annotations. Feel free to use them as usual.
 *
 * @ORM\Entity
 * @ORM\Table
 */
class PointEntity
{
    /**
     * @var int The usual Doctrine Identifier
     *
     *  As you can see we do not change anything in Entity and Table annotations. Feel free to use it as usual.
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @var Point
     *
     * As you can see we declare a point property of type point.
     * point shall be declared in the doctrine.yaml as a custom type.
     * Feel free to use options as usual. As example, I declared that point is not nullable. But you can
     * set it to nullable=true if you want.
     *
     * @ORM\Column(type="point", nullable=false)
     */
    protected $point;

    /**
     * Get id.
     * This is the usual Id getter.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get point.
     * This is a standard getter.
     *
     * @return Point
     */
    public function getPoint(): Point
    {
        return $this->point;
    }

    /**
     * Set point.
     * This is a fluent setter.
     *
     * @param Point $point point to set
     *
     * @return self
     */
    public function setPoint(Point $point): self
    {
        $this->point = $point;

        return $this;
    }
}
```

## Seven examples with each geometric spatial types

The [Fixtures directory][Fixtures directory] creates some spatial entities for our tests. Inside this directory, you will find a lot of entities which
are implementing geometric properties:

- Entity with a [geometric][geometric] type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/NoHintGeometryEntity.php)
- Entity with a [geometric linestring][geometric linestring] type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/LineStringEntity.php)
- Entity with a [geometric multilinestring][geometric multilinestring]  type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/MultiLineStringEntity.php)
- Entity with a [geometric multipoint][geometric multipoint]  type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/MultiPointEntity.php)
- Entity with a [geometric multipolygon][geometric multipolygon] type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/MultiPolygonEntity.php)
- Entity with a [geometric point][geometric point] type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/PointEntity.php)
- Entity with a [geometric polygon][geometric polygon] type. [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/PolygonEntity.php)

## Four examples with each geographic spatial types

The [Fixtures directory][Fixtures directory] creates some spatial entities for our tests. Inside this directory, you will find a lot of entities which
are implementing geographic properties:

- Entity with a [geographic][geographic] type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/GeographyEntity.php)
- Entity with a [geographic linestring][geographic linestring] type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/GeoLineStringEntity.php)
- Entity with a [geographic point][geographic point]  type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/GeoPointSridEntity.php)
- Entity with a [geographic polygon][geographic polygon]  type, [download](https://raw.githubusercontent.com/longitude-one/doctrine-spatial/main/tests/LongitudeOne/Spatial/Tests/Fixtures/GeoPolygonEntity.php)

[Fixtures directory]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures

[geographic]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/GeographyEntity.php

[geographic linestring]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/GeoLineStringEntity.php

[geographic point]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/GeoPointSridEntity.php

[geographic polygon]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/GeoPolygonEntity.php

[geometric]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/NoHintGeometryEntity.php

[geometric linestring]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/LineStringEntity.php

[geometric multilinestring]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/MultiLineStringEntity.php

[geometric multipoint]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/MultiPointEntity.php

[geometric multipolygon]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/MultiPolygonEntity.php

[geometric point]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/PointEntity.php

[geometric polygon]: https://github.com/longitude-one/doctrine-spatial/tree/main/tests/LongitudeOne/Spatial/Tests/Fixtures/PolygonEntity.php
