[SOLVED] Using controller to define db input

I am trying to define db content in the create action of a form. Here is the snippet from my controller. Yes it is just a piece of the code but only thing I see relevant to the question.


$model->attribute1 = 'Y';  // does not work

$model->save(false);

$model2->Id = $model->Id;  // works

$model2->save(false);

If I take


$model->attribute1 = 'Y';

out and use the form itself everything works but I need this to take place in the controller.

Any ideas?

How do you know it does not work?

Have you tried with


$model->attribute1 = 'Y';

echo $model->attribute1;

Could be that you assign some other value (or delete the current one) in the beforeSave()

mdomba, echoing it does not render anything. I don’t see anywhere that it could be written over as I am not using any special code yet, just basic CRUD with multi model form functionality. I am lost as how to troubleshoot this though.

If you just echo the code continues to execute and probably the whole page is rendered so it’s difficult to see where the value is echoed… try to put die() after the echo…

What is "attribute1"? Do you have a filed with that name in the table of that model?

Ok by adding die() I can now see it. attribute1 is just a sudoname to post on here. I am using my actual names in my code and yes they do match code/db. Like I said if I just use my form I do not have any problems. Any ideas why my code is not writing it to the db?

So you see now that the assignment works… so it must be something else…

One possibility is that this attribute gets a new value somewhere like beforesave()…

Another possibility is that this is a field in the table that cannot get the value you are assigning… like the filed is INTEGER and you are giving it a STRING…

Yes it is good to see that it is actually there but not so good it is not writing.

My db field for that attribute is char(1).

Rules in my model for it are ‘required’ and ‘length’, ‘max’=>1

For beforeSave() to exist I have to write it in, correct? Like I said I am just working with basic code at this point. I am using giix (extension) generated code not gii but still do not see any implicit beforeSave() calls written in there either.

And without a beforeSave() function in the mix, and my sequential statements in the controller of defining the attribute and then saving it is confusing me.

I’m just writing you the possibilities… you are the one that has the code and the database… and need to find what is wrong… I can only guess and give you ideas on what to check…

Could be some other value that is the reason why this record is not saved… .check the result of the save to see if it returns true or false…

check the SQL generated/executed… try to run it manually from phpmyadmin for example…

Yes I know I am the only one with eyes on the actual app and I do appreciate your suggestions. I come here to ask the question only after I have done everything I know how to in trying to figure out the problem. I am sure I am not the first to hard code attribute values into the controller so of course I wanted to make sure my code is correct.

I have logging turned on and enableParamLogging but the logs are not revealing anything. When I submit the form it only bounces the page and nothing happens. When a form does not work I have yet to see anything returned. Am I missing something here?

You did not post any code for the form… so I cannot help you here…

Did you try to validate the model? Did you try to execute manually the SQL ?

what about




var_dump($model->getErrors());



after saving?

I do not know if you are asking something different from what I meant when I said it works just fine if I assign that attribute through the form itself. Also manual SQL works just fine.

Just to try something different I kept that line of code in the controller and sent an invalid value through the form. Well the record was created and the value of the attribute was that which is defined in the controller and not what I entered in the form. Of course when I remove that statement in the controller and input that same invalid value in the form I then get a SQL error.

Therefore that value as defined in the controller is only accepted when some value is passed by the form.

This table only includes that attribute and an auto_increment ID so I removed any reference to $model (i.e. errorSummary in _form and ‘model’ => $model, in renderPartial of create) since the only assigned attribute would come from the controller, but that did not seem to have any affect on it.

Hi mbi, I tried this also and no errors appear. :(

I really did not understand all this “with form/without form” thing… and I’m a bit out of ideas… and you did not answer some questions… so I’m not sure you did try them…

Did you validate the model somewhere as by using save(false) you are not validating it… why don’t you use save() so that the validation is done… and than you can check if there are some validation error…

And did you check the result of save()… did you get true or false?

Something like




if(save())        	// <- without false, so that the validation is done

	echo "saved";

else

{

	echo "not saved";

	var_dump($model->getErrors());

}






$model->save(false); //false means no validation, thats why there are no errors



Taking your advice and that from mdomba I changed things hopefully show any errors. Still nothing appears.

Seems weird that as I mentioned a couple of posts back that it only works to overwrite input not directly input what I set it to.

Not sure if I am doing something wrong but taking your advice above I can not get any message to be rendered (i.e. saved / not saved). When I mention using a form or no form I am actually talking about using the generated gii content in the views of create.php and _form.php

you need again to put a die() after the if else… so that you can see the output…

Yes I also put that in there. I really do appreciate your help here.

Although I can not send code from this app directly I can recreate the problem in another file and post that on here with the schema if you can throw it into a test app so hopefully you can directly see what I am seeing. Let me know if you want me to put that together.

Fine for me… I would really like to find what is wrong here…

So did you get "saved" or "not saved" ?

One of these should be echoed…