Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 207 lines (163 sloc) 6.109 kB
1bfb208 @mariano README updates
authored
1 li3\_doctrine2 offers integration between [the most RAD PHP framework] [lithium]
b7bce75 @mariano Adding README
authored
2 and possibly the best PHP 5.3 ORM out there: [Doctrine2] [doctrine2]
3
4 # License #
5
1bfb208 @mariano README updates
authored
6 li3\_doctrine2 is released under the [BSD License] [license].
b7bce75 @mariano Adding README
authored
7
8 # Installation #
9
1bfb208 @mariano README updates
authored
10 It is recommended that you install li3\_doctrine2 as a GIT submodule, in order
b7bce75 @mariano Adding README
authored
11 to keep up with the latest upgrades. To do so, switch to the core directory
12 holding your lithium application, and do:
13
14 ```bash
540750d @mariano Fixing repo URL
authored
15 $ git submodule add https://github.com/mariano/li3_doctrine2.git app/libraries/li3_doctrine2
b7bce75 @mariano Adding README
authored
16 $ cd app/libraries/li3_doctrine2 && git submodule update --init
17 $ cd _source/doctrine2 && git submodule update --init
18 ```
19
1bfb208 @mariano README updates
authored
20 # Usage #
21
b9bef23 @mariano README updates
authored
22 ## Adding the li3\_doctrine2 library ##
1bfb208 @mariano README updates
authored
23
24 Once you have downloaded li3_doctrine2 and placed it in your `app/libraries`,
25 or your main `libraries` folder, you need to enable it by placing the following
26 at the end of your `app/config/bootstrap/libraries.php` file:
27
28 ```php
29 Libraries::add('li3_doctrine2');
30 ```
31
32 ## Defining a connection ##
33
b9bef23 @mariano README updates
authored
34 Setting up a connection with li3\_doctrine2 is easy. All you need to do is
35 add the following to your `app/config/bootstrap/connections.php` file (make
36 sure to edit the settings to match your host, without altering the `type`
37 setting):
38
39 ```php
40 Connections::add('default', array(
41 'type' => 'Doctrine',
42 'driver' => 'pdo_mysql',
43 'host' => 'localhost',
44 'user' => 'root',
45 'password' => 'password',
46 'dbname' => 'kontractor'
47 ));
48 ```
1bfb208 @mariano README updates
authored
49
50 ## Creating models ##
51
52 When looking to create your doctrine models, you have two choices: you can
53 have them follow your custom class hierarchy (or not at all), or you could
54 have them extend from the `BaseEntity` class provided by this library. The
b9bef23 @mariano README updates
authored
55 advantage of choosing the later is that your models will have lithium's
56 validation support, and can be better integrated with the custom adapters (such
57 as for session management, or for authorization) provided by this library.
58
59 Let us create a `User` model. Following doctrine's [basic mapping guide]
72f1e8b @mariano Documentation updates
authored
60 [doctrine-mapping-guide] we'll use annotations to define the properties, and
61 we will also include lithium validation rules (that's why we are choosing to
62 extend this model from `BaseEntity`):
b9bef23 @mariano README updates
authored
63
64 ```php
72f1e8b @mariano Documentation updates
authored
65 <?php
66 namespace app\models;
67
68 use Doctrine\ORM\Mapping\Column;
69 use Doctrine\ORM\Mapping\Entity;
70 use Doctrine\ORM\Mapping\GeneratedValue;
71 use Doctrine\ORM\Mapping\Id;
72 use Doctrine\ORM\Mapping\Table;
73 use lithium\security\Password;
74
75 /**
76 * @Entity
77 * @Table(name="users")
78 */
79 class User extends \li3_doctrine2\models\BaseEntity {
80 /**
81 * @Id
82 * @GeneratedValue
83 * @Column(type="integer")
84 */
85 protected $id;
86
87 /**
88 * @Column(type="string",unique=true)
89 */
90 protected $email;
91
92 /**
93 * @Column(type="text")
94 */
95 protected $password;
96
97 /**
98 * @Column(type="string")
99 */
100 protected $name;
101
102 /**
103 * Validation rules
104 */
105 protected $validates = array(
106 'email' => array(
107 'required' => array('notEmpty', 'message' => 'Email is required'),
108 'valid' => array('email', 'message' => 'You must specify a valid email address', 'skipEmpty' => true)
109 ),
110 'password' => array('notEmpty', 'message' => 'Password must not be blank'),
111 'name' => array('notEmpty', 'message' => 'Please provide your full name')
112
113 );
114
115 public function getId() {
116 return $this->id;
117 }
118
119 public function getEmail() {
120 return $this->email;
121 }
122
123 public function setEmail($email) {
124 $this->email = $email;
125 }
126
127 public function setPassword($password) {
128 $this->password = !empty($password) ? Password::hash($password) : null;
129 }
130
131 public function getName() {
132 return $this->name;
133 }
134
135 public function setName($name) {
136 $this->name = $name;
137 }
138
139 public function isActivated() {
140 return $this->activated;
141 }
142
143 public function setActivated($activated) {
144 $this->activated = $activated;
145 }
146
147 public function getCreated() {
148 return $this->created;
149 }
150 }
151 ?>
b9bef23 @mariano README updates
authored
152 ```
1bfb208 @mariano README updates
authored
153
154 # Integrating Doctrine libraries #
155
156 In this section I'll cover some of the doctrine extension libraries out there,
157 and how to integrate them with li3_doctrine2.
158
159 ## DoctrineExtensions ##
b7bce75 @mariano Adding README
authored
160
161 If there is one tool I would recommend you checkout for your Doctrine models,
e811e60 @mariano Fixing link reference in README
authored
162 that would be [DoctrineExtensions] [DoctrineExtensions]. It provides with a set
b7bce75 @mariano Adding README
authored
163 of behavioral extensions to the Doctrine core that will simplify your
164 development.
165
166 To use DoctrineExtensions, you should first add it as GIT submodule. To do so,
167 switch to the core directory holding your lithium application, and do:
168
169 ```bash
170 $ git submodule add https://github.com/l3pp4rd/DoctrineExtensions.git app/libraries/_source/DoctrineExtensions
171 ```
172
173 Next you would use your connection configuration (in `app/config/connections.php`)
174 to configure Doctrine with your desired behaviors. For example, if you wish
175 to use Timestampable and Sluggable, you would first add the library in
176 `app/config/libraries.php`:
177
178 ```php
179 Libraries::add('Gedmo', array(
1bfb208 @mariano README updates
authored
180 'path' => LITHIUM_APP_PATH . '/libraries/_source/DoctrineExtensions/lib/Gedmo'
b7bce75 @mariano Adding README
authored
181 ));
182 ```
183
a165763 @mariano Making the creation of entity manager filterable
authored
184 And then you would filter the `createEntityManager` method in the `Doctrine`
185 datasource to add the behaviors. Edit your `app/config/connections.php` file
186 and add the following right below the connection definition:
b7bce75 @mariano Adding README
authored
187
188 ```php
a165763 @mariano Making the creation of entity manager filterable
authored
189 Connections::get('default')->applyFilter('createEntityManager',
190 function($self, $params, $chain) {
191 $params['eventManager']->addEventSubscriber(
192 new \Gedmo\Timestampable\TimestampableListener()
b7bce75 @mariano Adding README
authored
193 );
a165763 @mariano Making the creation of entity manager filterable
authored
194 $params['eventManager']->addEventSubscriber(
195 new \Gedmo\Sluggable\SluggableListener()
196 );
197 return $chain->next($self, $params, $chain);
b7bce75 @mariano Adding README
authored
198 }
a165763 @mariano Making the creation of entity manager filterable
authored
199 );
b7bce75 @mariano Adding README
authored
200 ```
201
202 [lithium]: http://lithify.me
203 [doctrine2]: http://www.doctrine-project.org
204 [license]: http://www.opensource.org/licenses/bsd-license.php
205 [DoctrineExtensions]: https://github.com/l3pp4rd/DoctrineExtensions
b9bef23 @mariano README updates
authored
206 [doctrine-mapping-guide]: http://www.doctrine-project.org/docs/orm/2.1/en/reference/basic-mapping.html
Something went wrong with that request. Please try again.