# doctrine-laminas-hydrator
[![Build Status](https://travis-ci.org/doctrine/doctrine-laminas-hydrator.svg?branch=master)](https://travis-ci.org/doctrine/doctrine-laminas-hydrator)
[![Coverage Status](https://coveralls.io/repos/github/doctrine/doctrine-laminas-hydrator/badge.svg?branch=master)](https://coveralls.io/github/doctrine/doctrine-laminas-hydrator?branch=master)
This library provides Doctrine Hydrators for Laminas.
## Installation
Run the following to install this library:
```bash
$ composer require doctrine/doctrine-laminas-hydrator
```
## Usage
Hydrators convert an array of data to an object (this is called "hydrating") and
convert an object back to an array (this is called "extracting"). Hydrators are mainly used in the context of Forms,
with the binding functionality of Laminas, but can also be used in any hydrating/extracting context (for
instance, it can be used in RESTful context). If you are not really comfortable with hydrators, please first
read [Laminas hydrator's documentation](https://docs.laminas.dev/laminas-hydrator/).
### Basic usage
The library ships with a very powerful hydrator that allow almost any use-case.
#### Create a hydrator
To create a Doctrine Hydrator, you just need one thing: an object manager (also called Entity Manager in Doctrine ORM
or Document Manager in Doctrine ODM):
```php
use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
$hydrator = new DoctrineHydrator($objectManager);
```
The hydrator constructor also allows a second parameter, `byValue`, which is true by default. We will come back later
about this distinction, but to be short, it allows the hydrator the change the way it gets/sets data by either
accessing the public API of your entity (getters/setters) or directly get/set data through reflection, hence bypassing
any of your custom logic.
#### Example 1 : simple entity with no associations
Let's begin by a simple example:
```php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class City
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=48)
*/
protected $name;
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
```
Now, let's use the Doctrine hydrator:
```php
use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
$hydrator = new DoctrineHydrator($entityManager);
$city = new City();
$data = [
'name' => 'Paris',
];
$city = $hydrator->hydrate($data, $city);
echo $city->getName(); // prints "Paris"
$dataArray = $hydrator->extract($city);
echo $dataArray['name']; // prints "Paris"
```
As you can see from this example, in simple cases, the Doctrine Hydrator provides nearly no benefits over a
simpler hydrator like "ClassMethods". However, even in those cases, I suggest you to use it, as it performs automatic
conversions between types. For instance, it can convert timestamp to DateTime (which is the type used by Doctrine to
represent dates):
```php
namespace Application\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Appointment
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="datetime")
*/
protected $time;
public function getId()
{
return $this->id;
}
public function setTime(DateTime $time)
{
$this->time = $time;
}
public function getTime()
{
return $this->time;
}
}
```
Let's use the hydrator:
```php
use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
$hydrator = new DoctrineHydrator($entityManager);
$appointment = new Appointment();
$data = [
'time' => '1357057334',
];
$appointment = $hydrator->hydrate($data, $appointment);
echo get_class($appointment->getTime()); // prints "DateTime"
```
As you can see, the hydrator automatically converted the timestamp to a DateTime object during the hydration, hence
allowing us to have a nice API in our entity with correct typehint.
#### Example 2 : OneToOne/ManyToOne associations
Doctrine Hydrator is especially useful when dealing with associations (OneToOne, OneToMany, ManyToOne) and
integrates nicely with the Form/Fieldset logic ([learn more about this here](https://docs.laminas.dev/laminas-form/collections/)).
Let's take a simple example with a BlogPost and a User entity to illustrate OneToOne association:
```php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=48)
*/
protected $username;
/**
* @ORM\Column(type="string")
*/
protected $password;
public function getId()
{
return $this->id;
}
public function setUsername($username)
{
$this->username = $username;
}
public function getUsername()
{
return $this->username;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getPassword()
{
return $this->password;
}
}
```
And the BlogPost entity, with a ManyToOne association:
```php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class BlogPost
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Application\Entity\User")
*/
protected $user;
/**
* @ORM\Column(type="string")
*/
protected $title;
public function getId()
{
return $this->id;
}
public function setUser(User $user)
{
$this->user = $user;
}
public function getUser()
{
return $this->user;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
}
```
There are two use cases that can arise when using OneToOne association: the toOne entity (in this case, the User) may
already exist (which will often be the case with a User and BlogPost example), or it can be created. The
DoctrineHydrator natively supports both cases.
##### Existing entity in the association
When the association's entity already exists, all you need to do is simply give the identifier of the association:
```php
use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
$hydrator = new DoctrineHydrator($entityManager);
$blogPost = new BlogPost();
$data = [
'title' => 'The best blog post in the world!',
'user' => [
'id' => 2, // Written by user 2
],
];
$blogPost = $hydrator->hydrate($data, $blogPost);
echo $blogPost->getTitle(); // prints "The best blog post in the world!"
echo $blogPost->getUser()->getId(); // prints 2
```
**NOTE** : when using association whose primary key is not compound, you can rewrite the following more succinctly:
```php
$data = [
'title' => 'The best blog post in the world!',
'user' => [
'id' => 2, // Written by user 2
],
];
```
to:
```php
$data = [
'title' => 'The best blog post in the world!',
'user' => 2,
];
```
##### Non-existing entity in the association
If the association's entity does not exist, you just need to give the object:
```php
use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator;
$hydrator = new DoctrineHydrator($entityManager);
$blogPost = new BlogPost();
$user = new User();
$user->setUsername('bakura');
$user->setPassword('p@$$w0rd');
$data =
没有合适的资源?快使用搜索试试~ 我知道了~
PHP实例开发源码—DBShop php电子商务网店系统.zip
共2000个文件
php:23946个
png:450个
md:403个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 48 浏览量
2022-11-14
02:04:51
上传
评论
收藏 67.57MB ZIP 举报
温馨提示
PHP实例开发源码—DBShop php电子商务网店系统.zip
资源推荐
资源详情
资源评论
收起资源包目录
PHP实例开发源码—DBShop php电子商务网店系统.zip (2000个子文件)
random_compat.phar.pubkey.asc 488B
doctrine-module.bat 231B
doctrine.bat 229B
generate-deps-for-config-factory.bat 161B
laminas-development-mode.bat 155B
generate-factory-for-class.bat 155B
templatemap_generator.php.bat 144B
var-dump-server.bat 142B
doctrine-module.bat 138B
jp.php.bat 127B
doctrine-dbal.bat 125B
doctrine.bat 119B
carbon.bat 118B
carbon.bat 93B
demo.bin 72KB
bit-image.bin 10KB
graphics.bin 9KB
receipt-with-logo.bin 9KB
character-tables.bin 8KB
barcode.bin 3KB
pdf417-code.bin 2KB
character-encodings.bin 2KB
qr-code.bin 2KB
text-size.bin 368B
margins-and-spacing.bin 339B
unifont-print-buffer.bin 243B
rgb32-xbgr.bmp 32KB
rgba32-61754.bmp 32KB
rgba32.bmp 32KB
rgba32-1010102.bmp 32KB
rgba32-81284.bmp 32KB
rgba32abf.bmp 32KB
rgba32h56.bmp 32KB
rgb32-111110.bmp 32KB
rgb32h52.bmp 32KB
rgb32-7187.bmp 32KB
rgb32bf.bmp 32KB
rgb32bfdef.bmp 32KB
rgb32fakealpha.bmp 32KB
rgb32.bmp 32KB
rgb24prof.bmp 27KB
rgb24largepal.bmp 25KB
rgb24pal.bmp 25KB
rgb24prof2.bmp 25KB
rgb24lprof.bmp 24KB
rgb24.bmp 24KB
reallybig.bmp 24KB
rgb16-565pal.bmp 17KB
rgba16-1924.bmp 16KB
rgba16-4444.bmp 16KB
rgba16-5551.bmp 16KB
rgb16-231.bmp 16KB
rgb16-3103.bmp 16KB
rgb16bfdef.bmp 16KB
rgb16-565.bmp 16KB
rgb16-880.bmp 16KB
rgb16faketrns.bmp 16KB
rgb16.bmp 16KB
pal8oversizepal.bmp 9KB
pal8offs.bmp 9KB
pal8v5.bmp 9KB
pal8v4.bmp 9KB
pal8os2v2-sz.bmp 9KB
pal8os2v2.bmp 9KB
pal8-0.bmp 9KB
pal8os2v2-40sz.bmp 9KB
pal8.bmp 9KB
pal8topdown.bmp 9KB
pal8gs.bmp 9KB
badpalettesize.bmp 9KB
pal8os2v2-16.bmp 9KB
pal8rletrns.bmp 9KB
badrle.bmp 9KB
badrlebis.bmp 9KB
badrleter.bmp 9KB
pal8w126.bmp 9KB
ba-bm.bmp 9KB
pal8w125.bmp 9KB
pal8os2-hs.bmp 9KB
pal8os2-sz.bmp 9KB
pal8os2.bmp 9KB
pal8os2sp.bmp 9KB
pal8rle.bmp 9KB
rletopdown.bmp 9KB
pal8badindex.bmp 8KB
pal8w124.bmp 8KB
pal8rlecut.bmp 8KB
pal8nonsquare.bmp 5KB
pal4rletrns.bmp 4KB
badrle4.bmp 4KB
badrle4bis.bmp 4KB
badrle4ter.bmp 4KB
pal4gs.bmp 4KB
pal4.bmp 4KB
pal4rle.bmp 4KB
pal4rlecut.bmp 4KB
rgb24jpeg.bmp 2KB
pal1huff.bmp 2KB
pal2.bmp 2KB
pal2color.bmp 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
毕业_设计
- 粉丝: 1935
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功