CRUD Operations fails on example table "tbl_user"

Hi,

I try to generate crud methods from the quickstart docs: http://www.yiiframework.com/doc/guide/quickstart.first-app but it fails complaining

about missing primary key.

First I created the table "tbl_user" from the mysql example.

Then in the yiic shell:

model User tbl_user

crud User

Fails with the error : "Error: Table "tbl_user" does not have a primary key."

My table looks like this:

mysql> describe tbl_user ;

±---------±-------------±-----±----±--------±---------------+

| Field | Type | Null | Key | Default | Extra |

±---------±-------------±-----±----±--------±---------------+

| id | int(11) | NO | PRI | NULL | auto_increment |

| username | varchar(128) | NO | | NULL | |

| password | varchar(128) | NO | | NULL | |

| email | varchar(128) | NO | | NULL | |

php version is 5.2.8 and mysql version is 5.1.45

Any ideas ?

thanks ,

ola

You need to set the id as a primary key (with auto increment)

I created the table primary key , and autoincrement:

CREATE TABLE tbl_user (

id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,


username VARCHAR(128) NOT NULL,


password VARCHAR(128) NOT NULL,


email VARCHAR(128) NOT NULL

);

In the CrudCommand.php class, the primary key is null:

    public function generateController($source,$params)


    {


            list($controllerClass,$modelClass)=$params;


            $model=CActiveRecord::model($modelClass);





            $id=$model->tableSchema->primaryKey;





                  // ---------------------------------


                 // This code fails throws exception:


                 // ---------------------------------


            if($id===null)


                    throw new ShellException(Yii::t('yii','Error: Table "{table}" does not have a primary key.',array('{table}'=>$model->tableName())));

ola

Tried to debug some more: Wrote a small test to retreive metadata from the primary key, and can’t see anything wrong:

$DB = new PDO("mysql:host=$hostname;dbname=mytestdatabase", $username, $password);


$select = $DB->query('SELECT id FROM tbl_user');


$meta = $select->getColumnMeta(0);


var_dump ( $meta);

Output is

array(7) {

["native_type"]=>

string(4) "LONG"

["flags"]=>

array(2) {

[0]=>


string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> &quot;not_null&quot;


[1]=&gt;


string(11) &quot;primary_key&quot;

}

["table"]=>

string(8) “tbl_user”

["name"]=>

string(2) "id"

["len"]=>

int(11)

["precision"]=>

int(0)

["pdo_type"]=>

int(2)

}

That’s very strange. Could you execute SQL “SHOW COLUMNS FROM tbl_user” and see the results?

Could you also exit the "yiic shell" and re-enter it to generate the model class?

Hi,

I’m using mysq:

mysql> desc tbl_user ;

±---------±-------------±-----±----±--------±---------------+

| Field | Type | Null | Key | Default | Extra |

±---------±-------------±-----±----±--------±---------------+

| id | int(11) | NO | PRI | NULL | auto_increment |

| username | varchar(128) | NO | | NULL | |

| password | varchar(128) | NO | | NULL | |

| email | varchar(128) | NO | | NULL | |

±---------±-------------±-----±----±--------±---------------+

ola


Dear use crud tbl_user instead of crud user, look on your data base table that how it is defined in database, your crud query depend on your table name.

I kind of get it to work with just using the table name:

model tbl_user

crud tbl_user

but the app blows up when i try to create a new user. And it also require me to type the auto increment id in the create user form wich

is not correct.

PHP Error

Description

Cannot modify header information - headers already sent by (output started at /usr/local/yii/framework/db/ar/CActiveRecord.php:327)