Doesn't save new entry, but save when editing exsiting one

This is a weird issue and I can’t seem to locate the issue.

I have an attribute that doesn’t get saved when using the create view, but does when using the update view?

I checked the serialized data upon submission and the value is there, but I can’t figure out why it isn’t committed to the database.

Would anyone have any ideas of where I should be looking, what could possibly be causing this.

I’ve checked the model, controller, view files, I just can’t spot any differences or issues.

The Controller reports the value of the Attribute correctly in the post when saving, validates without issue, yet it isn’t actually committed to the database.

So I hard coded a value into my controller and nothing!

$model->InitialRequest = date("Y-m-d H:i:s");

The column is never populated in the table, can anyone offer any suggestions?

I’ve managed to get it function in a horrible way, after the initial controller save, I retrieve another model of that entry, push the same value and save it again.

$modelConIR = $this->findModel($model->ProjectId);
$modelConIR->InitialRequest = $model->InitialRequest;
$modelConIR->save(false);

I just don’t get why this would be necessary.

No where in the controller do I work with this attribute in any way, so it isn’t be manipulated, overwritten.

May I ask what you suspect I am doing wrong?

The validation does not return any issues and forcing a 2nd save works, so the entry is acceptable. This is why I am confused at this point.

It seems its failing validation, because when you save passing false its working.

Check out the input and result of your load methods, also try running $model->validate() and checking the $model->errors content.

Another good thing to do is review your Model’s rules and attributes - try looking for caps letters.

If problem persists, try profiling it with xdebug.

Thank you bpanatta for the suggestions, I will investigate each.

For clarity, in the controller, I was already performing validation and then save(false), but the attribute would not save, yet, if I then find that same model, push the value a 2nd time and save(false) again, it takes?

I have to wait until this evening, but I will do more detailed testing. My biggest issue here is locally, on my dev machine it works fine, it is only on the production host that I am experiencing the issue, but can’t track down the difference. I’ve pushed the files (controller, model, view, …) and it remains.

Thank you once again for the pointers, I will explorer.

maybe share your code?

This is my most complex view with multiple dynamicforms.

Here’s the controller.

public function actionCreate()
{
	$model = new Projects();
	$modelsLegs = [new ProjectsLegs];
	$modelsNotes = [new ProjectsNotes];

	if ($model->load(Yii::$app->request->post())) {
		\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

		$modelsLegs = Model::createMultipleLegs(ProjectsLegs::class);
		Model::loadMultiple($modelsLegs, Yii::$app->request->post());

		$modelsNotes = Model::createMultipleNotes(ProjectsNotes::class);
		Model::loadMultiple($modelsNotes, Yii::$app->request->post());

		// validate all models
		$valid = $model->validate();
		if ( !$valid ) {
			$validationErrors = ''; // '<b>' . $model->Company . '</b><br>';
			foreach ($model->getErrors() as $attribute => $errors) {
				$validationErrors .= '<div class="col-md-offset-1">' . $attribute . ' - ' . implode('<br>', $errors) . '</div>';
			}
			return json_encode([
				'status' => 'Error',
				'message' => '<i class="glyphicon glyphicon-remove"></i> There are problems with the Project entry!',
				'errors' => $validationErrors
			]);
		}
		$valid = Model::validateMultiple($modelsNotes) && $valid;
		if ( !$valid ) {
			return json_encode(['status' => 'Error', 'message' => 'Notes Model Not Valid!']);
		}
		$valid = Model::validateMultiple($modelsLegs) && $valid;
		if ( !$valid ) {
			return json_encode(['status' => 'Error', 'message' => 'Leg Model Not Valid!']);
		}

		if ( $valid ) {
			$transaction = \Yii::$app->db->beginTransaction();
			try {
				if ($flag = $model->save(false)) {
					foreach ($modelsNotes as $modelNote) {
						$modelNote->ProjId = $model->ProjId;
						if (! ($flag = $modelNote->save(false))) {
							$transaction->rollBack();
							return json_encode(['status' => 'Error', 'message' => 'Note not created!']);
							break;
						}
					}

					foreach ($modelsLegs as $modelLeg) {
						$modelLeg->ProjId = $model->ProjId;
						if (! ($flag = $modelLeg->save(false))) {
							$transaction->rollBack();
							return json_encode(['status' => 'Error', 'message' => 'Leg not created!']);
							break;
						}
					}
				}

				if ($flag) {
					$transaction->commit();

					// ***** Attempt saving the InitialRequest value, again since it doesn't work the 1st time? *****
					$modelConIR = $this->findModel($model->ProjId);
					$modelConIR->InitialRequest = $model->InitialRequest;
					$modelConIR->save(false);

					return json_encode([
						'status' => 'Success', 
						'message' => '<i class="glyphicon glyphicon-ok"></i> Project '.$model->ProjNo.' created successfully.',
						]);
				}
			} catch (ErrorException $e) {
				$transaction->rollBack();
				return json_encode(['status' => 'Error', 'message' => '<i class="glyphicon glyphicon-remove"></i> Project not created!']);
			}
		}
		
	} else {
		return $this->renderAjax('create', [
			'model' => $model,
			'modelsNotes' => (empty($modelsNotes)) ? [new ProjectsNotes] : $modelsNotes,
			'modelsLegs' => (empty($modelsLegs)) ? [new ProjectsLegs] : $modelsLegs,
		]);
	}
}

The model validation does not report any issue.

You can see a 3 line resubmission (look for ***** in the code) of just that attribute to make things work.

If you have any ideas I’d be very grateful.

This appears to be Database related, as I just attempted to perform an Insert in the db, it save the records, but once again that one column is blank, so not a Yii issue, but something is off with my table.

So it was definitely a db issue. I had a trigger on the table, and had not added this new field to it, thus it was never getting populated.

Problem solve and not an issue related to Yii. It was a developer issue (Me) in this case :frowning_face:.

3 Likes

Thanks for sharing the real cause.