This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rb.php
9831 lines (8603 loc) · 255 KB
/
rb.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
namespace RedBeanPHP {
/**
* RedBean Logging interface.
* Provides a uniform and convenient logging
* interface throughout RedBeanPHP.
*
* @file RedBean/Logging.php
* @desc Logging interface for RedBeanPHP ORM
* @author Gabor de Mooij and the RedBeanPHP Community
* @license BSD/GPLv2
*
* copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
interface Logger
{
/**
* A logger (for\PDO or OCI driver) needs to implement the log method.
* The log method will receive logging data. Note that the number of parameters is 0, this means
* all parameters are optional and the number may vary. This way the logger can be used in a very
* flexible way. Sometimes the logger is used to log a simple error message and in other
* situations sql and bindings are passed.
* The log method should be able to accept all kinds of parameters and data by using
* functions like func_num_args/func_get_args.
*
* @return void
*/
public function log();
}
}
namespace RedBeanPHP\Logger {
use RedBeanPHP\Logger as Logger;
use RedBeanPHP\RedException as RedException;
use RedBeanPHP\RedException\Security as Security;
/**
* Logger. Provides a basic logging function for RedBeanPHP.
*
* @file RedBean/Logger.php
* @desc Logger
* @author Gabor de Mooij and the RedBeanPHP Community
* @license BSD/GPLv2
*
* Provides a basic logging function for RedBeanPHP.
*
* copyright (c) G.J.G.T. (Gabor) de Mooij
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class RDefault implements Logger
{
/**
* @var integer
*/
protected $mode = 0;
/**
* @var array
*/
protected $logs = array();
/**
* Default logger method logging to STDOUT.
* This is the default/reference implementation of a logger.
* This method will write the message value to STDOUT (screen).
*
* @param $message (optional)
*
* @return void
*/
public function log()
{
if ( func_num_args() < 1 ) return;
foreach ( func_get_args() as $argument ) {
if ( is_array( $argument ) ) {
$log = print_r( $argument, TRUE );
if ( $this->mode === 0 ) {
echo $log;
} else {
$this->logs[] = $log;
}
} else {
if ( $this->mode === 0 ) {
echo $argument;
} else {
$this->logs[] = $argument;
}
}
if ($this->mode === 0) echo "<br>\n";
}
}
/**
* Returns the logs array.
*
* @return array
*/
public function getLogs()
{
return $this->logs;
}
/**
* Empties the logs array.
*
* @return self
*/
public function clear()
{
$this->logs = array();
return $this;
}
/**
* Selects a logging mode.
* Mode 0 means echoing all statements, while mode 1
* means populating the logs array.
*
* @param integer $mode mode
*
* @return self
*/
public function setMode( $mode )
{
if ($mode !== 0 && $mode !== 1) {
throw new RedException( 'Invalid mode selected for logger, use 1 or 0.' );
}
$this->mode = $mode;
return $this;
}
/**
* Searches for all log entries in internal log array
* for $needle and returns those entries.
*
* @param string $needle needle
*
* @return array
*/
public function grep( $needle )
{
$found = array();
foreach( $this->logs as $logEntry ) {
if (strpos( $logEntry, $needle ) !== false) $found[] = $logEntry;
}
return $found;
}
}
}
namespace RedBeanPHP {
/**
* Interface for database drivers
*
* @file RedBean/Driver.php
* @desc Describes the API for database classes
* @author Gabor de Mooij and the RedBeanPHP Community
* @license BSD/GPLv2
*
* The Driver API conforms to the ADODB pseudo standard
* for database drivers.
*
* copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
interface Driver
{
/**
* Runs a query and fetches results as a multi dimensional array.
*
* @param string $sql SQL to be executed
* @param array $bindings list of values to bind to SQL snippet
*
* @return array
*/
public function GetAll( $sql, $bindings = array() );
/**
* Runs a query and fetches results as a column.
*
* @param string $sql SQL Code to execute
* @param array $bindings list of values to bind to SQL snippet
*
* @return array
*/
public function GetCol( $sql, $bindings = array() );
/**
* Runs a query and returns results as a single cell.
*
* @param string $sql SQL to execute
* @param array $bindings list of values to bind to SQL snippet
*
* @return mixed
*/
public function GetCell( $sql, $bindings = array() );
/**
* Runs a query and returns results as an associative array
* indexed by the first column.
*
* @param string $sql SQL to execute
* @param array $bindings list of values to bind to SQL snippet
*
* @return mixed
*/
public function GetAssocRow( $sql, $bindings = array() );
/**
* Runs a query and returns a flat array containing the values of
* one row.
*
* @param string $sql SQL to execute
* @param array $bindings list of values to bind to SQL snippet
*
* @return array
*/
public function GetRow( $sql, $bindings = array() );
/**
* Executes SQL code and allows key-value binding.
* This function allows you to provide an array with values to bind
* to query parameters. For instance you can bind values to question
* marks in the query. Each value in the array corresponds to the
* question mark in the query that matches the position of the value in the
* array. You can also bind values using explicit keys, for instance
* array(":key"=>123) will bind the integer 123 to the key :key in the
* SQL. This method has no return value.
*
* @param string $sql SQL Code to execute
* @param array $bindings list of values to bind to SQL snippet
*
* @return array Affected Rows
*/
public function Execute( $sql, $bindings = array() );
/**
* Returns the latest insert ID if driver does support this
* feature.
*
* @return integer
*/
public function GetInsertID();
/**
* Returns the number of rows affected by the most recent query
* if the currently selected driver driver supports this feature.
*
* @return integer
*/
public function Affected_Rows();
/**
* Toggles debug mode. In debug mode the driver will print all
* SQL to the screen together with some information about the
* results. All SQL code that passes through the driver will be
* passes on to the screen for inspection.
* This method has no return value.
*
* @param boolean $trueFalse turn on/off
*
* @return void
*/
public function setDebugMode( $tf );
/**
* Starts a transaction.
*
* @return void
*/
public function CommitTrans();
/**
* Commits a transaction.
*
* @return void
*/
public function StartTrans();
/**
* Rolls back a transaction.
*
* @return void
*/
public function FailTrans();
}
}
namespace RedBeanPHP\Driver {
use RedBeanPHP\Driver as Driver;
use RedBeanPHP\Logger as Logger;
use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter;
use RedBeanPHP\RedException\SQL as SQL;
use RedBeanPHP\Logger\RDefault as RDefault;
use RedBeanPHP\PDOCompatible as PDOCompatible;
/**
*\PDO Driver
* This Driver implements the RedBean Driver API
*
* @file RedBean/PDO.php
* @desc \PDO Driver
* @author Gabor de Mooij and the RedBeanPHP Community, Desfrenes
* @license BSD/GPLv2
*
* (c) copyright Desfrenes & Gabor de Mooij and the RedBeanPHP community
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class RPDO implements Driver
{
/**
* @var integer
*/
protected $max;
/**
* @var string
*/
protected $dsn;
/**
* @var boolean
*/
protected $debug = FALSE;
/**
* @var Logger
*/
protected $logger = NULL;
/**
* @var\PDO
*/
protected $pdo;
/**
* @var integer
*/
protected $affectedRows;
/**
* @var integer
*/
protected $resultArray;
/**
* @var array
*/
protected $connectInfo = array();
/**
* @var boolean
*/
protected $isConnected = FALSE;
/**
* @var bool
*/
protected $flagUseStringOnlyBinding = FALSE;
/**
* @var string
*/
protected $mysqlEncoding = '';
/**
* Binds parameters. This method binds parameters to a\PDOStatement for
* Query Execution. This method binds parameters as NULL, INTEGER or STRING
* and supports both named keys and question mark keys.
*
* @param \PDOStatement $statement \PDO Statement instance
* @param array $bindings values that need to get bound to the statement
*
* @return void
*/
protected function bindParams( $statement, $bindings )
{
foreach ( $bindings as $key => &$value ) {
if ( is_integer( $key ) ) {
if ( is_null( $value ) ) {
$statement->bindValue( $key + 1, NULL,\PDO::PARAM_NULL );
} elseif ( !$this->flagUseStringOnlyBinding && AQueryWriter::canBeTreatedAsInt( $value ) && $value <= $this->max ) {
$statement->bindParam( $key + 1, $value,\PDO::PARAM_INT );
} else {
$statement->bindParam( $key + 1, $value,\PDO::PARAM_STR );
}
} else {
if ( is_null( $value ) ) {
$statement->bindValue( $key, NULL,\PDO::PARAM_NULL );
} elseif ( !$this->flagUseStringOnlyBinding && AQueryWriter::canBeTreatedAsInt( $value ) && $value <= $this->max ) {
$statement->bindParam( $key, $value,\PDO::PARAM_INT );
} else {
$statement->bindParam( $key, $value,\PDO::PARAM_STR );
}
}
}
}
/**
* This method runs the actual SQL query and binds a list of parameters to the query.
* slots. The result of the query will be stored in the protected property
* $rs (always array). The number of rows affected (result of rowcount, if supported by database)
* is stored in protected property $affectedRows. If the debug flag is set
* this function will send debugging output to screen buffer.
*
* @param string $sql the SQL string to be send to database server
* @param array $bindings the values that need to get bound to the query slots
*
* @return void
*
* @throws SQL
*/
protected function runQuery( $sql, $bindings, $options = array() )
{
$this->connect();
if ( $this->debug && $this->logger ) {
$this->logger->log( $sql, $bindings );
}
try {
if ( strpos( 'pgsql', $this->dsn ) === 0 ) {
$statement = $this->pdo->prepare( $sql, array(\PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => TRUE ) );
} else {
$statement = $this->pdo->prepare( $sql );
}
$this->bindParams( $statement, $bindings );
$statement->execute();
$this->affectedRows = $statement->rowCount();
if ( $statement->columnCount() ) {
$fetchStyle = ( isset( $options['fetchStyle'] ) ) ? $options['fetchStyle'] : NULL;
$this->resultArray = $statement->fetchAll( $fetchStyle );
if ( $this->debug && $this->logger ) {
$this->logger->log( 'resultset: ' . count( $this->resultArray ) . ' rows' );
}
} else {
$this->resultArray = array();
}
} catch (\PDOException $e ) {
//Unfortunately the code field is supposed to be int by default (php)
//So we need a property to convey the SQL State code.
$err = $e->getMessage();
if ( $this->debug && $this->logger ) $this->logger->log( 'An error occurred: ' . $err );
$exception = new SQL( $err, 0 );
$exception->setSQLState( $e->getCode() );
throw $exception;
}
}
/**
* Try to fix MySQL character encoding problems.
* MySQL < 5.5 does not support proper 4 byte unicode but they
* seem to have added it with version 5.5 under a different label: utf8mb4.
* We try to select the best possible charset based on your version data.
*/
protected function setEncoding()
{
$driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME );
$version = floatval( $this->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION ) );
if ($driver === 'mysql') {
$encoding = ($version >= 5.5) ? 'utf8mb4' : 'utf8';
$this->pdo->setAttribute(\PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES '.$encoding ); //on every re-connect
$this->pdo->exec(' SET NAMES '. $encoding); //also for current connection
$this->mysqlEncoding = $encoding;
}
}
/**
* Returns the best possible encoding for MySQL based on version data.
*
* @return string
*/
public function getMysqlEncoding()
{
return $this->mysqlEncoding;
}
/**
* Constructor. You may either specify dsn, user and password or
* just give an existing\PDO connection.
* Examples:
* $driver = new RPDO($dsn, $user, $password);
* $driver = new RPDO($existingConnection);
*
* @param string|object $dsn database connection string
* @param string $user optional, usename to sign in
* @param string $pass optional, password for connection login
*
*/
public function __construct( $dsn, $user = NULL, $pass = NULL )
{
if ( is_object( $dsn ) ) {
$this->pdo = $dsn;
$this->isConnected = TRUE;
$this->setEncoding();
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION );
$this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE,\PDO::FETCH_ASSOC );
// make sure that the dsn at least contains the type
$this->dsn = $this->getDatabaseType();
} else {
$this->dsn = $dsn;
$this->connectInfo = array( 'pass' => $pass, 'user' => $user );
}
//PHP 5.3 PDO SQLite has a bug with large numbers:
if ( strpos( $this->dsn, 'sqlite' ) === 0 && PHP_MAJOR_VERSION === 5 && PHP_MINOR_VERSION === 3) {
$this->max = 2147483647; //otherwise you get -2147483648 ?! demonstrated in build #603 on Travis.
} else {
$this->max = PHP_INT_MAX; //the normal value of course (makes it possible to use large numbers in LIMIT clause)
}
}
/**
* Whether to bind all parameters as strings.
*
* @param boolean $yesNo pass TRUE to bind all parameters as strings.
*
* @return void
*/
public function setUseStringOnlyBinding( $yesNo )
{
$this->flagUseStringOnlyBinding = (boolean) $yesNo;
}
/**
* Establishes a connection to the database using PHP\PDO
* functionality. If a connection has already been established this
* method will simply return directly. This method also turns on
* UTF8 for the database and\PDO-ERRMODE-EXCEPTION as well as
*\PDO-FETCH-ASSOC.
*
* @throws\PDOException
*
* @return void
*/
public function connect()
{
if ( $this->isConnected ) return;
try {
$user = $this->connectInfo['user'];
$pass = $this->connectInfo['pass'];
$this->pdo = new\PDO(
$this->dsn,
$user,
$pass,
array(\PDO::ATTR_ERRMODE =>\PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE =>\PDO::FETCH_ASSOC,
)
);
$this->setEncoding();
$this->pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, TRUE );
$this->isConnected = TRUE;
} catch (\PDOException $exception ) {
$matches = array();
$dbname = ( preg_match( '/dbname=(\w+)/', $this->dsn, $matches ) ) ? $matches[1] : '?';
throw new\PDOException( 'Could not connect to database (' . $dbname . ').', $exception->getCode() );
}
}
/**
* @see Driver::GetAll
*/
public function GetAll( $sql, $bindings = array() )
{
$this->runQuery( $sql, $bindings );
return $this->resultArray;
}
/**
* @see Driver::GetAssocRow
*/
public function GetAssocRow( $sql, $bindings = array() )
{
$this->runQuery( $sql, $bindings, array(
'fetchStyle' => \PDO::FETCH_ASSOC
)
);
return $this->resultArray;
}
/**
* @see Driver::GetCol
*/
public function GetCol( $sql, $bindings = array() )
{
$rows = $this->GetAll( $sql, $bindings );
$cols = array();
if ( $rows && is_array( $rows ) && count( $rows ) > 0 ) {
foreach ( $rows as $row ) {
$cols[] = array_shift( $row );
}
}
return $cols;
}
/**
* @see Driver::GetCell
*/
public function GetCell( $sql, $bindings = array() )
{
$arr = $this->GetAll( $sql, $bindings );
$row1 = array_shift( $arr );
$col1 = array_shift( $row1 );
return $col1;
}
/**
* @see Driver::GetRow
*/
public function GetRow( $sql, $bindings = array() )
{
$arr = $this->GetAll( $sql, $bindings );
return array_shift( $arr );
}
/**
* @see Driver::Excecute
*/
public function Execute( $sql, $bindings = array() )
{
$this->runQuery( $sql, $bindings );
return $this->affectedRows;
}
/**
* @see Driver::GetInsertID
*/
public function GetInsertID()
{
$this->connect();
return (int) $this->pdo->lastInsertId();
}
/**
* @see Driver::Affected_Rows
*/
public function Affected_Rows()
{
$this->connect();
return (int) $this->affectedRows;
}
/**
* Toggles debug mode. In debug mode the driver will print all
* SQL to the screen together with some information about the
* results.
*
* @param boolean $trueFalse turn on/off
* @param Logger $logger logger instance
*
* @return void
*/
public function setDebugMode( $tf, $logger = NULL )
{
$this->connect();
$this->debug = (bool) $tf;
if ( $this->debug and !$logger ) {
$logger = new RDefault();
}
$this->setLogger( $logger );
}
/**
* Injects Logger object.
* Sets the logger instance you wish to use.
*
* @param Logger $logger the logger instance to be used for logging
*/
public function setLogger( Logger $logger )
{
$this->logger = $logger;
}
/**
* Gets Logger object.
* Returns the currently active Logger instance.
*
* @return Logger
*/
public function getLogger()
{
return $this->logger;
}
/**
* @see Driver::StartTrans
*/
public function StartTrans()
{
$this->connect();
$this->pdo->beginTransaction();
}
/**
* @see Driver::CommitTrans
*/
public function CommitTrans()
{
$this->connect();
$this->pdo->commit();
}
/**
* @see Driver::FailTrans
*/
public function FailTrans()
{
$this->connect();
$this->pdo->rollback();
}
/**
* Returns the name of database driver for\PDO.
* Uses the\PDO attribute DRIVER NAME to obtain the name of the
*\PDO driver.
*
* @return string
*/
public function getDatabaseType()
{
$this->connect();
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME );
}
/**
* Returns the version number of the database.
*
* @return mixed $version version number of the database
*/
public function getDatabaseVersion()
{
$this->connect();
return $this->pdo->getAttribute(\PDO::ATTR_CLIENT_VERSION );
}
/**
* Returns the underlying PHP\PDO instance.
*
* @return\PDO
*/
public function getPDO()
{
$this->connect();
return $this->pdo;
}
/**
* Closes database connection by destructing\PDO.
*
* @return void
*/
public function close()
{
$this->pdo = NULL;
$this->isConnected = FALSE;
}
/**
* Returns TRUE if the current\PDO instance is connected.
*
* @return boolean
*/
public function isConnected()
{
return $this->isConnected && $this->pdo;
}
}
}
namespace RedBeanPHP {
use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter;
use RedBeanPHP\BeanHelper as BeanHelper;
use RedBeanPHP\RedException\Security as Security;
/**
* OODBBean (Object Oriented DataBase Bean)
*
* @file RedBean/OODBBean.php
* @desc The Bean class used for passing information
* @author Gabor de Mooij and the RedBeanPHP community
* @license BSD/GPLv2
*
* copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class OODBBean implements\IteratorAggregate,\ArrayAccess,\Countable
{
/**
* Whether to skip beautification of columns or not.
*
* @var boolean
*/
private $flagSkipBeau = FALSE;
/**
* This is where the real properties of the bean live. They are stored and retrieved
* by the magic getter and setter (__get and __set).
*
* @var array $properties
*/
private $properties = array();
/**
* Here we keep the meta data of a bean.
*
* @var array
*/
private $__info = array();
/**
* The BeanHelper allows the bean to access the toolbox objects to implement
* rich functionality, otherwise you would have to do everything with R or
* external objects.
*
* @var BeanHelper
*/
private $beanHelper = NULL;
/**
* @var null
*/
private $fetchType = NULL;
/**
* @var string
*/
private $withSql = '';
/**
* @var array
*/
private $withParams = array();
/**
* @var string
*/
private $aliasName = NULL;
/**
* @var string
*/
private $via = NULL;
/**
* @var boolean
*/
private $noLoad = FALSE;
/**
* @var boolean
*/
private $all = FALSE;
/** Returns the alias for a type
*
* @param string $type type
*
* @return string $type type
*/
private function getAlias( $type )
{
if ( $this->fetchType ) {
$type = $this->fetchType;
$this->fetchType = NULL;
}
return $type;
}
/**
* Internal method.
* Obtains a shared list for a certain type.
*
* @param string $type the name of the list you want to retrieve.
*
* @return array
*/
private function getSharedList( $type, $redbean, $toolbox )
{
$writer = $toolbox->getWriter();
if ( $this->via ) {
$oldName = $writer->getAssocTable( array( $this->__info['type'], $type ) );
if ( $oldName !== $this->via ) {
//set the new renaming rule
$writer->renameAssocTable( $oldName, $this->via );
}
$this->via = NULL;
}
$beans = array();
if ($this->getID()) {
$type = $this->beau( $type );
$assocManager = $redbean->getAssociationManager();
$beans = $assocManager->related( $this, $type, $this->withSql, $this->withParams );
}
$this->withSql = '';
$this->withParams = array();
return $beans;
}
/**
* Internal method.
* Obtains the own list of a certain type.
*
* @param string $type name of the list you want to retrieve
*
* @return array
*/
private function getOwnList( $type, $redbean )
{
$type = $this->beau( $type );
if ( $this->aliasName ) {
$parentField = $this->aliasName;
$myFieldLink = $parentField . '_id';
$this->__info['sys.alias.' . $type] = $this->aliasName;
$this->aliasName = NULL;
} else {
$parentField = $this->__info['type'];
$myFieldLink = $parentField . '_id';
}
$beans = array();
if ( $this->getID() ) {
$firstKey = NULL;
if ( count( $this->withParams ) > 0 ) {
reset( $this->withParams );
$firstKey = key( $this->withParams );
}
if ( !is_numeric( $firstKey ) || $firstKey === NULL ) {
$bindings = $this->withParams;
$bindings[':slot0'] = $this->getID();