@@ -22,7 +22,7 @@ class InsertBehavior extends Behavior
2222 * @var array
2323 */
2424 protected $ _defaultConfig = [
25- 'event ' => ['beforeSave ' => true ]
25+ 'event ' => ['beforeSave ' => true ],
2626 ];
2727
2828 /**
@@ -78,23 +78,18 @@ public function insertOnce(Entity $entity, array $conditions = null)
7878 $ insertData ['modified ' ] = FrozenTime::now ()->toDateTimeString ();
7979 }
8080
81- $ escape = function ($ content ) {
82- return is_null ($ content ) ? 'NULL ' : '\'' . addslashes ($ content ) . '\'' ;
83- };
84-
85- $ escapedInsertData = array_map ($ escape , $ insertData );
8681 $ fields = array_keys ($ insertData );
8782 $ existsConditions = $ conditions ;
8883 if (is_null ($ existsConditions )) {
89- $ existsConditions = $ this ->getExistsConditions ($ escapedInsertData );
84+ $ existsConditions = $ this ->getExistsConditions ($ insertData );
9085 }
9186
9287 $ query = $ this ->_table
9388 ->query ()
9489 ->insert ($ fields )
9590 ->epilog (
9691 $ this
97- ->buildTmpTableSelectQuery ($ escapedInsertData )
92+ ->buildTmpTableSelectQuery ($ insertData )
9893 ->where (function (QueryExpression $ exp ) use ($ existsConditions ) {
9994 $ query = $ this ->_table
10095 ->find ()
@@ -111,55 +106,61 @@ public function insertOnce(Entity $entity, array $conditions = null)
111106 /**
112107 * build tmp table's select query for insert select query
113108 *
114- * @param array $escapedData escaped array data
109+ * @param array $insertData insert data
115110 * @throws LogicException select query is invalid
116111 * @return Query tmp table's select query
117112 */
118- private function buildTmpTableSelectQuery ($ escapedData )
113+ private function buildTmpTableSelectQuery ($ insertData )
119114 {
120115 $ driver = $ this ->_table
121116 ->getConnection ()
122117 ->getDriver ();
123118 $ schema = [];
124- foreach ($ escapedData as $ key => $ value ) {
119+ $ binds = [];
120+ foreach ($ insertData as $ key => $ value ) {
125121 $ 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+ }
127129 }
128130
129131 $ tmpTable = TableRegistry::getTableLocator ()->get ('tmp ' , [
130- 'schema ' => $ this ->_table ->getSchema ()
132+ 'schema ' => $ this ->_table ->getSchema (),
131133 ]);
132134 $ query = $ tmpTable
133135 ->find ()
134- ->select (array_keys ($ escapedData ))
136+ ->select (array_keys ($ insertData ))
135137 ->from (
136138 sprintf ('(SELECT %s) as tmp ' , implode (', ' , $ schema ))
137139 );
138140 /** @var Query $selectQuery */
139141 $ selectQuery = $ query ;
142+ foreach ($ binds as $ key => $ value ) {
143+ $ selectQuery ->bind ($ key , $ value );
144+ }
140145
141146 return $ selectQuery ;
142147 }
143148
144149 /**
145150 * get conditions for finding a record already exists
146151 *
147- * @param array $escapedData escaped array data
152+ * @param array $insertData insert data
148153 * @return array conditions
149154 */
150- private function getExistsConditions ($ escapedData )
155+ private function getExistsConditions ($ insertData )
151156 {
152157 $ autoFillFields = ['created ' , 'modified ' ];
153158 $ existsConditions = [];
154- foreach ($ escapedData as $ field => $ value ) {
159+ foreach ($ insertData as $ field => $ value ) {
155160 if (in_array ($ field , $ autoFillFields , true )) {
156161 continue ;
157162 }
158- if ($ value === 'NULL ' ) {
159- $ existsConditions [] = "{$ field } IS NULL " ;
160- } else {
161- $ existsConditions [] = "{$ field } = {$ value }" ;
162- }
163+ $ existsConditions [$ field . ' IS ' ] = $ value ;
163164 }
164165
165166 return $ existsConditions ;
0 commit comments