@@ -189,7 +189,7 @@ abstract class BaseModel
189189
190190 /**
191191 * Whether rules should be removed that do not exist
192- * in the passed in data. Used between inserts/ updates.
192+ * in the passed data. Used in updates.
193193 *
194194 * @var bool
195195 */
@@ -326,9 +326,9 @@ abstract protected function doFind(bool $singleton, $id = null);
326326 *
327327 * @param string $columnName Column Name
328328 *
329- * @throws DataException
330- *
331329 * @return array|null The resulting row of data, or null if no data found.
330+ *
331+ * @throws DataException
332332 */
333333 abstract protected function doFindColumn (string $ columnName );
334334
@@ -392,9 +392,9 @@ abstract protected function doUpdate($id = null, $data = null): bool;
392392 * @param int $batchSize The size of the batch to run
393393 * @param bool $returnSQL True means SQL is returned, false will execute the query
394394 *
395- * @throws DatabaseException
396- *
397395 * @return mixed Number of rows affected or FALSE on failure
396+ *
397+ * @throws DatabaseException
398398 */
399399 abstract protected function doUpdateBatch (?array $ set = null , ?string $ index = null , int $ batchSize = 100 , bool $ returnSQL = false );
400400
@@ -405,9 +405,9 @@ abstract protected function doUpdateBatch(?array $set = null, ?string $index = n
405405 * @param array|int|string|null $id The rows primary key(s)
406406 * @param bool $purge Allows overriding the soft deletes setting.
407407 *
408- * @throws DatabaseException
409- *
410408 * @return bool|string
409+ *
410+ * @throws DatabaseException
411411 */
412412 abstract protected function doDelete ($ id = null , bool $ purge = false );
413413
@@ -541,9 +541,9 @@ public function find($id = null)
541541 *
542542 * @param string $columnName Column Name
543543 *
544- * @throws DataException
545- *
546544 * @return array|null The resulting row of data, or null if no data found.
545+ *
546+ * @throws DataException
547547 */
548548 public function findColumn (string $ columnName )
549549 {
@@ -693,21 +693,31 @@ public function getInsertID()
693693 * @param array|object|null $data Data
694694 * @param bool $returnID Whether insert ID should be returned or not.
695695 *
696- * @throws ReflectionException
697- *
698696 * @return bool|int|string insert ID or true on success. false on failure.
697+ *
698+ * @throws ReflectionException
699699 */
700700 public function insert ($ data = null , bool $ returnID = true )
701701 {
702702 $ this ->insertID = 0 ;
703703
704+ // Set $cleanValidationRules to false temporary.
705+ $ cleanValidationRules = $ this ->cleanValidationRules ;
706+ $ this ->cleanValidationRules = false ;
707+
704708 $ data = $ this ->transformDataToArray ($ data , 'insert ' );
705709
706710 // Validate data before saving.
707- if (! $ this ->skipValidation && ! $ this ->cleanRules ()->validate ($ data )) {
711+ if (! $ this ->skipValidation && ! $ this ->validate ($ data )) {
712+ // Restore $cleanValidationRules
713+ $ this ->cleanValidationRules = $ cleanValidationRules ;
714+
708715 return false ;
709716 }
710717
718+ // Restore $cleanValidationRules
719+ $ this ->cleanValidationRules = $ cleanValidationRules ;
720+
711721 // Must be called first so we don't
712722 // strip out created_at values.
713723 $ data = $ this ->doProtectFields ($ data );
@@ -767,12 +777,16 @@ public function insert($data = null, bool $returnID = true)
767777 * @param int $batchSize The size of the batch to run
768778 * @param bool $testing True means only number of records is returned, false will execute the query
769779 *
770- * @throws ReflectionException
771- *
772780 * @return bool|int Number of rows inserted or FALSE on failure
781+ *
782+ * @throws ReflectionException
773783 */
774784 public function insertBatch (?array $ set = null , ?bool $ escape = null , int $ batchSize = 100 , bool $ testing = false )
775785 {
786+ // Set $cleanValidationRules to false temporary.
787+ $ cleanValidationRules = $ this ->cleanValidationRules ;
788+ $ this ->cleanValidationRules = false ;
789+
776790 if (is_array ($ set )) {
777791 foreach ($ set as &$ row ) {
778792 // If $data is using a custom class with public or protected
@@ -789,8 +803,11 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
789803 $ row = (array ) $ row ;
790804 }
791805
792- // Validate every row..
793- if (! $ this ->skipValidation && ! $ this ->cleanRules ()->validate ($ row )) {
806+ // Validate every row.
807+ if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
808+ // Restore $cleanValidationRules
809+ $ this ->cleanValidationRules = $ cleanValidationRules ;
810+
794811 return false ;
795812 }
796813
@@ -811,6 +828,9 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
811828 }
812829 }
813830
831+ // Restore $cleanValidationRules
832+ $ this ->cleanValidationRules = $ cleanValidationRules ;
833+
814834 return $ this ->doInsertBatch ($ set , $ escape , $ batchSize , $ testing );
815835 }
816836
@@ -832,7 +852,7 @@ public function update($id = null, $data = null): bool
832852 $ data = $ this ->transformDataToArray ($ data , 'update ' );
833853
834854 // Validate data before saving.
835- if (! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ data )) {
855+ if (! $ this ->skipValidation && ! $ this ->validate ($ data )) {
836856 return false ;
837857 }
838858
@@ -882,10 +902,10 @@ public function update($id = null, $data = null): bool
882902 * @param int $batchSize The size of the batch to run
883903 * @param bool $returnSQL True means SQL is returned, false will execute the query
884904 *
905+ * @return mixed Number of rows affected or FALSE on failure
906+ *
885907 * @throws DatabaseException
886908 * @throws ReflectionException
887- *
888- * @return mixed Number of rows affected or FALSE on failure
889909 */
890910 public function updateBatch (?array $ set = null , ?string $ index = null , int $ batchSize = 100 , bool $ returnSQL = false )
891911 {
@@ -906,7 +926,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
906926 }
907927
908928 // Validate data before saving.
909- if (! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ row )) {
929+ if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
910930 return false ;
911931 }
912932
@@ -937,9 +957,9 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
937957 * @param array|int|string|null $id The rows primary key(s)
938958 * @param bool $purge Allows overriding the soft deletes setting.
939959 *
940- * @throws DatabaseException
941- *
942960 * @return BaseResult|bool
961+ *
962+ * @throws DatabaseException
943963 */
944964 public function delete ($ id = null , bool $ purge = false )
945965 {
@@ -1027,7 +1047,7 @@ public function onlyDeleted()
10271047 public function replace (?array $ data = null , bool $ returnSQL = false )
10281048 {
10291049 // Validate data before saving.
1030- if ($ data && ! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ data )) {
1050+ if ($ data && ! $ this ->skipValidation && ! $ this ->validate ($ data )) {
10311051 return false ;
10321052 }
10331053
@@ -1153,9 +1173,9 @@ protected function doProtectFields(array $data): array
11531173 *
11541174 * @param int|null $userData An optional PHP timestamp to be converted.
11551175 *
1156- * @throws ModelException
1157- *
11581176 * @return mixed
1177+ *
1178+ * @throws ModelException
11591179 */
11601180 protected function setDate (?int $ userData = null )
11611181 {
@@ -1177,9 +1197,9 @@ protected function setDate(?int $userData = null)
11771197 *
11781198 * @param int $value value
11791199 *
1180- * @throws ModelException
1181- *
11821200 * @return int|string
1201+ *
1202+ * @throws ModelException
11831203 */
11841204 protected function intToDate (int $ value )
11851205 {
@@ -1440,9 +1460,9 @@ public function allowCallbacks(bool $val = true)
14401460 * @param string $event Event
14411461 * @param array $eventData Event Data
14421462 *
1443- * @throws DataException
1444- *
14451463 * @return mixed
1464+ *
1465+ * @throws DataException
14461466 */
14471467 protected function trigger (string $ event , array $ eventData )
14481468 {
@@ -1501,9 +1521,9 @@ public function asObject(string $class = 'object')
15011521 * @param bool $onlyChanged Only Changed Property
15021522 * @param bool $recursive If true, inner entities will be casted as array as well
15031523 *
1504- * @throws ReflectionException
1505- *
15061524 * @return array Array
1525+ *
1526+ * @throws ReflectionException
15071527 */
15081528 protected function objectToArray ($ data , bool $ onlyChanged = true , bool $ recursive = false ): array
15091529 {
@@ -1531,9 +1551,9 @@ protected function objectToArray($data, bool $onlyChanged = true, bool $recursiv
15311551 * @param bool $onlyChanged Only Changed Property
15321552 * @param bool $recursive If true, inner entities will be casted as array as well
15331553 *
1534- * @throws ReflectionException
1535- *
15361554 * @return array|null Array
1555+ *
1556+ * @throws ReflectionException
15371557 */
15381558 protected function objectToRawArray ($ data , bool $ onlyChanged = true , bool $ recursive = false ): ?array
15391559 {
@@ -1581,7 +1601,11 @@ protected function transformDataToArray($data, string $type): array
15811601 // properties representing the collection elements, we need to grab
15821602 // them as an array.
15831603 if (is_object ($ data ) && ! $ data instanceof stdClass) {
1584- $ data = $ this ->objectToArray ($ data , ($ type === 'update ' ), true );
1604+ // If it validates with entire rules, all fields are needed.
1605+ $ onlyChanged = ($ this ->skipValidation === false && $ this ->cleanValidationRules === false )
1606+ ? false : ($ type === 'update ' );
1607+
1608+ $ data = $ this ->objectToArray ($ data , $ onlyChanged , true );
15851609 }
15861610
15871611 // If it's still a stdClass, go ahead and convert to
0 commit comments