@@ -22,7 +22,7 @@ class InsertBehavior extends Behavior
22
22
* @var array
23
23
*/
24
24
protected $ _defaultConfig = [
25
- 'event ' => ['beforeSave ' => true ]
25
+ 'event ' => ['beforeSave ' => true ],
26
26
];
27
27
28
28
/**
@@ -78,23 +78,18 @@ public function insertOnce(Entity $entity, array $conditions = null)
78
78
$ insertData ['modified ' ] = FrozenTime::now ()->toDateTimeString ();
79
79
}
80
80
81
- $ escape = function ($ content ) {
82
- return is_null ($ content ) ? 'NULL ' : '\'' . addslashes ($ content ) . '\'' ;
83
- };
84
-
85
- $ escapedInsertData = array_map ($ escape , $ insertData );
86
81
$ fields = array_keys ($ insertData );
87
82
$ existsConditions = $ conditions ;
88
83
if (is_null ($ existsConditions )) {
89
- $ existsConditions = $ this ->getExistsConditions ($ escapedInsertData );
84
+ $ existsConditions = $ this ->getExistsConditions ($ insertData );
90
85
}
91
86
92
87
$ query = $ this ->_table
93
88
->query ()
94
89
->insert ($ fields )
95
90
->epilog (
96
91
$ this
97
- ->buildTmpTableSelectQuery ($ escapedInsertData )
92
+ ->buildTmpTableSelectQuery ($ insertData )
98
93
->where (function (QueryExpression $ exp ) use ($ existsConditions ) {
99
94
$ query = $ this ->_table
100
95
->find ()
@@ -111,55 +106,61 @@ public function insertOnce(Entity $entity, array $conditions = null)
111
106
/**
112
107
* build tmp table's select query for insert select query
113
108
*
114
- * @param array $escapedData escaped array data
109
+ * @param array $insertData insert data
115
110
* @throws LogicException select query is invalid
116
111
* @return Query tmp table's select query
117
112
*/
118
- private function buildTmpTableSelectQuery ($ escapedData )
113
+ private function buildTmpTableSelectQuery ($ insertData )
119
114
{
120
115
$ driver = $ this ->_table
121
116
->getConnection ()
122
117
->getDriver ();
123
118
$ schema = [];
124
- foreach ($ escapedData as $ key => $ value ) {
119
+ $ binds = [];
120
+ foreach ($ insertData as $ key => $ value ) {
125
121
$ col = $ driver ->quoteIdentifier ($ key );
126
- $ schema [] = "{$ value } AS {$ col }" ;
122
+ if (is_null ($ value )) {
123
+ $ schema [] = "NULL AS {$ col }" ;
124
+ } else {
125
+ $ bindKey = ': ' . strtolower ($ key );
126
+ $ binds [$ bindKey ] = $ value ;
127
+ $ schema [] = "{$ bindKey } AS {$ col }" ;
128
+ }
127
129
}
128
130
129
131
$ tmpTable = TableRegistry::getTableLocator ()->get ('tmp ' , [
130
- 'schema ' => $ this ->_table ->getSchema ()
132
+ 'schema ' => $ this ->_table ->getSchema (),
131
133
]);
132
134
$ query = $ tmpTable
133
135
->find ()
134
- ->select (array_keys ($ escapedData ))
136
+ ->select (array_keys ($ insertData ))
135
137
->from (
136
138
sprintf ('(SELECT %s) as tmp ' , implode (', ' , $ schema ))
137
139
);
138
140
/** @var Query $selectQuery */
139
141
$ selectQuery = $ query ;
142
+ foreach ($ binds as $ key => $ value ) {
143
+ $ selectQuery ->bind ($ key , $ value );
144
+ }
140
145
141
146
return $ selectQuery ;
142
147
}
143
148
144
149
/**
145
150
* get conditions for finding a record already exists
146
151
*
147
- * @param array $escapedData escaped array data
152
+ * @param array $insertData insert data
148
153
* @return array conditions
149
154
*/
150
- private function getExistsConditions ($ escapedData )
155
+ private function getExistsConditions ($ insertData )
151
156
{
152
157
$ autoFillFields = ['created ' , 'modified ' ];
153
158
$ existsConditions = [];
154
- foreach ($ escapedData as $ field => $ value ) {
159
+ foreach ($ insertData as $ field => $ value ) {
155
160
if (in_array ($ field , $ autoFillFields , true )) {
156
161
continue ;
157
162
}
158
- if ($ value === 'NULL ' ) {
159
- $ existsConditions [] = "{$ field } IS NULL " ;
160
- } else {
161
- $ existsConditions [] = "{$ field } = {$ value }" ;
162
- }
163
+ $ existsConditions [$ field . ' IS ' ] = $ value ;
163
164
}
164
165
165
166
return $ existsConditions ;
0 commit comments