How to save multiple form in the same table like phpmyadmin - cakephp

I'm working with CakePHP version 2.3
I've been wondering how to save a looped form like phpmyadmin does when you input multiple form for 1 table.
Let's say that I have a table products with id, name and content columns.
In order to save my data, I make a form with this 3 inputs multiplied by 3.
So that gives me something like this :
<?php echo $this->Form->create('Product'); ?>
<!-- Form for product 1 -->
<?php echo $this->Form->input('name', array('label' => "Name")); ?>
<?php echo $this->Form->input('content', array('label' => "Content")); ?>
<?php echo $this->Form->input('id'); ?>
<!-- Form for product 2 -->
<?php echo $this->Form->input('name', array('label' => "Name")); ?>
<?php echo $this->Form->input('content', array('label' => "Content")); ?>
<?php echo $this->Form->input('id'); ?>
<!-- Form for product 3 -->
<?php echo $this->Form->input('name', array('label' => "Name")); ?>
<?php echo $this->Form->input('content', array('label' => "Content")); ?>
<?php echo $this->Form->input('id'); ?>
<?php echo $this->Form->end('done'); ?>
Of course this can't work and I have no clue to how to get this done.
Does anybody could show me how it works?
Thank you very much for your help.

Do the following calls to input() method:
...
echo $this->Form->input('Product.0.name', array('label' => 'Name'));
echo $this->Form->input('Product.0.content', array('label' => 'Content'));
echo $this->Form->input('Product.1.name', array('label' => 'Name'));
echo $this->Form->input('Product.1.content', array('label' => 'Content'));
echo $this->Form->input('Product.2.name', array('label' => 'Name'));
echo $this->Form->input('Product.2.content', array('label' => 'Content'));
...
The previous code should output the following form elements
<input type="text" id="Product0name" name="data[Product][0][name]">
<input type="teaxtarea" id="Product0Content" name="data[Product][0][Content]">
<input type="text" id="Product1name" name="data[Product][1][name]">
<input type="teaxtarea" id="Product1Content" name="data[Product][1][Content]">
<input type="text" id="Product2name" name="data[Product][2][name]">
<input type="teaxtarea" id="Product2Content" name="data[Product][2][Content]">
which can posted and saved by your controller using saveAll() method very easily.
Also check out the CakePHP docs at: Field-naming-conventions

Related

User doesn't have user id after login

I can log in a user using Auth->login($this->data). Debugging the Auth->user() after login all I see is the username and password. Is the Id supposed to be auto populated or do I need a way to manually set it? By going off a few tutorials it seem like the ID should already be there.
What is debugged:
\app\Controller\UsersController.php (line 90)
array(
'User' => array(
'password' => '*****',
'username' => 'LittleRy'
)
)
As ndm comment, I changed the call to Auth->login() so it should be grabbing the request info. With this change login() is always returning false. Below is the login form. I believe everything is being passed in correctly.
<div class="users form">
<?php echo $this->Session->Flash('auth');
?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
try this $this->Auth->user('id'); to get user id.

CakePHP 2.x HABTM saveAll with Add view associated model optgroups

I have a CakePHP Add Photo View that is using optgroups so that the HABTM related Tag model's data is shown in grouped fashion in the view. In the Photo controller I send the related Tag model data as follows to get the optgroups grouping of Tags by Tag Category:
//fields shown to create optgroups eg group tags by category
$this->set('tags', $this->Photo->Tag->find('list', array(
'fields' => array('Tag.id', 'Tag.tagname', 'Tag.category'),
'conditions' => array(
'Tag.account_id' => $this->Session->read('Auth.User.account_id')))));
}
The view shows the Tags nicely grouped by Category. So that part works perfectly.
However, after adding a new Photo and selecting one or more Tags to assign to it, it does save the Photo properly but not the assigned Tags.
I believe that CakePHP saveAll is expecting a data array as would be delivered without the optgroup Category > Tag hierarcy, and is therefore not saving Tag data when new Photo is added and Tags are assigned to the Photo.
What do I need to do to give it the proper array to save the related Tags? Will it be in the controller after Create() where I 'remove' the Category grouping?
A bit of background on this from CakePHP http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
"If you would like to generate a select with optgroups, just pass data in hierarchical format. This works on multiple checkboxes and radio buttons too, but instead of optgroups wraps elements in fieldsets:"
$options = array(
'Group 1' => array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
),
'Group 2' => array(
'Value 3' => 'Label 3'
)
);
echo $this->Form->select('field', $options);
Output:
<select name="data[User][field]" id="UserField">
<optgroup label="Group 1">
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
</optgroup>
<optgroup label="Group 2">
<option value="Value 3">Label 3</option>
</optgroup>
</select>
Ok so this problem had nothing to do with the array structure.
It turns out I had 'Photo.Tag' instead of just 'Tag' in the Add Photo View. Here is the old Add Photo View code that didn't work:
<div class="photos form">
<p><?php echo $this->element('admin_nav'); ?></p>
<?php echo $this->Form->create('Photo'); ?>
<fieldset>
<legend><?php echo __('Add Photo'); ?></legend>
<?php
echo $this->Form->input('account_id', array('type'=>'hidden'));
//echo $this->Form->input('filename', array('type'=>'hidden'));
echo $this->Form->input('filename', array('type' => 'file', 'label' => 'Upload photo'));
echo $this->Form->input('desc');
echo $this->Form->input('Photo.Tag' , array('label'=>'Tags', 'multiple'=>'checkbox'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
Here is the new Add Photo View code which now saves the new photo with the tags selected:
<div class="photos form">
<p><?php echo $this->element('admin_nav'); ?></p>
<?php echo $this->Form->create('Photo'); ?>
<fieldset>
<legend><?php echo __('Add Photo'); ?></legend>
<?php
echo $this->Form->input('account_id', array('type'=>'hidden'));
//echo $this->Form->input('filename', array('type'=>'hidden'));
echo $this->Form->input('filename', array('type' => 'file', 'label' => 'Upload photo'));
echo $this->Form->input('desc');
echo $this->Form->input('Tag' , array('label'=>'Tags', 'multiple'=>'checkbox'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
While the Cakephp documentation on Saving Data makes references to how the array should be formatted, this appears to refer to all of the other relationships excluding HABTM. HABTM relationships can be saved in one action with the save on the parent model, in this case the Photo model eg:
$this->Photo->save($this->request->data)
will save the new Photo being added, plus any of the selected Tags, and they will be saved in the join table photostags.
What is a bit unusual, is that $this->Photo->save($this->request->data) appears to work same as $this->Photo->saveAll($this->request->data).

How to get edit view fields not to populate with existing data

I have got the following code in my edit.ctp:
<div class="employees form">
<?php echo $this->Form->create('Employee'); ?>
<fieldset>
<legend><?php echo __('Edit Employee Details'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('employee_name', array('required'=>false));
echo $this->Form->input('date_hired',array('required'=>false,'id'=>'datepicker','type'=>'text'));
echo $this->Form->input('employee_phone_number', array('required'=>false));
echo $this->Form->input('employee_email', array('required'=>false));
echo $this->Form->input('employee_address');
echo $this->Form->input('employee_dob',array('required'=>false,'id'=>'datepicker2','type'=>'text'));
echo $this->Form->input('access_level', array('required'=>false, 'options' => array('admin' => 'Admin', 'staff' => 'Staff')));
echo $this->Form->input('employee_username', array('required'=>false));
echo $this->Form->input('employee_pw', array('required'=>false));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
At the moment when I go in to the edit view, all fields get populated with existing data. I want the password field to be kept blank even if a password already exists. How can I do that?
echo $this->Form->input('employee_pw', array('type'=>'password', 'value'=>'', 'autocomplete'=>'off'));

Form submit button losing value attribute in CakePHP

We have baked a crud with validations. All seems ok.
However, when we hit the submit button,
<?php echo $this->Form->submit('go for it'); ?>
and if we get validation errors, the HTML submit button loses is value attribute.
Before hitting submit we get this output:
<input type="submit" value="go for it"/>
After hitting submit we get this output:
<input type="submit" value></input>
Here's the full ctp code:
<?php echo $this->Form->create('ScheduleAccess'); ?>
<?php echo $this->Form->input('id'); ?>
<fieldset class="short">
<legend>Bla bla</legend>
<?php echo $this->Form->input('schedule_es', array('label' => 'Bla bla (es)')); ?>
<?php echo $this->Form->input('schedule_en', array('label' => 'Ble ble (en)')); ?>
</fieldset>
<fieldset class="short">
<legend>Last Bla bla</legend>
<?php echo $this->Form->input('last_entry', array(
'dateFormat' => 'YMD',
'timeFormat' => '24',
'label'=>'until:'
)); ?>
</fieldset>
<fieldset class="full">
<legend>terms</legend>
<div class="medium wysihtml5">
<?php echo $this->Form->input('free_entry_day_time_es', array('label' => 'Things (es)')); ?>
<?php echo $this->Form->input('free_entries_and_discounts_es', array('label' => 'More things (es)')); ?>
</div>
<div class="medium wysihtml5">
<?php echo $this->Form->input('free_entry_day_time_en', array('label' => 'Things (en)')); ?>
<?php echo $this->Form->input('free_entries_and_discounts_en', array('label' => 'More things (en)')); ?>
</div>
</fieldset>
<?php //echo $this->Element('admin/save'); ?>
<?php echo $this->Form->submit('go for it'); ?>
<?php echo $this->Form->end(); ?>
Has anyone found a similar issue?
just for grin's try this
<?php echo $this->Form->end('Go for it'); ?>
end can provide the submit button. If this works then you probably have something overwriting the value of the submit. I'd start looking in your $this->request->data to see what its sending over to the view.
Hope this helps.

Getting Cake PHP to output a HTML submit button with a given class name

I can get Cake to output a submit button using the following PHP:
<?php echo $this->Form->end(__('Submit')); ?>
which outputs this HTML:
<input type="submit" value="Submit">
but I want to use a specific input class to get the following:
<input type="submit" value="Submit" class="some class">
is this possible?
Thank you :).
Yes it's possible :
<?php
echo $this->Form->submit(__('Submit',true), array('class'=>'some class'));
echo $this->Form->end();
?>
Which is documented here.
This should do the trick:
<?php echo $this->Form->end(array('label' => __('Submit', true), 'class' => 'some class')); ?>

Resources