77 * @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88 * @copyright Copyright © 2022 Muhammet ŞAFAK
99 * @license ./LICENSE MIT
10- * @version 1.1.8
10+ * @version 1.1.9
1111 * @link https://www.muhammetsafak.com.tr
1212 */
1313
@@ -88,11 +88,9 @@ public function __construct(array $configurations)
8888 if (isset ($ this ->configurations ['fetch ' ])){
8989 $ dataMapperOptions ['fetch ' ] = $ this ->configurations ['fetch ' ];
9090 }
91- $ this ->_dataMapper = new DataMapper ($ this ->_connection , $ dataMapperOptions );
92- $ this ->_queryBuilder = new QueryBuilder ([
93- 'allowedFields ' => ($ this ->configurations ['allowedFields ' ] ?? null ),
94- 'schema ' => ($ this ->configurations ['tableSchema ' ] ?? null ),
95- 'schemaID ' => ($ this ->configurations ['tableSchemaID ' ] ?? null ),
91+ $ this ->_dataMapper = new DataMapper ($ this , $ dataMapperOptions );
92+ $ this ->_queryBuilder = new QueryBuilder ($ this , [
93+ 'allowedFields ' => ($ this ->configurations ['allowedFields ' ] ?? null )
9694 ]);
9795 $ this ->_validation = new Validation ($ this ->configurations ['validation ' ]['methods ' ], $ this ->configurations ['validation ' ]['messages ' ], $ this ->configurations ['validation ' ]['labels ' ], $ this );
9896 }
@@ -140,11 +138,9 @@ public function __call($name, $arguments)
140138 throw new DatabaseException ('The " ' . $ name . '" method does not exist. ' );
141139 }
142140
143- public function setSchemaID (? string $ schemaID ): self
141+ public function with ( ): self
144142 {
145- $ this ->configurations ['tableSchemaID ' ] = $ schemaID ;
146- $ this ->getQueryBuilder ()->setSchemaID ($ schemaID );
147- return $ this ;
143+ return clone $ this ;
148144 }
149145
150146 /**
@@ -155,6 +151,21 @@ public function getSchema()
155151 return $ this ->configurations ['tableSchema ' ];
156152 }
157153
154+ public function setSchema (string $ schema ): self
155+ {
156+ if ($ schema === '' ){
157+ $ this ->configurations ['tableSchema ' ] = null ;
158+ }else {
159+ $ this ->configurations ['tableSchema ' ] = $ schema ;
160+ }
161+ return $ this ;
162+ }
163+
164+ public function withSchema (string $ schema ): self
165+ {
166+ return $ this ->with ()->setSchema ($ schema );
167+ }
168+
158169 /**
159170 * @return string|null
160171 */
@@ -163,6 +174,21 @@ public function getSchemaID()
163174 return $ this ->configurations ['tableSchemaID ' ];
164175 }
165176
177+ public function setSchemaID (string $ schemaID ): self
178+ {
179+ if ($ schemaID === '' ){
180+ $ this ->configurations ['tableSchemaID ' ] = null ;
181+ }else {
182+ $ this ->configurations ['tableSchemaID ' ] = $ schemaID ;
183+ }
184+ return $ this ;
185+ }
186+
187+ public function withSchemaID (string $ schemaID ): self
188+ {
189+ return $ this ->with ()->setSchemaID ($ schemaID );
190+ }
191+
166192 public function isError (): bool
167193 {
168194 $ this ->get_error_merge ();
@@ -211,23 +237,25 @@ public function getValidation(): Validation
211237 */
212238 public function create (array $ fields )
213239 {
214- $ data = []; $ parameters = [];
240+ $ data = [];
215241 $ isCreatedField = !empty ($ this ->configurations ['createdField ' ]);
242+ if ($ isCreatedField ){
243+ $ createdFieldParameterName = $ this ->getDataMapper ()->addParameter ($ this ->configurations ['createdField ' ], \date ($ this ->configurations ['timestampFormat ' ]));
244+ }
216245 if (\count ($ fields ) === \count ($ fields , \COUNT_RECURSIVE )){
217246 $ this ->getValidation ()->setData ($ fields );
218247 foreach ($ fields as $ column => $ value ) {
219248 if ($ this ->getValidation ()->validation ($ column , null ) === FALSE ){
220249 $ this ->errors [] = $ this ->getValidation ()->getError ();
221250 return false ;
222251 }
223- $ data [$ column ] = ': ' .$ column ;
224- $ parameters [': ' . $ column ] = $ value ;
252+ $ data [$ column ] = $ value ;
225253 }
226254 if (empty ($ data )){
227255 return false ;
228256 }
229257 if ($ isCreatedField ){
230- $ data [$ this ->configurations ['createdField ' ]] = ' : ' . $ this -> configurations [ ' createdField ' ] ;
258+ $ data [$ this ->configurations ['createdField ' ]] = $ createdFieldParameterName ;
231259 }
232260 }else {
233261 $ i = 0 ;
@@ -239,24 +267,19 @@ public function create(array $fields)
239267 $ this ->errors [] = $ this ->getValidation ()->getError ();
240268 return false ;
241269 }
242- $ data [$ i ][$ column ] = ': ' . $ column . '_ ' . $ i ;
243- $ parameters [': ' . $ column . '_ ' . $ i ] = $ value ;
270+ $ data [$ i ][$ column ] = $ value ;
244271 }
245272 if (empty ($ data )){
246273 continue ;
247274 }
248275 if ($ isCreatedField ){
249- $ data [$ i ][$ this ->configurations ['createdField ' ]] = ' : ' . $ this -> configurations [ ' createdField ' ] ;
276+ $ data [$ i ][$ this ->configurations ['createdField ' ]] = $ createdFieldParameterName ;
250277 }
251278 ++$ i ;
252279 }
253280 }
254- if ($ isCreatedField ){
255- $ parameters [': ' . $ this ->configurations ['createdField ' ]] = \date ($ this ->configurations ['timestampFormat ' ]);
256- }
257- $ this ->getDataMapper ()->setParameters ($ parameters );
258- unset($ parameters );
259281 $ query = $ this ->getQueryBuilder ()->insertQuery ($ data );
282+ $ this ->getQueryBuilder ()->reset ();
260283 $ this ->getDataMapper ()->persist ($ query , []);
261284 return $ this ->getDataMapper ()->numRows () > 0 ;
262285 }
0 commit comments