Help - Crud creates form without primary key field

Hello there,

Complete yii newbie here, but I’ve gone through the documentation I could find and searched the forum for crud related things and didnt find an answer, so here goes:

  1. I downloaded, installed and configured yii 1.1.0.r1700.

  2. I created a mysql table:


CREATE TABLE  `roomnavigator`.`Employee` (

`name` VARCHAR( 100 ) NOT NULL ,

`pass` VARCHAR( 100 ) NOT NULL ,

PRIMARY KEY (  `name` )

) ENGINE = INNODB

  1. I ran model Employee (no errors)

  2. I ran crud Employee (no errors)

However, when I now visit http://bla.com/index.php?r=employee/create i’m only allowed to enter ‘pass’. If I enter something into the pass field and click create, I get an error:


Please fix the following input errors:

Name cannot be blank.

Clearly, the model is correct, only the generated crud is wrong…

When I look at the generated _form.php, it also only lists the pass field, but not the name field:


<div class="form">


<?php echo CHtml::beginForm(); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo CHtml::errorSummary($model); ?>


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'pass'); ?>

		<?php echo CHtml::activePasswordField($model,'pass',array('size'=>60,'maxlength'=>100)); ?>

		<?php echo CHtml::error($model,'pass'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php echo CHtml::endForm(); ?>


</div><!-- form -->

Could anyone tell me what I’m doing wrong?

Thank you in advance.

Check your view, there is no specified field for name.

If CRUD didn’t add it for you, then you should do it manually… just add some lines like:




    <div class="row">

        <?php echo $form->labelEx($model,'name'); ?>

        <?php echo $form->textField($model,'name'); ?>

        <?php echo $form->error($model,'name'); ?>

    </div>



I you still have problems take a look to iCRUD. If you want to use iCRUD you need to download Yii 1.1.1-dev from SVN repo.

Best regards.

The ‘name’ field is not created in the form, because it is a primary key (I think). In most cases you will have the DB or application to deal with the primary key.

Just add an id column whether you need it or not. Set the id column to be the primary key (not null and autoincrement). Regenerate your model, then regenerate your crud.

Thank you for all the replies! So actually, it appears this is working as intended because crud is assuming i have an autoincrement id field when I don’t? Thats not a good assumption and I’d consider that a bug, crud could check for that and act accordingly…

Since I’m just starting out I’ll give the 1.1.1 version a try and install iCRUD.

Else I’ll add it manually as you guys suggested (though I was hoping to use crud to prevent exactly that ;))

Thanks again!

I agree that perhaps the crud shouldn’t make assumptions about the primary key like that.

I recently had the same problem with a country table where I wanted to use the (ISO 3166-1 alpha-2) 2 letter country codes as the primary key, since they are natively unique anyway.

For ease of use I ended up adding a redundant ID field in the same way, but it is just that - redundant.

A simple check to see if the primary key is auto-incremented before generating the appropriate code would probably be all that is needed.