1 |
<?php
|
2 |
/*
|
3 |
* $Id: Doctrine.php 3092 2007-11-08 19:01:22Z Jonathan.Wage $
|
4 |
*
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
16 |
*
|
17 |
* This software consists of voluntary contributions made by many individuals
|
18 |
* and is licensed under the LGPL. For more information, see
|
19 |
* <http://www.phpdoctrine.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine
|
24 |
* the base class of Doctrine framework
|
25 |
*
|
26 |
* @package Doctrine
|
27 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
28 |
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
29 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
30 |
* @link www.phpdoctrine.com
|
31 |
* @since 1.0
|
32 |
* @version $Revision: 3092 $
|
33 |
*/
|
34 |
final class Doctrine
|
35 |
{
|
36 |
/**
|
37 |
* VERSION
|
38 |
*/
|
39 |
const VERSION = '0.1.0';
|
40 |
|
41 |
/**
|
42 |
* ERROR CONSTANTS
|
43 |
*/
|
44 |
const ERR = -1;
|
45 |
const ERR_SYNTAX = -2;
|
46 |
const ERR_CONSTRAINT = -3;
|
47 |
const ERR_NOT_FOUND = -4;
|
48 |
const ERR_ALREADY_EXISTS = -5;
|
49 |
const ERR_UNSUPPORTED = -6;
|
50 |
const ERR_MISMATCH = -7;
|
51 |
const ERR_INVALID = -8;
|
52 |
const ERR_NOT_CAPABLE = -9;
|
53 |
const ERR_TRUNCATED = -10;
|
54 |
const ERR_INVALID_NUMBER = -11;
|
55 |
const ERR_INVALID_DATE = -12;
|
56 |
const ERR_DIVZERO = -13;
|
57 |
const ERR_NODBSELECTED = -14;
|
58 |
const ERR_CANNOT_CREATE = -15;
|
59 |
const ERR_CANNOT_DELETE = -16;
|
60 |
const ERR_CANNOT_DROP = -17;
|
61 |
const ERR_NOSUCHTABLE = -18;
|
62 |
const ERR_NOSUCHFIELD = -19;
|
63 |
const ERR_NEED_MORE_DATA = -20;
|
64 |
const ERR_NOT_LOCKED = -21;
|
65 |
const ERR_VALUE_COUNT_ON_ROW = -22;
|
66 |
const ERR_INVALID_DSN = -23;
|
67 |
const ERR_CONNECT_FAILED = -24;
|
68 |
const ERR_EXTENSION_NOT_FOUND = -25;
|
69 |
const ERR_NOSUCHDB = -26;
|
70 |
const ERR_ACCESS_VIOLATION = -27;
|
71 |
const ERR_CANNOT_REPLACE = -28;
|
72 |
const ERR_CONSTRAINT_NOT_NULL = -29;
|
73 |
const ERR_DEADLOCK = -30;
|
74 |
const ERR_CANNOT_ALTER = -31;
|
75 |
const ERR_MANAGER = -32;
|
76 |
const ERR_MANAGER_PARSE = -33;
|
77 |
const ERR_LOADMODULE = -34;
|
78 |
const ERR_INSUFFICIENT_DATA = -35;
|
79 |
const ERR_CLASS_NAME = -36;
|
80 |
|
81 |
/**
|
82 |
* PDO derived constants
|
83 |
*/
|
84 |
const CASE_LOWER = 2;
|
85 |
const CASE_NATURAL = 0;
|
86 |
const CASE_UPPER = 1;
|
87 |
const CURSOR_FWDONLY = 0;
|
88 |
const CURSOR_SCROLL = 1;
|
89 |
const ERRMODE_EXCEPTION = 2;
|
90 |
const ERRMODE_SILENT = 0;
|
91 |
const ERRMODE_WARNING = 1;
|
92 |
const FETCH_ASSOC = 2;
|
93 |
const FETCH_BOTH = 4;
|
94 |
const FETCH_BOUND = 6;
|
95 |
const FETCH_CLASS = 8;
|
96 |
const FETCH_CLASSTYPE = 262144;
|
97 |
const FETCH_COLUMN = 7;
|
98 |
const FETCH_FUNC = 10;
|
99 |
const FETCH_GROUP = 65536;
|
100 |
const FETCH_INTO = 9;
|
101 |
const FETCH_LAZY = 1;
|
102 |
const FETCH_NAMED = 11;
|
103 |
const FETCH_NUM = 3;
|
104 |
const FETCH_OBJ = 5;
|
105 |
const FETCH_ORI_ABS = 4;
|
106 |
const FETCH_ORI_FIRST = 2;
|
107 |
const FETCH_ORI_LAST = 3;
|
108 |
const FETCH_ORI_NEXT = 0;
|
109 |
const FETCH_ORI_PRIOR = 1;
|
110 |
const FETCH_ORI_REL = 5;
|
111 |
const FETCH_SERIALIZE = 524288;
|
112 |
const FETCH_UNIQUE = 196608;
|
113 |
const NULL_EMPTY_STRING = 1;
|
114 |
const NULL_NATURAL = 0;
|
115 |
const NULL_TO_STRING = NULL;
|
116 |
const PARAM_BOOL = 5;
|
117 |
const PARAM_INPUT_OUTPUT = -2147483648;
|
118 |
const PARAM_INT = 1;
|
119 |
const PARAM_LOB = 3;
|
120 |
const PARAM_NULL = 0;
|
121 |
const PARAM_STMT = 4;
|
122 |
const PARAM_STR = 2;
|
123 |
|
124 |
/**
|
125 |
* ATTRIBUTE CONSTANTS
|
126 |
*/
|
127 |
|
128 |
/**
|
129 |
* PDO derived attributes
|
130 |
*/
|
131 |
const ATTR_AUTOCOMMIT = 0;
|
132 |
const ATTR_PREFETCH = 1;
|
133 |
const ATTR_TIMEOUT = 2;
|
134 |
const ATTR_ERRMODE = 3;
|
135 |
const ATTR_SERVER_VERSION = 4;
|
136 |
const ATTR_CLIENT_VERSION = 5;
|
137 |
const ATTR_SERVER_INFO = 6;
|
138 |
const ATTR_CONNECTION_STATUS = 7;
|
139 |
const ATTR_CASE = 8;
|
140 |
const ATTR_CURSOR_NAME = 9;
|
141 |
const ATTR_CURSOR = 10;
|
142 |
const ATTR_ORACLE_NULLS = 11;
|
143 |
const ATTR_PERSISTENT = 12;
|
144 |
const ATTR_STATEMENT_CLASS = 13;
|
145 |
const ATTR_FETCH_TABLE_NAMES = 14;
|
146 |
const ATTR_FETCH_CATALOG_NAMES = 15;
|
147 |
const ATTR_DRIVER_NAME = 16;
|
148 |
const ATTR_STRINGIFY_FETCHES = 17;
|
149 |
const ATTR_MAX_COLUMN_LEN = 18;
|
150 |
|
151 |
/**
|
152 |
* Doctrine constants
|
153 |
*/
|
154 |
const ATTR_LISTENER = 100;
|
155 |
const ATTR_QUOTE_IDENTIFIER = 101;
|
156 |
const ATTR_FIELD_CASE = 102;
|
157 |
const ATTR_IDXNAME_FORMAT = 103;
|
158 |
const ATTR_SEQNAME_FORMAT = 104;
|
159 |
const ATTR_SEQCOL_NAME = 105;
|
160 |
const ATTR_CMPNAME_FORMAT = 118;
|
161 |
const ATTR_DBNAME_FORMAT = 117;
|
162 |
const ATTR_TBLCLASS_FORMAT = 119;
|
163 |
const ATTR_TBLNAME_FORMAT = 120;
|
164 |
const ATTR_EXPORT = 140;
|
165 |
const ATTR_DECIMAL_PLACES = 141;
|
166 |
|
167 |
const ATTR_PORTABILITY = 106;
|
168 |
const ATTR_VALIDATE = 107;
|
169 |
const ATTR_COLL_KEY = 108;
|
170 |
const ATTR_QUERY_LIMIT = 109;
|
171 |
const ATTR_DEFAULT_TABLE_TYPE = 112;
|
172 |
const ATTR_DEF_TEXT_LENGTH = 113;
|
173 |
const ATTR_DEF_VARCHAR_LENGTH = 114;
|
174 |
const ATTR_DEF_TABLESPACE = 115;
|
175 |
const ATTR_EMULATE_DATABASE = 116;
|
176 |
const ATTR_USE_NATIVE_ENUM = 117;
|
177 |
const ATTR_DEFAULT_SEQUENCE = 133;
|
178 |
|
179 |
const ATTR_FETCHMODE = 118;
|
180 |
const ATTR_NAME_PREFIX = 121;
|
181 |
const ATTR_CREATE_TABLES = 122;
|
182 |
const ATTR_COLL_LIMIT = 123;
|
183 |
|
184 |
const ATTR_CACHE = 150;
|
185 |
const ATTR_CACHE_LIFESPAN = 151;
|
186 |
const ATTR_LOAD_REFERENCES = 153;
|
187 |
const ATTR_RECORD_LISTENER = 154;
|
188 |
const ATTR_THROW_EXCEPTIONS = 155;
|
189 |
const ATTR_DEFAULT_PARAM_NAMESPACE = 156;
|
190 |
|
191 |
/**
|
192 |
* LIMIT CONSTANTS
|
193 |
*/
|
194 |
|
195 |
/**
|
196 |
* constant for row limiting
|
197 |
*/
|
198 |
const LIMIT_ROWS = 1;
|
199 |
|
200 |
/**
|
201 |
* constant for record limiting
|
202 |
*/
|
203 |
const LIMIT_RECORDS = 2;
|
204 |
|
205 |
/**
|
206 |
* FETCHMODE CONSTANTS
|
207 |
*/
|
208 |
|
209 |
/**
|
210 |
* IMMEDIATE FETCHING
|
211 |
* mode for immediate fetching
|
212 |
*/
|
213 |
const FETCH_IMMEDIATE = 0;
|
214 |
|
215 |
/**
|
216 |
* BATCH FETCHING
|
217 |
* mode for batch fetching
|
218 |
*/
|
219 |
const FETCH_BATCH = 1;
|
220 |
|
221 |
/**
|
222 |
* LAZY FETCHING
|
223 |
* mode for offset fetching
|
224 |
*/
|
225 |
const FETCH_OFFSET = 3;
|
226 |
|
227 |
/**
|
228 |
* LAZY OFFSET FETCHING
|
229 |
* mode for lazy offset fetching
|
230 |
*/
|
231 |
const FETCH_LAZY_OFFSET = 4;
|
232 |
|
233 |
/**
|
234 |
* FETCH CONSTANTS
|
235 |
*/
|
236 |
|
237 |
|
238 |
/**
|
239 |
* FETCH VALUEHOLDER
|
240 |
*/
|
241 |
const FETCH_VHOLDER = 1;
|
242 |
|
243 |
/**
|
244 |
* FETCH RECORD
|
245 |
*
|
246 |
* Specifies that the fetch method shall return Doctrine_Record
|
247 |
* objects as the elements of the result set.
|
248 |
*
|
249 |
* This is the default fetchmode.
|
250 |
*/
|
251 |
const FETCH_RECORD = 2;
|
252 |
|
253 |
/**
|
254 |
* FETCH ARRAY
|
255 |
*/
|
256 |
const FETCH_ARRAY = 3;
|
257 |
|
258 |
/**
|
259 |
* PORTABILITY CONSTANTS
|
260 |
*/
|
261 |
|
262 |
/**
|
263 |
* Portability: turn off all portability features.
|
264 |
* @see self::ATTR_PORTABILITY
|
265 |
*/
|
266 |
const PORTABILITY_NONE = 0;
|
267 |
|
268 |
/**
|
269 |
* Portability: convert names of tables and fields to case defined in the
|
270 |
* "field_case" option when using the query*(), fetch*() methods.
|
271 |
* @see self::ATTR_PORTABILITY
|
272 |
*/
|
273 |
const PORTABILITY_FIX_CASE = 1;
|
274 |
|
275 |
/**
|
276 |
* Portability: right trim the data output by query*() and fetch*().
|
277 |
* @see self::ATTR_PORTABILITY
|
278 |
*/
|
279 |
const PORTABILITY_RTRIM = 2;
|
280 |
|
281 |
/**
|
282 |
* Portability: force reporting the number of rows deleted.
|
283 |
* @see self::ATTR_PORTABILITY
|
284 |
*/
|
285 |
const PORTABILITY_DELETE_COUNT = 4;
|
286 |
|
287 |
/**
|
288 |
* Portability: convert empty values to null strings in data output by
|
289 |
* query*() and fetch*().
|
290 |
* @see self::ATTR_PORTABILITY
|
291 |
*/
|
292 |
const PORTABILITY_EMPTY_TO_NULL = 8;
|
293 |
|
294 |
/**
|
295 |
* Portability: removes database/table qualifiers from associative indexes
|
296 |
* @see self::ATTR_PORTABILITY
|
297 |
*/
|
298 |
const PORTABILITY_FIX_ASSOC_FIELD_NAMES = 16;
|
299 |
|
300 |
/**
|
301 |
* Portability: makes Doctrine_Expression throw exception for unportable RDBMS expressions
|
302 |
* @see self::ATTR_PORTABILITY
|
303 |
*/
|
304 |
const PORTABILITY_EXPR = 32;
|
305 |
|
306 |
/**
|
307 |
* Portability: turn on all portability features.
|
308 |
* @see self::ATTR_PORTABILITY
|
309 |
*/
|
310 |
const PORTABILITY_ALL = 63;
|
311 |
|
312 |
/**
|
313 |
* LOCKMODE CONSTANTS
|
314 |
*/
|
315 |
|
316 |
/**
|
317 |
* mode for optimistic locking
|
318 |
*/
|
319 |
const LOCK_OPTIMISTIC = 0;
|
320 |
|
321 |
/**
|
322 |
* mode for pessimistic locking
|
323 |
*/
|
324 |
const LOCK_PESSIMISTIC = 1;
|
325 |
|
326 |
/**
|
327 |
* EXPORT CONSTANTS
|
328 |
*/
|
329 |
|
330 |
/**
|
331 |
* EXPORT_NONE
|
332 |
*/
|
333 |
const EXPORT_NONE = 0;
|
334 |
|
335 |
/**
|
336 |
* EXPORT_TABLES
|
337 |
*/
|
338 |
const EXPORT_TABLES = 1;
|
339 |
|
340 |
/**
|
341 |
* EXPORT_CONSTRAINTS
|
342 |
*/
|
343 |
const EXPORT_CONSTRAINTS = 2;
|
344 |
|
345 |
/**
|
346 |
* EXPORT_PLUGINS
|
347 |
*/
|
348 |
const EXPORT_PLUGINS = 4;
|
349 |
|
350 |
/**
|
351 |
* EXPORT_ALL
|
352 |
*/
|
353 |
const EXPORT_ALL = 7;
|
354 |
|
355 |
/**
|
356 |
* HYDRATION CONSTANTS
|
357 |
*/
|
358 |
const HYDRATE_RECORD = 2;
|
359 |
|
360 |
/**
|
361 |
* HYDRATE_ARRAY
|
362 |
*/
|
363 |
const HYDRATE_ARRAY = 3;
|
364 |
|
365 |
/**
|
366 |
* HYDRATE_NONE
|
367 |
*/
|
368 |
const HYDRATE_NONE = 4;
|
369 |
|
370 |
/**
|
371 |
* VALIDATION CONSTANTS
|
372 |
*/
|
373 |
const VALIDATE_NONE = 0;
|
374 |
|
375 |
/**
|
376 |
* VALIDATE_LENGTHS
|
377 |
*/
|
378 |
const VALIDATE_LENGTHS = 1;
|
379 |
|
380 |
/**
|
381 |
* VALIDATE_TYPES
|
382 |
*/
|
383 |
const VALIDATE_TYPES = 2;
|
384 |
|
385 |
/**
|
386 |
* VALIDATE_CONSTRAINTS
|
387 |
*/
|
388 |
const VALIDATE_CONSTRAINTS = 4;
|
389 |
|
390 |
/**
|
391 |
* VALIDATE_ALL
|
392 |
*/
|
393 |
const VALIDATE_ALL = 7;
|
394 |
|
395 |
/**
|
396 |
* IDENTIFIER_AUTOINC
|
397 |
*
|
398 |
* constant for auto_increment identifier
|
399 |
*/
|
400 |
const IDENTIFIER_AUTOINC = 1;
|
401 |
|
402 |
/**
|
403 |
* IDENTIFIER_SEQUENCE
|
404 |
*
|
405 |
* constant for sequence identifier
|
406 |
*/
|
407 |
const IDENTIFIER_SEQUENCE = 2;
|
408 |
|
409 |
/**
|
410 |
* IDENTIFIER_NATURAL
|
411 |
*
|
412 |
* constant for normal identifier
|
413 |
*/
|
414 |
const IDENTIFIER_NATURAL = 3;
|
415 |
|
416 |
/**
|
417 |
* IDENTIFIER_COMPOSITE
|
418 |
*
|
419 |
* constant for composite identifier
|
420 |
*/
|
421 |
const IDENTIFIER_COMPOSITE = 4;
|
422 |
|
423 |
/**
|
424 |
* Path
|
425 |
*
|
426 |
* @var string $path doctrine root directory
|
427 |
*/
|
428 |
private static $_path;
|
429 |
|
430 |
/**
|
431 |
* Debug
|
432 |
*
|
433 |
* Bool true/false
|
434 |
*
|
435 |
* @var boolean $_debug
|
436 |
*/
|
437 |
private static $_debug = false;
|
438 |
|
439 |
/**
|
440 |
* _loadedModels
|
441 |
*
|
442 |
* Array of all the loaded models and the path to each one for autoloading
|
443 |
*
|
444 |
* @var string
|
445 |
*/
|
446 |
private static $_loadedModels = array();
|
447 |
|
448 |
/**
|
449 |
* __construct
|
450 |
*
|
451 |
* @return void
|
452 |
* @throws Doctrine_Exception
|
453 |
*/
|
454 |
public function __construct()
|
455 |
{
|
456 |
throw new Doctrine_Exception('Doctrine is static class. No instances can be created.');
|
457 |
}
|
458 |
|
459 |
/**
|
460 |
* debug
|
461 |
*
|
462 |
* @param string $bool
|
463 |
* @return void
|
464 |
*/
|
465 |
public static function debug($bool = null)
|
466 |
{
|
467 |
if ($bool !== null) {
|
468 |
self::$_debug = (bool) $bool;
|
469 |
}
|
470 |
|
471 |
return self::$_debug;
|
472 |
}
|
473 |
|
474 |
/**
|
475 |
* getPath
|
476 |
* returns the doctrine root
|
477 |
*
|
478 |
* @return string
|
479 |
*/
|
480 |
public static function getPath()
|
481 |
{
|
482 |
if ( ! self::$_path) {
|
483 |
self::$_path = dirname(__FILE__);
|
484 |
}
|
485 |
|
486 |
return self::$_path;
|
487 |
}
|
488 |
|
489 |
/**
|
490 |
* loadModels
|
491 |
*
|
492 |
* Recursively load all models from a directory or array of directories
|
493 |
*
|
494 |
* @param string $directory Path to directory of models or array of directory paths
|
495 |
* @return array $loadedModels
|
496 |
*/
|
497 |
public static function loadModels($directory)
|
498 |
{
|
499 |
if ($directory !== null) {
|
500 |
$manager = Doctrine_Manager::getInstance();
|
501 |
|
502 |
foreach ((array) $directory as $dir) {
|
503 |
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
|
504 |
RecursiveIteratorIterator::LEAVES_ONLY);
|
505 |
|
506 |
foreach ($it as $file) {
|
507 |
$e = explode('.', $file->getFileName());
|
508 |
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
|
509 |
self::$_loadedModels[$e[0]] = $file->getPathName();
|
510 |
}
|
511 |
}
|
512 |
}
|
513 |
}
|
514 |
|
515 |
return self::getLoadedModels(array_keys(self::$_loadedModels));
|
516 |
}
|
517 |
|
518 |
/**
|
519 |
* getLoadedModels
|
520 |
*
|
521 |
* Get all the loaded models, you can provide an array of classes or it will use get_declared_classes()
|
522 |
*
|
523 |
* Will filter through an array of classes and return the Doctrine_Records out of them.
|
524 |
* If you do not specify $classes it will return all of the currently loaded Doctrine_Records
|
525 |
*
|
526 |
* @param $classes Array of classes to filter through, otherwise uses get_declared_classes()
|
527 |
* @return array $loadedModels
|
528 |
*/
|
529 |
public static function getLoadedModels($classes = null)
|
530 |
{
|
531 |
if ($classes === null) {
|
532 |
$classes = get_declared_classes();
|
533 |
$classes = array_merge($classes, array_keys(self::$_loadedModels));
|
534 |
}
|
535 |
|
536 |
$parent = new ReflectionClass('Doctrine_Record');
|
537 |
|
538 |
$loadedModels = array();
|
539 |
|
540 |
foreach ((array) $classes as $name) {
|
541 |
$class = new ReflectionClass($name);
|
542 |
|
543 |
// Skip the following classes
|
544 |
// - abstract classes
|
545 |
// - not a subclass of Doctrine_Record
|
546 |
// - don't have a setTableDefinition method
|
547 |
if ($class->isAbstract() ||
|
548 |
!$class->isSubClassOf($parent) ||
|
549 |
!$class->hasMethod('setTableDefinition')) {
|
550 |
continue;
|
551 |
}
|
552 |
|
553 |
$loadedModels[] = $name;
|
554 |
}
|
555 |
|
556 |
return $loadedModels;
|
557 |
}
|
558 |
|
559 |
/**
|
560 |
* getConnectionByTableName
|
561 |
*
|
562 |
* Get the connection object for a table by the actual table name
|
563 |
*
|
564 |
* @param string $tableName
|
565 |
* @return object Doctrine_Connection
|
566 |
*/
|
567 |
public static function getConnectionByTableName($tableName)
|
568 |
{
|
569 |
$loadedModels = self::getLoadedModels();
|
570 |
|
571 |
foreach ($loadedModels as $name) {
|
572 |
$model = new $name();
|
573 |
$table = $model->getTable();
|
574 |
|
575 |
if ($table->getTableName() == $tableName) {
|
576 |
return $table->getConnection();
|
577 |
}
|
578 |
}
|
579 |
|
580 |
return Doctrine_Manager::connection();
|
581 |
}
|
582 |
|
583 |
/**
|
584 |
* generateModelsFromDb
|
585 |
*
|
586 |
* method for importing existing schema to Doctrine_Record classes
|
587 |
*
|
588 |
* @param string $directory Directory to write your models to
|
589 |
* @param array $databases Array of databases to generate models for
|
590 |
* @return boolean
|
591 |
* @throws Exception
|
592 |
*/
|
593 |
public static function generateModelsFromDb($directory, array $databases = array())
|
594 |
{
|
595 |
return Doctrine_Manager::connection()->import->importSchema($directory, $databases);
|
596 |
}
|
597 |
|
598 |
/**
|
599 |
* generateYamlFromDb
|
600 |
*
|
601 |
* Generates models from database to temporary location then uses those models to generate a yaml schema file.
|
602 |
* This should probably be fixed. We should write something to generate a yaml schema file directly from the database.
|
603 |
*
|
604 |
* @param string $yamlPath Path to write oyur yaml schema file to
|
605 |
* @return void
|
606 |
*/
|
607 |
public static function generateYamlFromDb($yamlPath)
|
608 |
{
|
609 |
$directory = '/tmp/tmp_doctrine_models';
|
610 |
|
611 |
Doctrine::generateModelsFromDb($directory);
|
612 |
|
613 |
$export = new Doctrine_Export_Schema();
|
614 |
|
615 |
$result = $export->exportSchema($yamlPath, 'yml', $directory);
|
616 |
|
617 |
exec('rm -rf ' . $directory);
|
618 |
|
619 |
return $result;
|
620 |
}
|
621 |
|
622 |
/**
|
623 |
* generateModelsFromYaml
|
624 |
*
|
625 |
* Generate a yaml schema file from an existing directory of models
|
626 |
*
|
627 |
* @param string $yamlPath Path to your yaml schema files
|
628 |
* @param string $directory Directory to generate your models in
|
629 |
* @param array $options Array of options to pass to the schema importer
|
630 |
* @return void
|
631 |
*/
|
632 |
public static function generateModelsFromYaml($yamlPath, $directory, $options = array())
|
633 |
{
|
634 |
$import = new Doctrine_Import_Schema();
|
635 |
$import->setOptions($options);
|
636 |
|
637 |
return $import->importSchema($yamlPath, 'yml', $directory);
|
638 |
}
|
639 |
|
640 |
/**
|
641 |
* createTablesFromModels
|
642 |
*
|
643 |
* Creates database tables for the models in the specified directory
|
644 |
*
|
645 |
* @param string $directory Directory containing your models
|
646 |
* @return void
|
647 |
*/
|
648 |
public static function createTablesFromModels($directory = null)
|
649 |
{
|
650 |
return Doctrine_Manager::connection()->export->exportSchema($directory);
|
651 |
}
|
652 |
|
653 |
/**
|
654 |
* createTablesFromArray
|
655 |
*
|
656 |
* Creates database tables for the models in the supplied array
|
657 |
*
|
658 |
* @param array $array An array of models to be exported
|
659 |
* @return void
|
660 |
*/
|
661 |
public static function createTablesFromArray($array)
|
662 |
{
|
663 |
return Doctrine_Manager::connection()->export->exportClasses($array);
|
664 |
}
|
665 |
|
666 |
/**
|
667 |
* generateSqlFromModels
|
668 |
*
|
669 |
* @param string $directory
|
670 |
* @return string $build String of sql queries. One query per line
|
671 |
*/
|
672 |
public static function generateSqlFromModels($directory = null)
|
673 |
{
|
674 |
$sql = Doctrine_Manager::connection()->export->exportSql($directory);
|
675 |
|
676 |
$build = '';
|
677 |
foreach ($sql as $query) {
|
678 |
$build .= $query.";\n";
|
679 |
}
|
680 |
|
681 |
return $build;
|
682 |
}
|
683 |
|
684 |
/**
|
685 |
* generateYamlFromModels
|
686 |
*
|
687 |
* Generate yaml schema file for the models in the specified directory
|
688 |
*
|
689 |
* @param string $yamlPath Path to your yaml schema files
|
690 |
* @param string $directory Directory to generate your models in
|
691 |
* @return void
|
692 |
*/
|
693 |
public static function generateYamlFromModels($yamlPath, $directory)
|
694 |
{
|
695 |
$export = new Doctrine_Export_Schema();
|
696 |
|
697 |
return $export->exportSchema($yamlPath, 'yml', $directory);
|
698 |
}
|
699 |
|
700 |
/**
|
701 |
* createDatabases
|
702 |
*
|
703 |
* Creates databases for connections
|
704 |
*
|
705 |
* @param string $specifiedConnections Array of connections you wish to create the database for
|
706 |
* @return void
|
707 |
*/
|
708 |
public static function createDatabases($specifiedConnections = array())
|
709 |
{
|
710 |
if ( ! is_array($specifiedConnections)) {
|
711 |
$specifiedConnections = (array) $specifiedConnections;
|
712 |
}
|
713 |
|
714 |
$manager = Doctrine_Manager::getInstance();
|
715 |
$connections = $manager->getConnections();
|
716 |
|
717 |
foreach ($connections as $name => $connection) {
|
718 |
if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) {
|
719 |
continue;
|
720 |
}
|
721 |
|
722 |
$info = $manager->parsePdoDsn($connection->getOption('dsn'));
|
723 |
$username = $connection->getOption('username');
|
724 |
$password = $connection->getOption('password');
|
725 |
|
726 |
// Make connection without database specified so we can create it
|
727 |
$connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false);
|
728 |
|
729 |
try {
|
730 |
// Create database
|
731 |
$connect->export->createDatabase($name);
|
732 |
|
733 |
// Close the tmp connection with no database
|
734 |
$manager->closeConnection($connect);
|
735 |
|
736 |
// Close original connection
|
737 |
$manager->closeConnection($connection);
|
738 |
|
739 |
// Reopen original connection with newly created database
|
740 |
$manager->openConnection(new PDO($info['dsn'], $username, $password), $name, true);
|
741 |
} catch (Exception $e) {
|
742 |
|
743 |
}
|
744 |
}
|
745 |
}
|
746 |
|
747 |
/**
|
748 |
* dropDatabases
|
749 |
*
|
750 |
* Drops databases for connections
|
751 |
*
|
752 |
* @param string $specifiedConnections Array of connections you wish to drop the database for
|
753 |
* @return void
|
754 |
*/
|
755 |
public static function dropDatabases($specifiedConnections = array())
|
756 |
{
|
757 |
if ( ! is_array($specifiedConnections)) {
|
758 |
$specifiedConnections = (array) $specifiedConnections;
|
759 |
}
|
760 |
|
761 |
$manager = Doctrine_Manager::getInstance();
|
762 |
|
763 |
$connections = $manager->getConnections();
|
764 |
|
765 |
foreach ($connections as $name => $connection) {
|
766 |
if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) {
|
767 |
continue;
|
768 |
}
|
769 |
|
770 |
try {
|
771 |
$connection->export->dropDatabase($name);
|
772 |
} catch (Exception $e) {
|
773 |
|
774 |
}
|
775 |
}
|
776 |
}
|
777 |
|
778 |
/**
|
779 |
* dumpData
|
780 |
*
|
781 |
* Dump data to a yaml fixtures file
|
782 |
*
|
783 |
* @param string $yamlPath Path to write the yaml data fixtures to
|
784 |
* @param string $individualFiles Whether or not to dump data to individual fixtures files
|
785 |
* @return void
|
786 |
*/
|
787 |
public static function dumpData($yamlPath, $individualFiles = false)
|
788 |
{
|
789 |
$data = new Doctrine_Data();
|
790 |
|
791 |
return $data->exportData($yamlPath, 'yml', array(), $individualFiles);
|
792 |
}
|
793 |
|
794 |
/**
|
795 |
* loadData
|
796 |
*
|
797 |
* Load data from a yaml fixtures file.
|
798 |
* The output of dumpData can be fed to loadData
|
799 |
*
|
800 |
* @param string $yamlPath Path to your yaml data fixtures
|
801 |
* @param string $append Whether or not to append the data
|
802 |
* @return void
|
803 |
*/
|
804 |
public static function loadData($yamlPath, $append = false)
|
805 |
{
|
806 |
$data = new Doctrine_Data();
|
807 |
|
808 |
if ( ! $append) {
|
809 |
$data->purge();
|
810 |
}
|
811 |
|
812 |
return $data->importData($yamlPath, 'yml');
|
813 |
}
|
814 |
|
815 |
/**
|
816 |
* migrate
|
817 |
*
|
818 |
* Migrate database to specified $to version. Migrates from current to latest if you do not specify.
|
819 |
*
|
820 |
* @param string $migrationsPath Path to migrations directory which contains your migration classes
|
821 |
* @param string $to Version you wish to migrate to.
|
822 |
* @return bool true
|
823 |
* @throws new Doctrine_Migration_Exception
|
824 |
*/
|
825 |
public static function migrate($migrationsPath, $to = null)
|
826 |
{
|
827 |
$migration = new Doctrine_Migration($migrationsPath);
|
828 |
|
829 |
return $migration->migrate($to);
|
830 |
}
|
831 |
|
832 |
/**
|
833 |
* generateMigrationClass
|
834 |
*
|
835 |
* Generate new migration class skeleton
|
836 |
*
|
837 |
* @param string $className Name of the Migration class to generate
|
838 |
* @param string $migrationsPath Path to directory which contains your migration classes
|
839 |
*/
|
840 |
public static function generateMigrationClass($className, $migrationsPath)
|
841 |
{
|
842 |
$builder = new Doctrine_Migration_Builder($migrationsPath);
|
843 |
|
844 |
return $builder->generateMigrationClass($className);
|
845 |
}
|
846 |
|
847 |
/**
|
848 |
* generateMigrationsFromDb
|
849 |
*
|
850 |
* @param string $migrationsPath
|
851 |
* @return void
|
852 |
* @throws new Doctrine_Migration_Exception
|
853 |
*/
|
854 |
public static function generateMigrationsFromDb($migrationsPath)
|
855 |
{
|
856 |
$builder = new Doctrine_Migration_Builder($migrationsPath);
|
857 |
|
858 |
return $builder->generateMigrationsFromDb();
|
859 |
}
|
860 |
|
861 |
/**
|
862 |
* generateMigrationsFromModels
|
863 |
*
|
864 |
* @param string $migrationsPath
|
865 |
* @param string $modelsPath
|
866 |
* @return void
|
867 |
*/
|
868 |
public static function generateMigrationsFromModels($migrationsPath, $modelsPath = null)
|
869 |
{
|
870 |
$builder = new Doctrine_Migration_Builder($migrationsPath);
|
871 |
|
872 |
return $builder->generateMigrationsFromModels($modelsPath);
|
873 |
}
|
874 |
|
875 |
/**
|
876 |
* getTable
|
877 |
*
|
878 |
* @param string $tableName
|
879 |
* @return void
|
880 |
*/
|
881 |
public static function getTable($tableName)
|
882 |
{
|
883 |
return Doctrine_Manager::table($tableName);
|
884 |
}
|
885 |
|
886 |
/**
|
887 |
* fileFinder
|
888 |
*
|
889 |
* @param string $type
|
890 |
* @return void
|
891 |
*/
|
892 |
public static function fileFinder($type)
|
893 |
{
|
894 |
return Doctrine_FileFinder::type($type);
|
895 |
}
|
896 |
|
897 |
/**
|
898 |
* compile
|
899 |
*
|
900 |
* method for making a single file of most used doctrine runtime components
|
901 |
* including the compiled file instead of multiple files (in worst
|
902 |
* cases dozens of files) can improve performance by an order of magnitude
|
903 |
*
|
904 |
* @param string $target
|
905 |
* @param array $includedDrivers
|
906 |
* @throws Doctrine_Exception
|
907 |
* @return void
|
908 |
*/
|
909 |
public static function compile($target = null, $includedDrivers = array())
|
910 |
{
|
911 |
return Doctrine_Compiler::compile($target, $includedDrivers);
|
912 |
}
|
913 |
|
914 |
/**
|
915 |
* autoload
|
916 |
*
|
917 |
* simple autoload function
|
918 |
* returns true if the class was loaded, otherwise false
|
919 |
*
|
920 |
* @param string $classname
|
921 |
* @return boolean
|
922 |
*/
|
923 |
public static function autoload($className)
|
924 |
{
|
925 |
if (class_exists($className, false)) {
|
926 |
return false;
|
927 |
}
|
928 |
|
929 |
if ( ! self::$_path) {
|
930 |
self::$_path = dirname(__FILE__);
|
931 |
}
|
932 |
|
933 |
$class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
|
934 |
|
935 |
if (file_exists($class)) {
|
936 |
require_once($class);
|
937 |
|
938 |
return true;
|
939 |
}
|
940 |
|
941 |
$loadedModels = self::$_loadedModels;
|
942 |
|
943 |
if (isset($loadedModels[$className]) && file_exists($loadedModels[$className])) {
|
944 |
require_once($loadedModels[$className]);
|
945 |
|
946 |
return true;
|
947 |
}
|
948 |
|
949 |
return false;
|
950 |
}
|
951 |
|
952 |
/**
|
953 |
* dump
|
954 |
*
|
955 |
* dumps a given variable
|
956 |
*
|
957 |
* @param mixed $var a variable of any type
|
958 |
* @param boolean $output whether to output the content
|
959 |
* @return void|string
|
960 |
*/
|
961 |
public static function dump($var, $output = true)
|
962 |
{
|
963 |
$ret = array();
|
964 |
switch (gettype($var)) {
|
965 |
case 'array':
|
966 |
$ret[] = 'Array(';
|
967 |
foreach ($var as $k => $v) {
|
968 |
$ret[] = $k . ' : ' . self::dump($v, false);
|
969 |
}
|
970 |
$ret[] = ")";
|
971 |
break;
|
972 |
case 'object':
|
973 |
$ret[] = 'Object(' . get_class($var) . ')';
|
974 |
break;
|
975 |
default:
|
976 |
$ret[] = var_export($var, true);
|
977 |
}
|
978 |
if ($output) {
|
979 |
print implode("\n", $ret);
|
980 |
}
|
981 |
return implode("\n", $ret);
|
982 |
}
|
983 |
|
984 |
/**
|
985 |
* tableize
|
986 |
*
|
987 |
* returns table name from class name
|
988 |
*
|
989 |
* @param string $classname
|
990 |
* @return string
|
991 |
*/
|
992 |
public static function tableize($classname)
|
993 |
{
|
994 |
return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $classname));
|
995 |
}
|
996 |
|
997 |
/**
|
998 |
* classify
|
999 |
*
|
1000 |
* returns class name from table name
|
1001 |
*
|
1002 |
* @param string $tablename
|
1003 |
* @return string
|
1004 |
*/
|
1005 |
public static function classify($tablename)
|
1006 |
{
|
1007 |
return preg_replace_callback('~(_?)(_)([\w])~', array("Doctrine", "classifyCallback"), ucfirst($tablename));
|
1008 |
}
|
1009 |
|
1010 |
/**
|
1011 |
* classifyCallback
|
1012 |
*
|
1013 |
* Callback function to classify a classname properly.
|
1014 |
*
|
1015 |
* @param array $matches An array of matches from a pcre_replace call
|
1016 |
* @return string A string with matches 1 and mathces 3 in upper case.
|
1017 |
*/
|
1018 |
public static function classifyCallback($matches)
|
1019 |
{
|
1020 |
return $matches[1] . strtoupper($matches[3]);
|
1021 |
}
|
1022 |
|
1023 |
/**
|
1024 |
* isValidClassName
|
1025 |
*
|
1026 |
* checks for valid class name (uses camel case and underscores)
|
1027 |
*
|
1028 |
* @param string $classname
|
1029 |
* @return boolean
|
1030 |
*/
|
1031 |
public static function isValidClassname($classname)
|
1032 |
{
|
1033 |
if (preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $classname)) {
|
1034 |
return false;
|
1035 |
}
|
1036 |
|
1037 |
return true;
|
1038 |
}
|
1039 |
|
1040 |
/**
|
1041 |
* makeDirectories
|
1042 |
*
|
1043 |
* Makes the directories for a path recursively
|
1044 |
*
|
1045 |
* @param string $path
|
1046 |
* @return void
|
1047 |
*/
|
1048 |
public static function makeDirectories($path, $mode = 0777)
|
1049 |
{
|
1050 |
if (is_dir($path) || is_file($path)) {
|
1051 |
return true;
|
1052 |
}
|
1053 |
|
1054 |
return mkdir($path, $mode, true);
|
1055 |
}
|
1056 |
}
|