-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbo_fmcakemix.php
executable file
·1109 lines (916 loc) · 33 KB
/
dbo_fmcakemix.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
/**
* FMCakeMix
* @author Alex Gibbons [email protected]
* @date 02/2009
*
* Copyright (c) 2009 Alex Gibbons, Beezwax.net
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// =================================================================================
// = FX.php : required base class
// =================================================================================
// FX is a free open-source PHP class for accessing FileMaker using curl and xml
// By: Chris Hansen with Chris Adams, Gjermund Thorsen, and others
// Tested with version: 4.5.1
// Web Site: www.iviking.org
// =================================================================================
App::import('Vendor','FX', array('file' => 'FX.php'));
class DboFMCakeMix extends DataSource {
var $description = "FileMaker Data Source";
var $_baseConfig = array (
'host' => 'localhost',
'port' => 80,
);
// warning: these get added to schema, but allow you to pass in values
// so that the xml api parses them as functional parameters
var $allowed_parameters = array(
'-script',
'-script.prefind',
'-script.presort',
'-script.param',
'-script.prefind.param',
'-script.presort.param'
);
/**
* FileMaker column definition
*
* @var array
*/
var $columns = array(
'primary_key' => array('name' => 'NUMBER'),
'string' => array('name' => 'TEXT'),
'text' => array('name' => 'TEXT'),
'integer' => array('name' => 'NUMBER','formatter' => 'intval'),
'float' => array('name' => 'NUMBER', 'formatter' => 'floatval'),
'datetime' => array('name' => 'TIMESTAMP', 'format' => 'm/d/Y H:i:s', 'formatter' => 'date'),
'timestamp' => array('name' => 'TIMESTAMP', 'format' => 'm/d/Y H:i:s', 'formatter' => 'date'),
'time' => array('name' => 'TIME', 'format' => 'H:i:s', 'formatter' => 'date'),
'date' => array('name' => 'DATE', 'format' => 'm/d/Y', 'formatter' => 'date'),
'binary' => array('name' => 'CONTAINER'),
'boolean' => array('name' => 'NUMBER')
);
/**
* Constructor
*/
function __construct($config = null) {
$this->debug = Configure :: read() > 0;
$this->fullDebug = Configure :: read() > 1;
$this->timeFlag = getMicrotime();
parent :: __construct($config);
return $this->connect();
}
/**
* Destructor. Closes connection to the database.
*/
function __destruct() {
$this->close();
parent :: __destruct();
}
/**
* Connect. Creates connection handler to database
*/
function connect() {
$config = $this->config;
$this->connected = false;
$this->connection = new FX($config['host'],$config['port'], $config['dataSourceType'], $config['scheme']);
$this->connection->SetDBPassword($config['password'],$config['login']);
// encoding convert. see http://msyk.net/fmp/fx_ja/
if (isset($config['encoding']) && !empty($config['encoding'])) {
$this->connection->SetCharacterEncoding(Configure::read('App.encoding'));
$this->connection->SetDataParamsEncoding($config['encoding']);
}
$this->connected = true; //always returns true
return $this->connected;
}
/**
* Close.
*/
function close() {
if ($this->fullDebug && Configure :: read() > 1) {
$this->showLog();
}
$this->disconnect();
}
/**
* disconnect
*/
function disconnect() {
$this->connected = false;
return $this->connected;
}
/**
* Checks if it's connected to the database
*
* @return boolean True if the database is connected, else false
*/
function isConnected() {
return $this->connected;
}
/**
* Reconnects to database server with optional new settings
*
* @param array $config An array defining the new configuration settings
* @return boolean True on success, false on failure
*/
function reconnect($config = null) {
$this->disconnect();
if ($config != null) {
$this->config = am($this->_baseConfig, $this->config, $config);
}
return $this->connect();
}
/**
* Returns properly formatted field name
*
* @param array $config An array defining the new configuration settings
* @return boolean True on success, false on failure
*/
function name($data) {
return $data;
}
/*
TODO: needs to use recursion
TODO: needs to handle filemakers ability to put mutliple tables on one layout
TODO: should somehow include the ability to specify layout
*/
/**
* The "R" in CRUD
*
* @param Model $model
* @param array $queryData
* @param integer $recursive Number of levels of association
* @return unknown
*/
function read(&$model, $queryData = array(), $recursive = null) {
$fm_database = empty($model->fmDatabaseName) ? $this->config['database'] : $model->fmDatabaseName;
$fm_layout = empty($model->defaultLayout) ? $this->config['defaultLayout'] : $model->defaultLayout;
$queryLimit = $queryData['limit'] == null ? 'all' : $queryData['limit'];
$linkedModels = array();
// take recursive value from recursive param or from queryData
$_recursive = $model->recursive;
if (!is_null($recursive)) {
$model->recursive = $recursive;
} else if(isset($queryData['recursive'])) {
$model->recursive = $queryData['recursive'];
}
// set connection data if Count query
if($queryData['fields'] == 'COUNT') {
// reset the connection parameters to return only 1 result, improves performance
$this->connection->SetDBData($fm_database, $fm_layout, 1 );
} else {
// set basic connection data
$this->connection->SetDBData($fm_database, $fm_layout, $queryLimit );
}
/*
TODO : this has a junk interpretation of a logical or statement, that isn't nestable
* it therefore turns the whole query into an or, if an or statement is injected somewhere
* this is a major limitation of fx.php
*/
if(!empty($queryData['conditions'])) {
$conditions = array(); // a clean set of queries
$isOr = false; // a boolean indicating wether this query is logical or
foreach($queryData['conditions'] as $conditionField => $conditionValue) {
// if a logical or statement has been pased somewhere
if($conditionField == 'or') {
$isOr = true;
if(is_array($conditionValue)) {
$conditions = array_merge($conditions, $conditionValue);
}
} else {
$conditions[$conditionField] = $conditionValue;
}
}
// look for condition operators set in conditions array
// remove them then include them fx style in the query
$operators = array();
foreach($conditions as $conditionField => $conditionValue) {
$operator = $this->parseConditionField($model, $conditionField, 'operator');
$field = $this->parseConditionField($model, $conditionField, 'field');
if ($operator) {
$operators[$field] = $conditionValue;
unset($conditions[$conditionField]);
}
}
foreach($conditions as $conditionField => $conditionValue) {
$field = $this->parseConditionField($model, $conditionField, 'field');
$this->connection->AddDBParam($field, $conditionValue, isset($operators[$field]) ? $operators[$field] : 'eq');
//add or operator
if($isOr){
$this->connection->SetLogicalOR();
}
}
}
// set sort order
foreach($queryData['order'] as $orderCondition) {
if(!empty($orderCondition)){
foreach($orderCondition as $conditionField => $sortRule) {
$field = $this->parseConditionField($model, $conditionField, 'field');
$sortRuleFm = $sortRule == 'desc' ? 'descend' : 'ascend';
$this->connection->AddSortParam($field, $sortRuleFm);
}
}
}
// set skip records if there is an offset
if(!empty($queryData['offset'])) {
$this->connection->FMSkipRecords($queryData['offset']);
}
// return a found count if requested
if($queryData['fields'] == 'COUNT') {
// perform find without returning result data
$fmResults = $this->connection->FMFind(true, 'basic');
// test result
if(!$this->handleFXResult($fmResults, $model->name, 'read (count)')) {
return FALSE;
}
$countResult = array();
$countResult[0][0] = array('count' => $fmResults['foundCount']);
// return found count
return $countResult;
} else {
// perform the find in FileMaker
$fmResults = $this->connection->FMFind();
if(!$this->handleFXResult($fmResults, $model->name, 'read')) {
return FALSE;
}
}
$resultsOut = array();
// format results
if(!empty($fmResults['data'])) {
$i = 0;
foreach($fmResults['data'] as $recmodid => $recordData) {
$relatedModels = array();
$recmodid_Ary = explode('.', $recmodid);
$resultsOut[$i][$model->name]['-recid'] = $recmodid_Ary[0];
$resultsOut[$i][$model->name]['-modid'] = $recmodid_Ary[1];
foreach($recordData as $field => $value) {
$resultsOut[$i][$model->name][$field] = isset($value[0]) ? $value[0] : null;
}
$i++;
}
}
// ================================
// = Searching for Related Models =
// ================================
if ($model->recursive > 0) {
foreach ($model->__associations as $type) {
foreach ($model->{$type} as $assoc => $assocData) {
$linkModel =& $model->{$assoc};
if (!in_array($type . '/' . $assoc, $linkedModels)) {
if ($model->useDbConfig == $linkModel->useDbConfig) {
$db =& $this;
} else {
$db =& ConnectionManager::getDataSource($linkModel->useDbConfig);
}
} elseif ($model->recursive > 1 && ($type == 'belongsTo' || $type == 'hasOne')) {
$db =& $this;
}
if (isset($db)) {
$stack = array($assoc);
$db->queryAssociation($model, $linkModel, $type, $assoc, $assocData, $array, true, $resultsOut, $model->recursive - 1, $stack);
unset($db);
}
}
}
}
if (!is_null($recursive)) {
$model->recursive = $_recursive;
}
// return data
return $resultsOut;
}
/**
* Calculate
* currently this only returns a 'count' flag if a count is requested. This will tell
* the read function to return a found count rather than results
*
* @param model $model
* @param string $func Lowercase name of SQL function, i.e. 'count' or 'max'
* @param array $params Function parameters
* @return string flag informing read function to parse results as per special case of $func
* @access public
*/
function calculate(&$model, $func, $params = array()) {
$params = (array)$params;
switch (strtolower($func)) {
case 'count':
if (!isset($params[0])) {
$params[0] = '*';
}
if (!isset($params[1])) {
$params[1] = 'count';
}
return 'COUNT';
case 'max':
case 'min':
if (!isset($params[1])) {
$params[1] = $params[0];
}
return strtoupper($func) . '(' . $this->name($params[0]) . ') AS ' . $this->name($params[1]);
break;
}
}
/**
* The "D" in CRUD
* can only delete from the recid that is internal to filemaker
* We do this by using the deleteAll model method, which lets us pass conditions to the driver
* delete statement. This method will only work if the conditions array contains a 'recid' field
* and value. Also, must pass cascade value of false with the deleteAll method.
*
* @param Model $model
* @param array $conditions
* @return boolean Success
*/
function delete(&$model, $conditions = null) {
$fm_database = empty($model->fmDatabaseName) ? $this->config['database'] : $model->fmDatabaseName;
$fm_layout = empty($model->defaultLayout) ? $this->config['defaultLayout'] : $model->defaultLayout;
// set basic connection data
$this->connection->SetDBData($fm_database, $fm_layout);
if(is_null($conditions)) {
$this->connection->AddDBParam('-recid', $model->getId(), 'eq');
} else {
// must contain a -recid field
foreach($conditions as $field => $value) {
$this->connection->AddDBParam($field, $value, 'eq');
}
}
// perform deletion
$return = $this->connection->FMDelete(TRUE);
if(!$this->handleFXResult($return, $model->name, 'delete')) {
return FALSE;
} else {
return TRUE;
}
}
/**
* The "C" in CRUD
*
* @param Model $model
* @param array $fields
* @param array $values
* @return boolean Success
*/
function create(&$model, $fields = null, $values = null) {
$id = null;
// if empty then use data in model
if ($fields == null) {
unset($fields, $values);
$fields = array_keys($model->data);
$values = array_values($model->data);
}
$count = count($fields);
// get connection parameters
$fm_database = empty($model->fmDatabaseName) ? $this->config['database'] : $model->fmDatabaseName;
$fm_layout = empty($model->defaultLayout) ? $this->config['defaultLayout'] : $model->defaultLayout;
// set basic connection data
$this->connection->SetDBData($fm_database, $fm_layout);
// if by chance the recid was passed to this create method we want
// to make sure we remove it as filemaker will reject the request.
if(isset($model->fm_recid) && !empty($model->fm_recid)) {
foreach($fields as $index => $field) {
if($field == $model->fm_recid) {
unset($fields[$index]);
unset($values[$index]);
}
}
}
foreach($fields as $index => $field) {
$this->connection->AddDBParam($field, $values[$index]);
}
// perform creation
$return = $this->connection->FMNew();
if(!$this->handleFXResult($return, $model->name, 'new')) {
return FALSE;
}
if($return['errorCode'] != 0) {
return false;
}
// write recid to model id and __lastinsert attributes
foreach($return['data'] as $recmodid => $returnedModel){
$recmodid_Ary = explode('.', $recmodid);
$model->id = $recmodid_Ary[0];
$model->setInsertID($recmodid_Ary[0]);
}
$resultsOut = array();
if(!empty($return['data'])) {
foreach($return['data'] as $recmodid => $recordData) {
$recmodid_Ary = explode('.', $recmodid);
$resultsOut[$model->name]['-recid'] = $recmodid_Ary[0];
$resultsOut[$model->name]['-modid'] = $recmodid_Ary[1];
foreach($recordData as $field => $value) {
$resultsOut[$model->name][$field] = $value[0];
}
}
}
$model->data = $resultsOut; // this returns data on a create
return true;
}
/**
* The "U" in CRUD
* This could be collapsed under create, for now it's separate for better debugging
* It's important to note that edit requires a FileMaker -recid that should be
* passed as a hidden form field
*
* @param Model $model
* @param array $fields
* @param array $values
* @param mixed $conditions
* @return array
*/
function update(&$model, $fields = array(), $values = null, $conditions = null) {
// get connection parameters
$fm_database = empty($model->fmDatabaseName) ? $this->config['database'] : $model->fmDatabaseName;
$fm_layout = empty($model->defaultLayout) ? $this->config['defaultLayout'] : $model->defaultLayout;
if(!empty($model->id)) {
// set basic connection data
$this->connection->SetDBData($fm_database, $fm_layout);
// **1 here we remove the primary key field if it's marked as readonly
// other fields can be removed by the controller, but cake requires
// the primary key to be included in the query if it's to consider
// the action an edit
foreach($fields as $index => $field) {
if(isset($model->primaryKeyReadOnly) && $field == $model->primaryKey) {
unset($fields[$index]);
unset($values[$index]);
}
}
// ensure that a recid is passed
if(!in_array('-recid',$fields)) {
array_push($fields, '-recid');
array_push($values, $model->getId());
}
// there must be a -recid field passed in here for the edit to work
// could be passed in hidden form field
foreach($fields as $index => $field) {
$this->connection->AddDBParam($field, $values[$index]);
}
// perform edit
$return = $this->connection->FMEdit();
if(!$this->handleFXResult($return, $model->name, 'update')) {
return FALSE;
}
if($return['errorCode'] != 0) {
return false;
} else {
foreach($return['data'] as $recmodid => $returnedModel){
$recmodid_Ary = explode('.', $recmodid);
$model->id = $recmodid_Ary[0];
$model->setInsertID($recmodid_Ary[0]);
}
return true;
}
} else {
return false;
}
}
/**
* Returns an array of the fields in given table name.
*
* @param string $model the model to inspect
* @return array Fields in table. Keys are name and type
*/
function describe(&$model) {
// describe caching
$cache = $this->__describeFromCache($model);
if ($cache != null) {
return $cache;
}
$fm_database = empty($model->fmDatabaseName) ? $this->config['database'] : $model->fmDatabaseName;
$fm_layout = empty($model->defaultLayout) ? $this->config['defaultLayout'] : $model->defaultLayout;
// set basic connection data
$this->connection->SetDBData($fm_database, $fm_layout);
// get layout info
$result = $this->connection->FMFindAny(true, 'basic');
// check for error
if (!$this->handleFXResult($result, $model->name, 'describe')) {
return FALSE;
}
$fieldsOut = array();
$fmFieldTypeConversion = array(
'TEXT' => 'string',
'DATE' => 'date',
'TIME' => 'time',
'TIMESTAMP' => 'timestamp',
'NUMBER' => 'float',
'CONTAINER' => 'binary'
);
foreach($result['fields'] as $field) {
$type = $fmFieldTypeConversion[$field['type']];
$fieldsOut[$field['name']] = array(
'type' => $type,
'null' => null,
'default' => null,
'length' => null,
'key' => null
);
}
$fieldsOut['-recid'] = array(
'type' => 'integer',
'null' => null,
'default' => null,
'length' => null,
'key' => null
);
$fieldsOut['-modid'] = array(
'type' => 'integer',
'null' => null,
'default' => null,
'length' => null,
'key' => null
);
// add in fm xml functional parameters so that they don't get cleaned from saves
foreach ($this->allowed_parameters as $param) {
$fieldsOut[$param] = array(
'type' => 'FM_PARAM',
'null' => null,
'default' => null,
'length' => null,
'key' => null
);
}
// value list handling
if (!empty($model->returnValueLists) && $model->returnValueLists === true) {
$layoutObject = $this->connection->FMView();
foreach($layoutObject['fields'] as $field) {
if (!empty($field['valuelist'])) {
$fieldsOut[$field['name']]['valuelist'] = $layoutObject['valueLists'][$field['valuelist']];
}
}
}
$this->__cacheDescription($this->fullTableName($model, false), $fieldsOut);
return $fieldsOut;
}
/**
* __describeFromCache
* looks for and potentially returns the cached description of the model
*
* @param $model
* @return the models cache description or null if none exists
*/
function __describeFromCache($model) {
if ($this->cacheSources === false) {
return null;
}
if (isset($this->__descriptions[$model->tablePrefix . $model->table])) {
return $this->__descriptions[$model->tablePrefix . $model->table];
}
$cache = $this->__cacheDescription($model->tablePrefix . $model->table);
if ($cache !== null) {
$this->__descriptions[$model->tablePrefix . $model->table] =& $cache;
return $cache;
}
return null;
}
/**
* __cacheDescription
*
* @param string $object : name of model
* @param mixed $data : the data to be cached
* @return mixed : the cached data
*/
function __cacheDescription($object, $data = null) {
if ($this->cacheSources === false) {
return null;
}
if ($data !== null) {
$this->__descriptions[$object] =& $data;
}
$key = ConnectionManager::getSourceName($this) . '_' . $object;
$cache = Cache::read($key, '_cake_model_');
if (empty($cache)) {
$cache = $data;
Cache::write($key, $cache, '_cake_model_');
}
return $cache;
}
/**
* GenerateAssociationQuery
*/
function generateAssociationQuery(& $model, & $linkModel, $type, $association = null, $assocData = array (), & $queryData, $external = false, & $resultSet) {
switch ($type) {
case 'hasOne' :
$id = $resultSet[$model->name][$model->primaryKey];
$queryData['conditions'] = trim($assocData['foreignKey']) . '=' . trim($id);
$queryData['limit'] = 1;
return $queryData;
case 'belongsTo' :
$id = $resultSet[$model->name][$assocData['foreignKey']];
$queryData['conditions'] = array(trim($linkModel->primaryKey) => trim($id));
$queryData['order'] = array();
$queryData['fields'] = '';
$queryData['limit'] = 1;
return $queryData;
case 'hasMany' :
$id = $resultSet[$model->name][$model->primaryKey];
$queryData['conditions'] = array(trim($assocData['foreignKey']) => trim($id));
$queryData['order'] = array();
$queryData['fields'] = '';
$queryData['limit'] = $assocData['limit'];
return $queryData;
case 'hasAndBelongsToMany' :
return null;
}
return null;
}
/**
* QueryAssociation
*
*/
function queryAssociation(& $model, & $linkModel, $type, $association, $assocData, & $queryData, $external = false, & $resultSet, $recursive, $stack) {
foreach($resultSet as $projIndex => $row) {
$queryData = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $row);
$associatedData = $this->readAssociated($linkModel, $queryData, 0);
foreach($associatedData as $assocIndex => $relatedModel) {
$modelName = key($relatedModel);
$resultSet[$projIndex][$modelName][$assocIndex] = $relatedModel[$modelName];
}
}
}
/**
* readAssociated
* very similar to read but for related data
* unlike read does not make a reference to the passed model
*
* @param Model $model
* @param array $queryData
* @param integer $recursive Number of levels of association
* @return unknown
*/
function readAssociated($linkedModel, $queryData = array (), $recursive = null) {
$fm_database = empty($linkedModel->fmDatabaseName) ? $this->config['database'] : $linkedModel->fmDatabaseName;
$fm_layout = empty($linkedModel->defaultLayout) ? $this->config['defaultLayout'] : $linkedModel->defaultLayout;
$queryLimit = $queryData['limit'] == null ? 'all' : $queryData['limit'];
// set basic connection data
$this->connection->SetDBData($fm_database, $fm_layout, $queryLimit );
// add the params
if(!empty($queryData['conditions'])) {
foreach($queryData['conditions'] as $conditionField => $conditionValue) {
$string = $conditionField;
$pattern = '/(\w+)\.(-*\w+)$/i';
$replacement = '${2}';
$plainField = preg_replace($pattern, $replacement, $string);
$this->connection->AddDBParam($plainField, $conditionValue, 'eq');
}
}
// set sort order
foreach($queryData['order'] as $orderCondition) {
if(!empty($orderCondition)){
foreach($orderCondition as $field => $sortRule) {
$string = $field;
$pattern = '/(\w+)\.(-*\w+)$/i';
$replacement = '${2}';
$plainField = preg_replace($pattern, $replacement, $string);
$sortRuleFm = $sortRule == 'desc' ? 'descend' : 'ascend';
$this->connection->AddSortParam($plainField, $sortRuleFm);
}
}
}
// set skip records if there is an offset
if(!empty($queryData['offset'])) {
$this->connection->FMSkipRecords($queryData['offset']);
}
// THIS MAY NOT BE NECESSARY FOR THE READASSOCIATED FUNCTION
// return a found count if requested
if($queryData['fields'] == 'COUNT') {
// perform find without returning result data
$fmResults = $this->connection->FMFind(true, 'basic');
// check for error
if(!$this->handleFXResult($fmResults, $linkedModel->name, 'readassociated (count)')) {
return FALSE;
}
$countResult = array();
$countResult[0][0] = array('count' => $fmResults['foundCount']);
// return found count
return $countResult;
} else {
// perform the find in FileMaker
$fmResults = $this->connection->FMFind();
// check for error
if(!$this->handleFXResult($fmResults, $linkedModel->name, 'readassociated')) {
return FALSE;
}
}
$resultsOut = array();
// format results
if(!empty($fmResults['data'])) {
$i = 0;
foreach($fmResults['data'] as $recmodid => $recordData) {
$relatedModels = array();
$recmodid_Ary = explode('.', $recmodid);
$resultsOut[$i][$linkedModel->name]['-recid'] = $recmodid_Ary[0];
$resultsOut[$i][$linkedModel->name]['-modid'] = $recmodid_Ary[1];
foreach($recordData as $field => $value) {
// if $field is not a related entity
if(strpos($field, '::') === false) {
// grab table field data (grabs first repitition)
$resultsOut[$i][$linkedModel->name][$field] = $value[0];
} else {
$resultsOut[$i][$linkedModel->name][$field] = isset($value[0]) ? $value[0] : null;
}
}
$i++;
}
} else {
}
return $resultsOut;
}
/**
* parseConditionField
*
* @param {Model}
* @param {String} field string from condition
* @param {String} model|field|operator
*/
function parseConditionField($model, $field, $match_part) {
$relations = $model->tableToModel;
$field_parts = explode('.', $field);
$model = null;
$field = null;
$operator = null;
if ($field_parts[count($field_parts) - 1] === 'op') {
$operator = array_pop($field_parts);
}
if (in_array($field_parts[0], $relations)) {
$model = array_splice($field_parts, 0, 1);
$model = $model[0];
}
$field = implode('.',$field_parts);
return $$match_part;
}
/**
* Gets full table name including prefix
*
* @param mixed $model
* @param boolean $quote
* @return string Full quoted table name
*/
function fullTableName($model, $quote = true) {
if (is_object($model)) {
$table = $model->tablePrefix . $model->table;
} elseif (isset($this->config['prefix'])) {
$table = $this->config['prefix'] . strval($model);
} else {
$table = strval($model);
}
if ($quote) {
return $this->name($table);
}
return $table;
}
/**
* Returns a formatted error message from previous database operation.
*
* @return string Error message with error number
*/
function lastError() {
if (FX::isError($this->lastFXError)) {
return $this->lastFXError.getCode() . ': ' . $this->lastFXError.getMessage();
}
return null;
}
/**
* handleFXResult
*
* logs queries, logs errors, and returns false on error
*
* @param FX result object or FX error object
* @param string : model name
* @param string : action name
*
* @return false if result is an FX error object
*/
function handleFXResult($result, $modelName = 'N/A', $actionName = 'N/A') {
$this->_queriesCnt++;
// if a connection error
if(FX::isError($result)) {
// log error
$this->_queriesLog[] = array(
'model' => $modelName,
'action' => $actionName,
'query' => '',
'error' => $result->toString(),
'numRows' => '',
'took' => round((getMicrotime() - $this->timeFlag) * 1000, 0)
);
if (count($this->_queriesLog) > $this->_queriesLogMax) {
array_pop($this->_queriesLog);
}
CakeLog::write('error', $this->formatErrorMessage('FX Error', $result->toString(), $modelName, $actionName));
$this->timeFlag = getMicrotime();
return FALSE;
// if a filemaker error other than no records found
} elseif ($result['errorCode'] != 0 && $result['errorCode'] != 401) {
// log error
$this->_queriesLog[] = array(
'model' => $modelName,
'action' => $actionName,
'query' => substr($result['URL'],strrpos($result['URL'], '?')),
'error' => $result['errorCode'],