Skip to content

Commit

Permalink
Update README to use PHP attributes & add info about postgres (#230)
Browse files Browse the repository at this point in the history
* chore: update README to use PHP attributes & add info about postgres
* fix(docs): correct attribute syntax issues

thanks @Chris53897
  • Loading branch information
jankal authored May 26, 2024
1 parent 5eae94d commit c909c19
Showing 1 changed file with 51 additions and 66 deletions.
117 changes: 51 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,44 +93,16 @@ return [

### Mappings

Then, in your models, you may annotate properties by setting the `@Column`
Then, in your models, you may annotate properties by setting the `#[Column]`
type to `uuid`, and defining a custom generator of `Ramsey\Uuid\UuidGenerator`.
Doctrine will handle the rest.

``` php
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidGenerator;

/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Product
{
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
protected $id;

public function getId()
{
return $this->id;
}
}
```

or, as follows, with [PHP 8 attributes](https://www.php.net/attributes) and [type declarations](https://www.php.net/types.declarations):

``` php
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Ramsey\Uuid\UuidInterface;


#[ORM\Entity]
#[ORM\Table(name: "products")]
class Product
Expand All @@ -139,16 +111,16 @@ class Product
#[ORM\Column(type: "uuid", unique: true)]
#[ORM\GeneratedValue(strategy: "CUSTOM")]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
protected UuidInterface|string $id;
protected UuidInterface $id;

public function getId(): string
public function getId(): UuidInterface
{
return $this->id;
}
}
```

If you use the XML Mapping instead of PHP annotations.
If you use the XML Mapping instead of PHP attributes.

``` xml
<id name="id" column="id" type="uuid">
Expand All @@ -171,7 +143,7 @@ id:
### Binary database columns
In the previous example, Doctrine will create a database column of type `CHAR(36)`,
In the previous example, Doctrine will create a database column of type `CHAR(36)` if MariaDB / MySQL are in use,
but you may also use this library to store UUIDs as binary strings. The
`UuidBinaryType` helps accomplish this.

Expand All @@ -197,7 +169,7 @@ doctrine:

Then, when annotating model class properties, use `uuid_binary` instead of `uuid`:

@Column(type="uuid_binary")
#[Column(type: "uuid_binary")]

### InnoDB-optimised binary UUIDs - deprecated

Expand Down Expand Up @@ -234,23 +206,17 @@ type to `uuid_binary_ordered_time`, and defining a custom generator of
`Ramsey\Uuid\UuidOrderedTimeGenerator`. Doctrine will handle the rest.

``` php
/**
* @Entity
* @Table(name="products")
*/
#[Entity]
#[Table(name: "products")]´
class Product
{
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @Id
* @Column(type="uuid_binary_ordered_time", unique=true)
* @GeneratedValue(strategy="CUSTOM")
* @CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
*/
protected $id;
public function getId()
#[Id]
#[Column(type: "uuid_binary_ordered_time", unique: true)]
#[GeneratedValue(strategy: "CUSTOM")]
#[CustomIdGenerator(class: UuidOrderedTimeGenerator::class)]
protected UuidInterface $id;
public function getId(): UuidInterface
{
return $this->id;
}
Expand Down Expand Up @@ -293,28 +259,22 @@ doctrine:
# uuid_binary: binary
```

Then, in your models, you may annotate properties by setting the `@Column`
Then, in your models, you may annotate properties by setting the `#[Column]`
type to `uuid_binary`, and defining a custom generator of
`Ramsey\Uuid\UuidV7Generator`. Doctrine will handle the rest.

``` php
/**
* @Entity
* @Table(name="products")
*/
#[Entity]
#[Table(name: "products")]
class Product
{
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @Id
* @Column(type="uuid_binary", unique=true)
* @GeneratedValue(strategy="CUSTOM")
* @CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidV7Generator")
*/
protected $id;
public function getId()
#[Id]
#[Column(type: "uuid_binary", unique: true)]
#[GeneratedValue(strategy: "CUSTOM")]
#[CustomIdGenerator(class: UuidV7Generator::class)]
protected UuidInterface $id;
public function getId(): UuidInterface
{
return $this->id;
}
Expand All @@ -330,6 +290,31 @@ If you use the XML Mapping instead of PHP annotations.
</id>
```

### PostgreSQL considerations


If you are using PostgreSQL, Doctrine uses PostgreSQL's `uuid` for the `Ramsey\Uuid\Doctrine\UuidType` (`uuid`).
Therefor you don't need to use the `uuid_binary` / `uuid_binary_ordered_time` types when using PostgreSQL.
You can still use `UuidV7Generator::class` to optimize indexing though.

``` php
#[Entity]
#[Table(name: "products")]
class Product
{
#[Id]
#[Column(type: "uuid", unique: true)]
#[GeneratedValue(strategy: "CUSTOM")]
#[CustomIdGenerator(class: UuidV7Generator::class)]
protected UuidInterface $id;
public function getId(): UuidInterface
{
return $this->id;
}
}
```

### Working with binary identifiers

When working with binary identifiers you may wish to convert them into a readable format.
Expand Down

0 comments on commit c909c19

Please sign in to comment.