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.