-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathObjectBuilder.php
6535 lines (5806 loc) · 224 KB
/
ObjectBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Generator\Builder\Om;
use Propel\Common\Util\SetColumnConverter;
use Propel\Generator\Exception\EngineException;
use Propel\Generator\Model\Column;
use Propel\Generator\Model\CrossForeignKeys;
use Propel\Generator\Model\ForeignKey;
use Propel\Generator\Model\IdMethod;
use Propel\Generator\Model\PropelTypes;
use Propel\Generator\Model\Table;
use Propel\Generator\Platform\MssqlPlatform;
use Propel\Generator\Platform\MysqlPlatform;
use Propel\Generator\Platform\OraclePlatform;
use Propel\Generator\Platform\PlatformInterface;
use Propel\Generator\Platform\SqlsrvPlatform;
use Propel\Runtime\Exception\PropelException;
/**
* Generates a PHP5 base Object class for user object model (OM).
*
* This class produces the base object class (e.g. BaseMyTable) which contains
* all the custom-built accessor and setter methods.
*
* @author Hans Lellelid <[email protected]>
*/
class ObjectBuilder extends AbstractObjectBuilder
{
/**
* Returns the package for the base object classes.
*
* @return string
*/
public function getPackage()
{
return parent::getPackage() . ".Base";
}
/**
* Returns the namespace for the base class.
*
* @return string
* @see Propel\Generator\Builder\Om.AbstractOMBuilder::getNamespace()
*/
public function getNamespace()
{
if ($namespace = parent::getNamespace()) {
return $namespace . '\\Base';
}
return 'Base';
}
/**
* Returns default key type.
*
* If not presented in configuration default will be 'TYPE_PHPNAME'
*
* @return string
*/
public function getDefaultKeyType()
{
$defaultKeyType = $this->getBuildProperty('generator.objectModel.defaultKeyType') ? $this->getBuildProperty('generator.objectModel.defaultKeyType') : 'phpName';
return "TYPE_".strtoupper($defaultKeyType);
}
/**
* Returns the name of the current class being built.
*
* @return string
*/
public function getUnprefixedClassName()
{
return $this->getStubObjectBuilder()->getUnprefixedClassName();
}
/**
* Validates the current table to make sure that it won't result in
* generated code that will not parse.
*
* This method may emit warnings for code which may cause problems
* and will throw exceptions for errors that will definitely cause
* problems.
*/
protected function validateModel()
{
parent::validateModel();
$table = $this->getTable();
// Check to see whether any generated foreign key names
// will conflict with column names.
$colPhpNames = [];
$fkPhpNames = [];
foreach ($table->getColumns() as $col) {
$colPhpNames[] = $col->getPhpName();
}
foreach ($table->getForeignKeys() as $fk) {
$fkPhpNames[] = $this->getFKPhpNameAffix($fk, false);
}
$intersect = array_intersect($colPhpNames, $fkPhpNames);
if (!empty($intersect)) {
throw new EngineException("One or more of your column names for [" . $table->getName() . "] table conflict with foreign key names (" . implode(", ", $intersect) . ")");
}
// Check foreign keys to see if there are any foreign keys that
// are also matched with an inversed referencing foreign key
// (this is currently unsupported behavior)
// see: http://propel.phpdb.org/trac/ticket/549
foreach ($table->getForeignKeys() as $fk) {
if ($fk->isMatchedByInverseFK()) {
throw new EngineException(sprintf('The 1:1 relationship expressed by foreign key %s is defined in both directions; Propel does not currently support this (if you must have both foreign key constraints, consider adding this constraint with a custom SQL file.)', $fk->getName()));
}
}
}
/**
* Returns the appropriate formatter (from platform) for a date/time column.
*
* @param Column $column
* @return string
*/
protected function getTemporalFormatter(Column $column)
{
$fmt = null;
if ($column->getType() === PropelTypes::DATE) {
$fmt = $this->getPlatform()->getDateFormatter();
} elseif ($column->getType() === PropelTypes::TIME) {
$fmt = $this->getPlatform()->getTimeFormatter();
} elseif ($column->getType() === PropelTypes::TIMESTAMP) {
$fmt = $this->getPlatform()->getTimestampFormatter();
}
return $fmt;
}
/**
* Returns the type-casted and stringified default value for the specified
* Column. This only works for scalar default values currently.
*
* @param Column $column
* @throws EngineException
* @return string
*/
protected function getDefaultValueString(Column $column)
{
$defaultValue = var_export(null, true);
$val = $column->getPhpDefaultValue();
if (null === $val) {
return $defaultValue;
}
if ($column->isTemporalType()) {
$fmt = $this->getTemporalFormatter($column);
try {
if (!($this->getPlatform() instanceof MysqlPlatform &&
($val === '0000-00-00 00:00:00' || $val === '0000-00-00'))) {
// while technically this is not a default value of NULL,
// this seems to be closest in meaning.
$defDt = new \DateTime($val);
$defaultValue = var_export($defDt->format($fmt), true);
}
} catch (\Exception $exception) {
// prevent endless loop when timezone is undefined
date_default_timezone_set('America/Los_Angeles');
throw new EngineException(sprintf('Unable to parse default temporal value "%s" for column "%s"', $column->getDefaultValueString(), $column->getFullyQualifiedName()), 0, $exception);
}
} elseif ($column->isEnumType()) {
$valueSet = $column->getValueSet();
if (!in_array($val, $valueSet)) {
throw new EngineException(sprintf('Default Value "%s" is not among the enumerated values', $val));
}
$defaultValue = array_search($val, $valueSet);
} elseif ($column->isSetType()) {
$defaultValue = SetColumnConverter::convertToInt($val, $column->getValueSet());
} elseif ($column->isPhpPrimitiveType()) {
settype($val, $column->getPhpType());
$defaultValue = var_export($val, true);
} elseif ($column->isPhpObjectType()) {
$defaultValue = 'new '.$column->getPhpType().'(' . var_export($val, true) . ')';
} elseif ($column->isPhpArrayType()) {
$defaultValue = var_export($val, true);
} else {
throw new EngineException("Cannot get default value string for " . $column->getFullyQualifiedName());
}
return $defaultValue;
}
/**
* Adds class phpdoc comment and opening of class.
*
* @param string &$script
*/
protected function addClassOpen(&$script)
{
$table = $this->getTable();
$tableName = $table->getName();
$tableDesc = $table->getDescription();
if (null !== ($parentClass = $this->getBehaviorContent('parentClass')) ||
null !== ($parentClass = ClassTools::classname($this->getBaseClass()))) {
$parentClass = ' extends '.$parentClass;
}
if ($this->getBuildProperty('generator.objectModel.addClassLevelComment')) {
$script .= "
/**
* Base class that represents a row from the '$tableName' table.
*
* $tableDesc
*";
if ($this->getBuildProperty('generator.objectModel.addTimeStamp')) {
$now = strftime('%c');
$script .= "
* This class was autogenerated by Propel " . $this->getBuildProperty('general.version') . " on:
*
* $now
*";
}
$script .= "
* @package propel.generator.".$this->getPackage()."
*/";
}
$script .= "
abstract class ".$this->getUnqualifiedClassName().$parentClass." implements ActiveRecordInterface ";
if ($interface = $this->getInterface()) {
$script .= ", Child" . ClassTools::classname($interface);
if ($interface !== ClassTools::classname($interface)) {
$this->declareClass($interface);
} else {
$this->declareClassFromBuilder($this->getInterfaceBuilder());
}
}
$script .= "
{";
}
/**
* Specifies the methods that are added as part of the basic OM class.
* This can be overridden by subclasses that wish to add more methods.
*
* @param string &$script
* @see ObjectBuilder::addClassBody()
*/
protected function addClassBody(&$script)
{
$this->declareClassFromBuilder($this->getStubObjectBuilder());
$this->declareClassFromBuilder($this->getStubQueryBuilder());
$this->declareClassFromBuilder($this->getTableMapBuilder());
$this->declareClasses(
'\Exception',
'\PDO',
'\Propel\Runtime\Exception\PropelException',
'\Propel\Runtime\Connection\ConnectionInterface',
'\Propel\Runtime\Collection\Collection',
'\Propel\Runtime\Collection\ObjectCollection',
'\Propel\Runtime\Collection\ObjectCombinationCollection',
'\Propel\Runtime\Exception\BadMethodCallException',
'\Propel\Runtime\Exception\PropelException',
'\Propel\Runtime\ActiveQuery\Criteria',
'\Propel\Runtime\ActiveQuery\ModelCriteria',
'\Propel\Runtime\ActiveRecord\ActiveRecordInterface',
'\Propel\Runtime\Parser\AbstractParser',
'\Propel\Runtime\Propel',
'\Propel\Runtime\Map\TableMap'
);
$baseClass = $this->getBaseClass();
if (strrpos($baseClass, '\\') !== false) {
$this->declareClasses($baseClass);
}
$table = $this->getTable();
if (!$table->isAlias()) {
$this->addConstants($script);
$this->addAttributes($script);
}
if ($table->hasCrossForeignKeys()) {
/* @var $refFK ForeignKey */
foreach ($table->getCrossFks() as $crossFKs) {
$this->addCrossScheduledForDeletionAttribute($script, $crossFKs);
}
}
foreach ($table->getReferrers() as $refFK) {
if (!$refFK->isLocalPrimaryKey()) {
$this->addRefFkScheduledForDeletionAttribute($script, $refFK);
}
}
if ($this->hasDefaultValues()) {
$this->addApplyDefaultValues($script);
}
$this->addConstructor($script);
$this->addBaseObjectMethods($script);
$this->addColumnAccessorMethods($script);
$this->addColumnMutatorMethods($script);
$this->addHasOnlyDefaultValues($script);
$this->addHydrate($script);
$this->addEnsureConsistency($script);
if (!$table->isReadOnly()) {
$this->addManipulationMethods($script);
}
if ($this->isAddGenericAccessors()) {
$this->addGetByName($script);
$this->addGetByPosition($script);
$this->addToArray($script);
}
if ($this->isAddGenericMutators()) {
$this->addSetByName($script);
$this->addSetByPosition($script);
$this->addFromArray($script);
$this->addImportFrom($script);
}
$this->addBuildCriteria($script);
$this->addBuildPkeyCriteria($script);
$this->addHashCode($script);
$this->addGetPrimaryKey($script);
$this->addSetPrimaryKey($script);
$this->addIsPrimaryKeyNull($script);
$this->addCopy($script);
$this->addFKMethods($script);
$this->addRefFKMethods($script);
$this->addCrossFKMethods($script);
$this->addClear($script);
$this->addClearAllReferences($script);
$this->addPrimaryString($script);
// apply behaviors
$this->applyBehaviorModifier('objectMethods', $script, " ");
if ($this->getBuildProperty('generator.objectModel.addHooks')) {
$this->addHookMethods($script);
}
$this->addMagicCall($script);
}
/**
* Closes class.
*
* @param string &$script
*/
protected function addClassClose(&$script)
{
$script .= "
}
";
$this->applyBehaviorModifier('objectFilter', $script, "");
}
/**
* Adds any constants to the class.
*
* @param string &$script
*/
protected function addConstants(&$script)
{
$script .= "
/**
* TableMap class name
*/
const TABLE_MAP = '" . addslashes($this->getTableMapBuilder()->getFullyQualifiedClassName()) . "';
";
}
/**
* Adds class attributes.
*
* @param string &$script
*/
protected function addAttributes(&$script)
{
$table = $this->getTable();
$script .= "
";
$script .= $this->renderTemplate('baseObjectAttributes');
if (!$table->isAlias()) {
$this->addColumnAttributes($script);
}
foreach ($table->getForeignKeys() as $fk) {
$this->addFKAttributes($script, $fk);
}
foreach ($table->getReferrers() as $refFK) {
$this->addRefFKAttributes($script, $refFK);
}
// many-to-many relationships
foreach ($table->getCrossFks() as $crossFKs) {
$this->addCrossFKAttributes($script, $crossFKs);
}
$this->addAlreadyInSaveAttribute($script);
// apply behaviors
$this->applyBehaviorModifier('objectAttributes', $script, " ");
}
/**
* Adds variables that store column values.
*
* @param string &$script
*/
protected function addColumnAttributes(&$script)
{
$table = $this->getTable();
foreach ($table->getColumns() as $col) {
$this->addColumnAttributeComment($script, $col);
$this->addColumnAttributeDeclaration($script, $col);
if ($col->isLazyLoad() ) {
$this->addColumnAttributeLoaderComment($script, $col);
$this->addColumnAttributeLoaderDeclaration($script, $col);
}
if ($col->getType() == PropelTypes::OBJECT || $col->getType() == PropelTypes::PHP_ARRAY) {
$this->addColumnAttributeUnserializedComment($script, $col);
$this->addColumnAttributeUnserializedDeclaration($script, $col);
}
if ($col->isSetType()) {
$this->addColumnAttributeConvertedDeclaration($script, $col);
}
}
}
/**
* Adds comment about the attribute (variable) that stores column values.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeComment(&$script, Column $column)
{
if ($column->isTemporalType()) {
$cptype = $this->getDateTimeClass($column);
} else {
$cptype = $column->getPhpType();
}
$clo = $column->getLowercasedName();
$script .= "
/**
* The value for the $clo field.
* ".$column->getDescription();
if ($column->getDefaultValue()) {
if ($column->getDefaultValue()->isExpression()) {
$script .= "
* Note: this column has a database default value of: (expression) ".$column->getDefaultValue()->getValue();
} else {
$script .= "
* Note: this column has a database default value of: ". $this->getDefaultValueString($column);
}
}
$script .= "
* @var $cptype
*/";
}
/**
* Adds the declaration of a column value storage attribute.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeDeclaration(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
protected \$" . $clo . ";
";
}
/**
* Adds the comment about the attribute keeping track if an attribute value
* has been loaded.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeLoaderComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* Whether the lazy-loaded \$$clo value has been loaded from database.
* This is necessary to avoid repeated lookups if \$$clo column is NULL in the db.
* @var boolean
*/";
}
/**
* Adds the declaration of the attribute keeping track of an attribute
* loaded state.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeLoaderDeclaration(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
protected \$".$clo."_isLoaded = false;
";
}
/**
* Adds the comment about the serialized attribute.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeUnserializedComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* The unserialized \$$clo value - i.e. the persisted object.
* This is necessary to avoid repeated calls to unserialize() at runtime.
* @var object
*/";
}
/**
* Adds the declaration of the serialized attribute.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeUnserializedDeclaration(&$script, Column $column)
{
$clo = $column->getLowercasedName() . "_unserialized";
$script .= "
protected \$" . $clo . ";
";
}
/**
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeConvertedDeclaration(&$script, Column $column)
{
$clo = $column->getLowercasedName() . "_converted";
$script .= "
protected \$" . $clo . ";
";
}
/**
* Adds the constructor for this object.
*
* @param string &$script
*/
protected function addConstructor(&$script)
{
$this->addConstructorComment($script);
$this->addConstructorOpen($script);
if ($this->hasDefaultValues()) {
$this->addConstructorBody($script);
}
$this->addConstructorClose($script);
}
/**
* Adds the comment for the constructor
*
* @param string &$script
*/
protected function addConstructorComment(&$script)
{
$script .= "
/**
* Initializes internal state of ".$this->getQualifiedClassName()." object.";
if ($this->hasDefaultValues()) {
$script .= "
* @see applyDefaults()";
}
$script .= "
*/";
}
/**
* Adds the function declaration for the constructor.
*
* @param string &$script
*/
protected function addConstructorOpen(&$script)
{
$script .= "
public function __construct()
{";
}
/**
* Adds the function body for the constructor.
*
* @param string &$script
*/
protected function addConstructorBody(&$script)
{
$script .= "
\$this->applyDefaultValues();";
}
/**
* Adds the function close for the constructor.
*
* @param string &$script
*/
protected function addConstructorClose(&$script)
{
$script .= "
}
";
}
/**
* Adds the base object functions.
*
* @param string &$script
*/
protected function addBaseObjectMethods(&$script)
{
$script .= $this->renderTemplate('baseObjectMethods', ['className' => $this->getUnqualifiedClassName()]);
}
/**
* Adds the base object hook functions.
*
* @param string &$script
*/
protected function addHookMethods(&$script)
{
$hooks = [];
foreach (['pre', 'post'] as $hook) {
foreach (['Insert', 'Update', 'Save', 'Delete'] as $action) {
$hooks[$hook.$action] = false === strpos($script, "function $hook.$action(");
}
}
$script .= $this->renderTemplate('baseObjectMethodHook', $hooks);
}
/**
* Adds the applyDefaults() method, which is called from the constructor.
*
* @param string &$script
*/
protected function addApplyDefaultValues(&$script)
{
$this->addApplyDefaultValuesComment($script);
$this->addApplyDefaultValuesOpen($script);
$this->addApplyDefaultValuesBody($script);
$this->addApplyDefaultValuesClose($script);
}
/**
* Adds the comment for the applyDefaults method.
*
* @param string &$script
*/
protected function addApplyDefaultValuesComment(&$script)
{
$script .= "
/**
* Applies default values to this object.
* This method should be called from the object's constructor (or
* equivalent initialization method).
* @see __construct()
*/";
}
/**
* Adds the function declaration for the applyDefaults method.
*
* @param string &$script
*/
protected function addApplyDefaultValuesOpen(&$script)
{
$script .= "
public function applyDefaultValues()
{";
}
/**
* Adds the function body of the applyDefault method.
*
* @param string &$script
*/
protected function addApplyDefaultValuesBody(&$script)
{
$table = $this->getTable();
// FIXME - Apply support for PHP default expressions here
// see: http://propel.phpdb.org/trac/ticket/378
$colsWithDefaults = [];
foreach ($table->getColumns() as $column) {
$def = $column->getDefaultValue();
if ($def !== null && !$def->isExpression()) {
$colsWithDefaults[] = $column;
}
}
foreach ($colsWithDefaults as $column) {
/** @var Column $column */
$clo = $column->getLowercasedName();
$defaultValue = $this->getDefaultValueString($column);
if ($column->isTemporalType()) {
$dateTimeClass = $this->getDateTimeClass($column);
$script .= "
\$this->".$clo." = PropelDateTime::newInstance($defaultValue, null, '$dateTimeClass');";
} else {
$script .= "
\$this->".$clo." = $defaultValue;";
}
}
}
/**
* Adds the function close for the applyDefaults method.
*
* @param string &$script
*/
protected function addApplyDefaultValuesClose(&$script)
{
$script .= "
}
";
}
/**
* Adds a date/time/timestamp getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addTemporalAccessor(&$script, Column $column)
{
$this->addTemporalAccessorComment($script, $column);
$this->addTemporalAccessorOpen($script, $column);
$this->addTemporalAccessorBody($script, $column);
$this->addTemporalAccessorClose($script);
}
/**
* Adds the comment for a temporal accessor.
*
* @param string &$script
* @param Column $column
*/
public function addTemporalAccessorComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$dateTimeClass = $this->getDateTimeClass($column);
$handleMysqlDate = false;
if ($this->getPlatform() instanceof MysqlPlatform) {
if ($column->getType() === PropelTypes::TIMESTAMP) {
$handleMysqlDate = true;
$mysqlInvalidDateString = '0000-00-00 00:00:00';
} elseif ($column->getType() === PropelTypes::DATE) {
$handleMysqlDate = true;
$mysqlInvalidDateString = '0000-00-00';
}
// 00:00:00 is a valid time, so no need to check for that.
}
$script .= "
/**
* Get the [optionally formatted] temporal [$clo] column value.
* {$column->getDescription()}
*
* @param string \$format The date/time format string (either date()-style or strftime()-style).
* If format is NULL, then the raw $dateTimeClass object will be returned.
*
* @return string|$dateTimeClass Formatted date/time value as string or $dateTimeClass object (if format is NULL), NULL if column is NULL" .($handleMysqlDate ? ', and 0 if column value is ' . $mysqlInvalidDateString : '')."
*
* @throws PropelException - if unable to parse/validate the date/time value.
*/";
}
/**
* Adds the function declaration for a temporal accessor.
*
* @param string &$script
* @param Column $column
*/
public function addTemporalAccessorOpen(&$script, Column $column)
{
$cfc = $column->getPhpName();
$defaultfmt = null;
$visibility = $column->getAccessorVisibility();
// Default date/time formatter strings are specified in propel config
if ($column->getType() === PropelTypes::DATE) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultDateFormat');
} elseif ($column->getType() === PropelTypes::TIME) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultTimeFormat');
} elseif ($column->getType() === PropelTypes::TIMESTAMP) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultTimeStampFormat');
}
if (empty($defaultfmt)) {
$defaultfmt = null;
}
$script .= "
".$visibility." function get$cfc(\$format = ".var_export($defaultfmt, true)."";
if ($column->isLazyLoad()) {
$script .= ", \$con = null";
}
$script .= ")
{";
}
/**
* Gets accessor lazy loaded snippets.
*
* @param Column $column
* @return string
*/
protected function getAccessorLazyLoadSnippet(Column $column)
{
if ($column->isLazyLoad()) {
$clo = $column->getLowercasedName();
$defaultValueString = 'null';
$def = $column->getDefaultValue();
if ($def !== null && !$def->isExpression()) {
$defaultValueString = $this->getDefaultValueString($column);
}
return "
if (!\$this->{$clo}_isLoaded && \$this->{$clo} === {$defaultValueString} && !\$this->isNew()) {
\$this->load{$column->getPhpName()}(\$con);
}
";
}
return '';
}
/**
* Adds the body of the temporal accessor.
*
* @param string &$script
* @param Column $column
*/
protected function addTemporalAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$dateTimeClass = $this->getDateTimeClass($column);
$this->declareClasses($dateTimeClass);
$defaultfmt = null;
// Default date/time formatter strings are specified in propel config
if ($column->getType() === PropelTypes::DATE) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultDateFormat');
} elseif ($column->getType() === PropelTypes::TIME) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultTimeFormat');
} elseif ($column->getType() === PropelTypes::TIMESTAMP) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultTimeStampFormat');
}
if (empty($defaultfmt)) {
$defaultfmt = null;
}
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$script .= "
if (\$format === null) {
return \$this->$clo;
} else {
return \$this->$clo instanceof \DateTimeInterface ? \$this->{$clo}->format(\$format) : null;
}";
}
/**
* Adds the body of the temporal accessor.
*
* @param string &$script
*/
protected function addTemporalAccessorClose(&$script)
{
$script .= "
}
";
}
/**
* Adds an object getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addObjectAccessor(&$script, Column $column)
{
$this->addDefaultAccessorComment($script, $column);
$this->addDefaultAccessorOpen($script, $column);
$this->addObjectAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Adds the function body for an object accessor method.
*
* @param string &$script
* @param Column $column
*/
protected function addObjectAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$cloUnserialized = $clo.'_unserialized';
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$script .= "
if (null == \$this->$cloUnserialized && is_resource(\$this->$clo)) {
if (\$serialisedString = stream_get_contents(\$this->$clo)) {
\$this->$cloUnserialized = unserialize(\$serialisedString);
}
}
return \$this->$cloUnserialized;";
}
/**
* Adds an array getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addArrayAccessor(&$script, Column $column)
{
$this->addDefaultAccessorComment($script, $column);
$this->addDefaultAccessorOpen($script, $column);
$this->addArrayAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Adds the function body for an array accessor method.
*
* @param string &$script
* @param Column $column
*/
protected function addArrayAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$cloUnserialized = $clo.'_unserialized';
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$script .= "
if (null === \$this->$cloUnserialized) {