Skip to content

Commit 07e85a9

Browse files
committed
initial commit
0 parents  commit 07e85a9

File tree

11 files changed

+2422
-0
lines changed

11 files changed

+2422
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
/logs/*
3+
.idea
4+
.env

README.md

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
Yii2 Embedded Models
2+
==========
3+
4+
Usage
5+
-----
6+
7+
In order to embedd another models to your model using you should extend it from one provided by library.
8+
Use HydrateBehavior to embed single model to property or HydrateCollectionBehavior to embed collection of models.
9+
To correctly attach behavior you have to provide valid configuration values to following properties:
10+
- `attribute` name of attribute in primary model in which you want to embed
11+
- `targetModel` class name of embedded model
12+
- `hydrator` Hydrator which will be used to hydrate and extract. You can provide class name if you configured hydrator in your DI container, array style definition for Yii::createObject or instance of Hydrator
13+
14+
Your embedded models can also embed another models.
15+
When validate will be called for your primary model it will be called for all your embedded models and fields in your primary model coreesponding to embedded models will be correctly populated with error messages.
16+
Embedded models will be automatically created by behavior when yor model is populated from database or when it is populated with `Model::load()` data from request
17+
18+
19+
20+
Example of attaching behavior to mongodb ActiveRecord model
21+
22+
```php
23+
24+
# configure default Hydrator object in your DI
25+
26+
Yii::$container->set(
27+
'Indigerd\Hydrator\Accessor\AccessorInterface',
28+
'Indigerd\Hydrator\Accessor\PropertyAccessor'
29+
);
30+
31+
Yii::$container->set(
32+
'Indigerd\Hydrator\Hydrator'
33+
);
34+
35+
# Primary ("parent") model
36+
37+
use Indigerd\Hydrator\Hydrator;
38+
use indigerd\embedded\behavior\HydrateBehavior;
39+
use indigerd\embedded\behavior\HydrateCollectionBehavior;
40+
use indigerd\embedded\model\mongodb\ActiveRecord;
41+
42+
class Clinic extends ActiveRecord
43+
{
44+
public static function collectionName(): string
45+
{
46+
return 'clinics';
47+
}
48+
49+
public function attributes(): array
50+
{
51+
return [
52+
'_id',
53+
'name',
54+
'country',
55+
'doctors',
56+
];
57+
}
58+
59+
public function behaviors() : array
60+
{
61+
return [
62+
[
63+
'class' => HydrateBehavior::class,
64+
'hydrator' => Hydrator::class,
65+
'targetModel' => Country::class,
66+
'attribute' => 'country'
67+
],
68+
[
69+
'class' => HydrateCollectionBehavior::class,
70+
'hydrator' => Hydrator::class,
71+
'targetModel' => Doctor::class,
72+
'attribute' => 'doctors'
73+
],
74+
];
75+
}
76+
77+
}
78+
79+
# Country model
80+
81+
use yii\base\Model;
82+
83+
class Country extends Model
84+
{
85+
protected $name;
86+
87+
protected $code;
88+
89+
public function setName($name)
90+
{
91+
$this->name = $name;
92+
}
93+
94+
public function getName()
95+
{
96+
return $this->name;
97+
}
98+
99+
public function setCode($code)
100+
{
101+
$this->code = $code;
102+
}
103+
104+
public function getCode()
105+
{
106+
return $this->code;
107+
}
108+
109+
public function fields() : array
110+
{
111+
return [
112+
'name',
113+
'code'
114+
];
115+
}
116+
}
117+
118+
119+
# Doctor model
120+
121+
use indigerd\embedded\model\Model;
122+
123+
class Doctor extends Model
124+
{
125+
protected $name;
126+
127+
protected $contact;
128+
129+
public function setName($name)
130+
{
131+
$this->name = $name;
132+
}
133+
134+
public function getName()
135+
{
136+
return $this->name;
137+
}
138+
139+
public function setContact(Contact $contact)
140+
{
141+
$this->contact = $contact;
142+
}
143+
144+
public function getContact()
145+
{
146+
return $this->contact;
147+
}
148+
149+
public function fields() : array
150+
{
151+
return [
152+
'name',
153+
'contact'
154+
];
155+
}
156+
157+
public function behaviors() : array
158+
{
159+
return [
160+
[
161+
'class' => HydrateBehavior::class,
162+
'hydrator' => Hydrator::class,
163+
'targetModel' => Contact::class,
164+
'attribute' => 'contact'
165+
],
166+
];
167+
}
168+
}
169+
170+
171+
# Contact model
172+
173+
use yii\base\Model;
174+
175+
class Contact extends Model
176+
{
177+
protected $phone;
178+
179+
protected $email;
180+
181+
public function setPhone($phone)
182+
{
183+
$this->phone = $phone;
184+
}
185+
186+
public function getPhone()
187+
{
188+
return $this->phone;
189+
}
190+
191+
public function setEmail($email)
192+
{
193+
$this->email = $email;
194+
}
195+
196+
public function getEmail()
197+
{
198+
return $this->email;
199+
}
200+
201+
public function fields() : array
202+
{
203+
return [
204+
'phone',
205+
'email'
206+
];
207+
}
208+
}
209+
```

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "indigerd/yii2-embedded-models",
3+
"description": "Behaviors for hydration and extraction of embedded models for yii2 framework",
4+
"keywords": ["library", "embedded model", "hydration", "extraction"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Alexander Stepanenko",
10+
"email": "alex.stepanenko@gmail.com"
11+
}
12+
],
13+
"repositories":[
14+
{
15+
"type": "composer",
16+
"url": "https://asset-packagist.org"
17+
}
18+
],
19+
"require": {
20+
"php": ">=7.2.0",
21+
"yiisoft/yii2": "^2.0.13",
22+
"yiisoft/yii2-mongodb": "^2.1",
23+
"indigerd/hydrator": "*"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "@stable"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"indigerd\\embedded\\": "src"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"indigerd\\embedded\\test\\": "tests"
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)