1

I'm new to CakePHP and still really have no thorough understanding. if I have a mysql database with only a primary key field how can I make an input field for it using cake bake option. After baking I tried adding this line to the view file generated

<fieldset>
<legend><?php echo __('Add Id'); ?></legend>
<?php
        **echo $this->Form->input('identity');**
?>
</fieldset>

It did not work ... What is the correct method to get an input field for the primary key?

The table id has only one field named identity in the data base

12
  • The table id has only one field named identity in the data base Commented Mar 16, 2013 at 3:52
  • do you want to say that textbox is not getting displayed ... ? Commented Mar 16, 2013 at 4:03
  • yes I can not get the input text box to appear Commented Mar 16, 2013 at 4:37
  • have you created the form first .. ? Commented Mar 16, 2013 at 4:51
  • <div class="ids form"> <?php echo $this->Form->create('Id'); ?> <fieldset> <legend><?php echo __('Edit Id'); ?></legend> <?php echo $this->Form->input('identity'); ?> </fieldset> <?php echo $this->Form->end(__('Submit')); ?> </div> is this what you asked? I just started using cakephp Commented Mar 16, 2013 at 4:52

2 Answers 2

3

CakePHP will, by default, output primary key fields as a hidden input. You can override it by specifying the 'type' of input to use, e.g.:

echo $this->Form->input('identity', array('type' => 'text'));

However, CakePHP supports two type of Primary Keys; autoNumber integers and GUID/UUID primary keys. Manually setting primary keys (e.g. use 'username' as primary key and trying to insert or edit the username) may cause problems, because CakePHP assumes that an update should be performed if the primary is not empty in your form.

http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions

Sign up to request clarification or add additional context in comments.

Comments

1

Change your statement to this:

echo $this->Form->input('id', array ('type'=>'text'));   

You should now have the id in full view.

CakePHP used the id or index, to determine, if the form entry is a new entry (ie INSERT) or an update to the existing extry (ie UPDATE).

If you make the index editable, you will have to deal with this issue, else you can end up having multiple records, one with the original record and a second record with the changes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.