Yet, model with no primary key

Greetings,

I have trouble loading a model from controller when the database table doesn’t have primary key. Please help… :)


CREATE TABLE `usergroup` (

  `UserName` varchar(64) NOT NULL DEFAULT '',

  `GroupName` varchar(64) NOT NULL DEFAULT '',

  `priority` int(11) NOT NULL DEFAULT '0',

  KEY `UserName` (`UserName`(32))

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

My model class :


class userGroup extends CActiveRecord {

	

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

	

	public function tableName(){

		return 'usergroup';

	}

	

	public function primaryKey(){

    	return 'UserName';

	}

	

	public function rules(){

		return array(

			array('UserName, GroupName', 'required'),

			array('UserName, GroupName', 'length', 'max'=>20),

			array('UserName, GroupName', 'safe', 'on'=>'search'),

		);

	}

	

	public function relations(){

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}

}

My controller class :


class UserGroupController extends Controller

{


	private $_model;


	/**

	 * Displays a particular model.

	 */

	public function actionView()

	{

		$this->render('view',array(

			'model'=>$this->loadModel(),

		));

	}

	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 */

	public function loadModel()

	{

		if($this->_model===null)

		{

			if(isset($_GET['id']))

				$this->_model=userGroup::model()->findbyPk($_GET['id']);

			if($this->_model===null)

				throw new CHttpException(404,'The requested page does not exist.');

		}

		return $this->_model;

	}

}

The error that I get is

where prm1 is the primary key. Im suspecting this has something to do with urlManager, im using the default path urlManager.


		'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),

Thank you for helping me out. :)

from Definitive Guide:

If a table does not have a primary key, it is required that the corresponding AR class specify which column(s) should be the primary key by overriding the primaryKey() method as follows,




public function primaryKey()

{

    return 'id';

    // For composite primary key, return an array like the following

    // return array('pk1', 'pk2');

}



Why don’t you want to define a primary key?

Yes, I did define the primary key column in the Model class. Please refer to my code above.

I always getting error on view / update action… But when I put the following URL, I can get the view page just fine.


http://localhost/webapp/index.php/userGroup/view/id/prm1

So I think it must be the urlManager rule… But I dont really understand how the pattern=>rule works. Any reference?

Because I’m not allowed to modify the database. :(

if is something about urlManager replacing <id:\d+> to <id:\w+> should work




<id:\d+>         ---> \d    Matches a digit [0-9].

<controller:\w+> ---> \w    Matches an alphanumberic character.



Thank you, Flavio.

For now, I settle with only this :


'urlManager'=>array(

	'urlFormat'=>'path',

),

Maybe I’ll try to configure the rules later on. :D