I use testdrive application with the following modifications
-
I have all tables in InnoDB
-
I added a number of models and controllers and views using crud command
-
I've changed UserIdentity to the following:
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
protected $_id;
/**
* Validates the username and password.
* This method should check the validity of the provided username
* and password in some way. In case of any authentication failure,
* set errorCode and errorMessage with appropriate values and return false.
* @param string username
* @param string password
* @return boolean whether the username and password are valid
*/
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record === null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password !== md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id = $record->userID;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
if (empty($this->_id)) return $this->username;
return $this->_id;
}
public function setId($id)
{
$this->_id = $id;
}
}
(I want Yii::app()->user->getId() method to return primary key, not username)
Now I log in, then go to the index.php?r=category (category controller used fur crud operations on Category table (see below))
Then go to category/create
it brings me the form with name, clicks number and gender.
I can't put anything to the gender field, but that's not problem, I know how to fix this.
So, I out category name, number of cliks and press "create", but instead of creating record, it takes me to the login page!
Whe I log in, everything is the same - when clicking "create" it takes me to the login page and terminates the session.
Why???
At the same time, if I do THE SAME for shop or tag controllers - everything goes well!
Here is code (sorry for posting so much):
- CategoryController
<?php
class CategoryController extends CController
{
/**
* @var string specifies the default action to be 'list'.
*/
public $defaultAction='list';
/**
* Specifies the action filters.
* This method overrides the parent implementation.
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method overrides the parent implementation.
* It is only effective when 'accessControl' filter is enabled.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('deny', // deny access to CUD for guest users
'actions'=>array('create','update','delete'),
'users'=>array('?'),
),
);
}
/**
* Lists all categorys.
*/
public function actionList()
{
$pages=$this->paginate(Category::model()->count());
$categoryList=Category::model()->findAll($this->getListCriteria($pages));
$this->render('list',array(
'categoryList'=>$categoryList,
'pages'=>$pages));
}
/**
* Shows a particular category.
*/
public function actionShow()
{
$this->render('show',array('category'=>$this->loadCategory()));
}
/**
* Creates a new category.
* If creation is successful, the browser will be redirected to the 'show' page.
*/
public function actionCreate()
{
$category=new Category;
if(Yii::app()->request->isPostRequest)
{
if(isset($_POST['Category']))
$category->setAttributes($_POST['Category']);
if($category->save())
$this->redirect(array('show','id'=>$category->categoryID));
}
$this->render('create',array('category'=>$category));
}
/**
* Updates a particular category.
* If update is successful, the browser will be redirected to the 'show' page.
*/
public function actionUpdate()
{
$category=$this->loadCategory();
if(Yii::app()->request->isPostRequest)
{
if(isset($_POST['Category']))
$category->setAttributes($_POST['Category']);
if($category->save())
$this->redirect(array('show','id'=>$category->categoryID));
}
$this->render('update',array('category'=>$category));
}
/**
* Deletes a particular category.
* If deletion is successful, the browser will be redirected to the 'list' page.
*/
public function actionDelete()
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadCategory()->delete();
$this->redirect(array('list'));
}
else
throw new CHttpException(500,'Invalid request. Please do not repeat this request again.');
}
/**
* Loads 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.
*/
protected function loadCategory()
{
if(isset($_GET['id']))
$category=Category::model()->findbyPk($_GET['id']);
if(isset($category))
return $category;
else
throw new CHttpException(500,'The requested category does not exist.');
}
/**
* @param CPagination the pagination information
* @return CDbCriteria the query criteria for Category list.
* It includes the ORDER BY and LIMIT/OFFSET information.
*/
protected function getListCriteria($pages)
{
$criteria=Yii::createComponent('system.db.schema.CDbCriteria');
$columns=Category::model()->tableSchema->columns;
if(isset($_GET['sort']) && isset($columns[$_GET['sort']]))
{
$criteria->order=$columns[$_GET['sort']]->rawName;
if(isset($_GET['desc']))
$criteria->order.=' DESC';
}
$criteria->limit=$pages->pageSize;
$criteria->offset=$pages->currentPage*$pages->pageSize;
return $criteria;
}
/**
* Generates the header cell for the specified column.
* This method will generate a hyperlink for the column.
* Clicking on the link will cause the data to be sorted according to the column.
* @param string the column name
* @return string the generated header cell content
*/
protected function generateColumnHeader($column)
{
$params=$_GET;
if(isset($params['sort']) && $params['sort']===$column)
{
if(isset($params['desc']))
unset($params['desc']);
else
$params['desc']=1;
}
else
{
$params['sort']=$column;
unset($params['desc']);
}
$url=$this->createUrl('list',$params);
return CHtml::link(Category::model()->getAttributeLabel($column),$url);
}
}
- Category model
<?php
class Category extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* This method is required by all child classes of CActiveRecord.
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'Category';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('name','length','max'=>100),
array('name, clicksNum, gender', 'required'),
array('clicksNum', 'numerical', 'integerOnly'=>true),
);
}
// -----------------------------------------------------------
// Uncomment the following methods to override them if needed
public function relations()
{
return array(
'sizes'=>array(self::MANY_MANY, 'SizeCategory', 'CategorySizeCategory(categoryID, sizeCategoryID)', 'order' => 'sizeCategory'),
'products'=>array(self::HAS_MANY , 'Product', 'categoryID', 'order' => 'name'),
'tags'=>array(self::HAS_MANY , 'CategoryTag', 'categoryID', 'order' => '??.clicksNum', 'limit' => Yii::app()->params['maxCategoryTags']),
'productsNum'=>array(self::HAS_MANY , 'Product', 'categoryID', 'select' => 'count(??.productID)'),
);
}
/*public function attributeLabels()
{
return array(
'authorID'=>'Author',
);
}
public function protectedAttributes()
{
return array();
}
*/
}
- SHOW CREATE TABLE Category:
CREATE TABLE `Category` (
`categoryID` int(5) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`clicksNum` int(11) NOT NULL DEFAULT '0',
`gender` enum('male','female','boys','girls') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'female',
PRIMARY KEY (`categoryID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
- TagController
<?php
class TagController extends CController
{
/**
* @var string specifies the default action to be 'list'.
*/
public $defaultAction='list';
/**
* Specifies the action filters.
* This method overrides the parent implementation.
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method overrides the parent implementation.
* It is only effective when 'accessControl' filter is enabled.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('deny', // deny access to CUD for guest users
'actions'=>array('create','update','delete'),
'users'=>array('?'),
),
);
}
/**
* Lists all tags.
*/
public function actionList()
{
$pages=$this->paginate(Tag::model()->count());
$tagList=Tag::model()->findAll($this->getListCriteria($pages));
$this->render('list',array(
'tagList'=>$tagList,
'pages'=>$pages));
}
/**
* Shows a particular tag.
*/
public function actionShow()
{
$this->render('show',array('tag'=>$this->loadTag()));
}
/**
* Creates a new tag.
* If creation is successful, the browser will be redirected to the 'show' page.
*/
public function actionCreate()
{
$tag=new Tag;
if(Yii::app()->request->isPostRequest)
{
if(isset($_POST['Tag']))
{
$tag->setAttributes($_POST['Tag']);
$tag->dtAdded = date('Y-m-d H:i:s');
$tag->userID = Yii::app()->user->getId();
}
if($tag->save())
$this->redirect(array('show','id'=>$tag->tagID));
}
$this->render('create',array('tag'=>$tag));
}
/**
* Updates a particular tag.
* If update is successful, the browser will be redirected to the 'show' page.
*/
public function actionUpdate()
{
$tag=$this->loadTag();
if(Yii::app()->request->isPostRequest)
{
if(isset($_POST['Tag']))
$tag->setAttributes($_POST['Tag']);
if($tag->save())
$this->redirect(array('show','id'=>$tag->tagID));
}
$this->render('update',array('tag'=>$tag));
}
/**
* Deletes a particular tag.
* If deletion is successful, the browser will be redirected to the 'list' page.
*/
public function actionDelete()
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadTag()->delete();
$this->redirect(array('list'));
}
else
throw new CHttpException(500,'Invalid request. Please do not repeat this request again.');
}
/**
* Loads 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.
*/
protected function loadTag()
{
if(isset($_GET['id']))
$tag=Tag::model()->findbyPk($_GET['id']);
if(isset($tag))
return $tag;
else
throw new CHttpException(500,'The requested tag does not exist.');
}
/**
* @param CPagination the pagination information
* @return CDbCriteria the query criteria for Tag list.
* It includes the ORDER BY and LIMIT/OFFSET information.
*/
protected function getListCriteria($pages)
{
$criteria=Yii::createComponent('system.db.schema.CDbCriteria');
$columns=Tag::model()->tableSchema->columns;
if(isset($_GET['sort']) && isset($columns[$_GET['sort']]))
{
$criteria->order=$columns[$_GET['sort']]->rawName;
if(isset($_GET['desc']))
$criteria->order.=' DESC';
}
$criteria->limit=$pages->pageSize;
$criteria->offset=$pages->currentPage*$pages->pageSize;
return $criteria;
}
/**
* Generates the header cell for the specified column.
* This method will generate a hyperlink for the column.
* Clicking on the link will cause the data to be sorted according to the column.
* @param string the column name
* @return string the generated header cell content
*/
protected function generateColumnHeader($column)
{
$params=$_GET;
if(isset($params['sort']) && $params['sort']===$column)
{
if(isset($params['desc']))
unset($params['desc']);
else
$params['desc']=1;
}
else
{
$params['sort']=$column;
unset($params['desc']);
}
$url=$this->createUrl('list',$params);
return CHtml::link(Tag::model()->getAttributeLabel($column),$url);
}
}
- Tag model
<?php
class Tag extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* This method is required by all child classes of CActiveRecord.
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'Tag';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('text','length','max'=>100),
array('text, dtAdded, userID, clicksNum, isCategory', 'required'),
array('userID, clicksNum, isCategory', 'numerical', 'integerOnly'=>true),
);
}
// -----------------------------------------------------------
// Uncomment the following methods to override them if needed
/*
public function relations()
{
return array(
'author'=>array(self::BELONGS_TO, 'User', 'author_id'),
'comments'=>array(self::HAS_MANY, 'Comment', 'post_id', 'with'=>'author', 'order'=>'create_time DESC'),
'tags'=>array(self::MANY_MANY, 'Tag', 'post_tag(post_id, tag_id)', 'order'=>'name'),
);
}
public function attributeLabels()
{
return array(
'authorID'=>'Author',
);
}
public function protectedAttributes()
{
return array();
}
*/
}
- SHOW CREATE TABLE Tag:
CREATE TABLE `Tag` ( `tagID` int(11) NOT NULL AUTO_INCREMENT, `text` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `dtAdded` datetime NOT NULL, `userID` int(11) NOT NULL, `clicksNum` int(11) NOT NULL DEFAULT '0', `isCategory` tinyint(4) NOT NULL DEFAULT '0', PRIMARY KEY (`tagID`), KEY `fk_Tag_User` (`userID`), CONSTRAINT `fk_Tag_User` FOREIGN KEY (`userID`) REFERENCES `User` (`userID`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Please help with this!