important part of the way the controller worked. So I will explain
what I learned and hopefully that will help others too.
Where to put the code? I want to prime a field in the add form. I
could put a trigger in the data, but then there would be code in the
database I have to remember. Don't want it in the model and putting it
in the view felt wrong. The Controller is the way to go. I even have
an add function I can put code in.
My baked controller had the following add function for my
Organizations table...
function add() {
if (!empty($this->data)) {
$this->Organization->create();
if ($this->Organization->save($this->data)) {
$this->Session->setFlash(__('The Organization has been saved',
true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Organization could not be saved.
Please, try again.', true));
}
}
}
Pretty simple. However I did not know where to put this code, $this-
>data['Organization']['active'] = 1;? Well, I tried it everywhere not
understanding how the function works. The key was realizing that this
code is called several times. Ah.
When the function is called the code is processed. So it says if the
data is not empty, then process the submit from the form. It worked or
it did not. However, when the code is empty what does the function do?
It opens the form. The default behavior is to display the view
associated with the controller function. (Technically I may be missing
it or using the wrong terminology, chime in with more details if
anyone wishes.) In this case the add.ctp view.
The reason my checkbox would not prime is because I was not getting my
assignment in before the view opened. So I added an else statement
like this...
function add() {
if (!empty($this->data)) {
$this->Organization->create();
if ($this->Organization->save($this->data)) {
$this->Session->setFlash(__('The Organization has been saved',
true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Organization could not be saved.
Please, try again.', true));
}
} else {
//Prime Fields
$this->data['Organization']['active'] = 1;
}
}
Whoo, Hoo. Now we are cooking. I got the sugar, i got the eggs, and
some grease. It is all in there now. So if you need to prime some
fields just add an else and start your form off right.
Only question that remains is what is the 'default' option for in the
automagic fields? Maybe it just does not work for a checkbox? On
another note if you want to reuse your magic fields in your edit and
add views, you have to be careful not to have the default value set
for fields that are valid empty. Otherwise when the user clears a
field, it will default to the default value when you open the form
again. Might not always be the behavior you want.
Thanks eromark for getting me on the right path.
On Jul 26, 9:17 am, "euromark (munich)" <dereurom...@googlemail.com>
wrote:
> file_controller.php in the add function
> is absoluty correct
>
> did you create your form with the form helper?
> <?php echo $form->create('MODELNAME');?>
> etc?
>
> PS: if you are a beginner, always let CAKE bake your models,
> controllers, views!
> this way everything is set up correctly for the cake "magic" to work
>
> On 26 Jul., 15:51, Bart <bart.hardi...@gmail.com> wrote:
>
> > Thanks for the correction. However, I have tried this in several
> > places to no success. I just have some disconnect in how the
> > processing or logic flow works. Where do I need to place this nice
> > simple line of code?
>
> > I have put it in the Add function in several places, the only place
> > that did anything was when I made it the first line of code. Ha, at
> > least something happened. (I created a blank record with active set to
> > 1.) Putting the line before the Create() or the Set() did nothing. I
> > even tried putting the line of code in the add.ctp. That seemed like
> > the wrong place to put it anyway but no check in the check box.
>
> > Should be clear that I am a beginner. I have tried searching the docs
> > and think my problem is terminology. Any links in the docs to set me
> > straight would be very welcome too. Thanks
>
> > On Jul 26, 6:02 am, euromark (München) <dereurom...@googlemail.com>
> > wrote:
>
> > > $this->data['MODELNAME']['active'] = 1;
>
> > > On 26 Jul., 06:34, Bart <bart.hardi...@gmail.com> wrote:
>
> > > > I have an tinyint(1) field in most of my tables named active default
> > > > value is set to null. By default I want the value of this field to be
> > > > 1. However, I cannot get the automagic checkbox to be checked in the
> > > > form. I cannot get the value to prime to 1. Seems simple enough but i
> > > > am still stuck.
>
> > > > I have tried setting the default value in the add.ctp.
>
> > > > echo $form->input('active', array( 'default' => '1'));
> > > > echo $form->input('active', array( 'default' => 1));
> > > > echo $form->input('active', array( 'default' => true));
> > > > echo $form->input('active', array( 'default' => 'true'));
>
> > > > None of these worked. Was not sure of the syntax so I tried everything
> > > > but the sink.
>
> > > > I tried adding a line to the file_controller.php in the add function.
>
> > > > $this->data['file']['active'] = 1;
>
> > > > I could not get it to work this way either. Not sure exactly where in
> > > > the add function the form is actually called or if I am in the right
> > > > direction. So I tried it in several places.
>
> > > > I have also tried changing the default value in the mysql table. Still
> > > > not getting it.
>
> > > > I really think this should be simple but I am floundering. I know I am
> > > > making it too hard. Please point me in the right direction.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment