Yii Blog problem

I just completed the 3.1.2 Customizing rules() Method, but when I tried to insert it, I get this error:


CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`blogdb`.`tbl_post`, CONSTRAINT `FK_post_author` FOREIGN KEY (`author_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `tbl_post` (`title`, `content`, `tags`, `status`) VALUES (:yp0, :yp1, :yp2, :yp3) 

the data I use came from protected\data\schema.mysql.sql

which contains a create code of


CREATE TABLE `tbl_post` (

	`id` INT(11) NOT NULL AUTO_INCREMENT,

	`title` VARCHAR(128) NOT NULL COLLATE 'utf8_unicode_ci',

	`content` TEXT NOT NULL COLLATE 'utf8_unicode_ci',

	`tags` TEXT NULL COLLATE 'utf8_unicode_ci',

	`status` INT(11) NOT NULL,

	`create_time` INT(11) NULL DEFAULT NULL,

	`update_time` INT(11) NULL DEFAULT NULL,

	`author_id` INT(11) NOT NULL,

	PRIMARY KEY (`id`),

	INDEX `FK_post_author` (`author_id`),

	CONSTRAINT `FK_post_author` FOREIGN KEY (`author_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE

)

I’m using Yii 1.1.9

Is there something I did wrong or is it a bug?

Hi. It may be a error of the blog tut.

See this comment:

http://www.yiiframework.com/doc/blog/1.1/en/post.model#c4625

Thanks! I’ll check it out.

I finished Displaying Post and

Now I got this error:


PHP warning


array_map() [<a href='function.array-map'>function.array-map</a>]: Argument #2 should be an array


C:\xampp\htdocs\yii\framework\web\auth\CAccessControlFilter.php(93)


081     {

082         foreach($rules as $rule)

083         {

084             if(is_array($rule) && isset($rule[0]))

085             {

086                 $r=new CAccessRule;

087                 $r->allow=$rule[0]==='allow';

088                 foreach(array_slice($rule,1) as $name=>$value)

089                 {

090                     if($name==='expression' || $name==='roles' || $name==='message')

091                         $r->$name=$value;

092                     else

093                         $r->$name=array_map('strtolower',$value);

094                 }

095                 $this->_rules[]=$r;

096             }

097         }

098     }

099 

100     /**

101      * Performs the pre-action filtering.

102      * @param CFilterChain $filterChain the filter chain that the filter is on.

103      * @return boolean whether the filtering process should continue and the action

104      * should be executed.

105      */


Stack Trace

#0 	

–

 C:\xampp\htdocs\yii\framework\web\auth\CAccessControlFilter.php(93): array_map("strtolower", "users")


88                 foreach(array_slice($rule,1) as $name=>$value)

89                 {

90                     if($name==='expression' || $name==='roles' || $name==='message')

91                         $r->$name=$value;

92                     else

93                         $r->$name=array_map('strtolower',$value);

94                 }

95                 $this->_rules[]=$r;

96             }

97         }

98     }


#1 	

–

 C:\xampp\htdocs\yii\framework\web\CController.php(1145): CAccessControlFilter->setRules(array(array(0 => "allow", "actions" => array("index", "view"), "users" => array("*")), array(0 => "allow", "users" => array("@")), array("deny", "users")))


1140      * @param CFilterChain $filterChain the filter chain that the filter is on.

1141      */

1142     public function filterAccessControl($filterChain)

1143     {

1144         $filter=new CAccessControlFilter;

1145         $filter->setRules($this->accessRules());

1146         $filter->filter($filterChain);

1147     }

1148 

1149     /**

1150      * Returns a persistent page state value.


#2 	

–

 C:\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)


54      * @param CFilterChain $filterChain the filter chain that the filter is on.

55      */

56     public function filter($filterChain)

57     {

58         $method='filter'.$this->name;

59         $filterChain->controller->$method($filterChain);

60     }

61 }


#3 	

–

 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(131): CInlineFilter->filter(CFilterChain)


126     {

127         if($this->offsetExists($this->filterIndex))

128         {

129             $filter=$this->itemAt($this->filterIndex++);

130             Yii::trace('Running filter '.($filter instanceof CInlineFilter ? get_class($this->controller).'.filter'.$filter->name.'()':get_class($filter).'.filter()'),'system.web.filters.CFilterChain');

131             $filter->filter($this);

132         }

133         else

134             $this->controller->runAction($this->action);

135     }

136 }


#4 	

–

 C:\xampp\htdocs\yii\framework\web\CController.php(292): CFilterChain->run()


287             $this->runAction($action);

288         else

289         {

290             $priorAction=$this->_action;

291             $this->_action=$action;

292             CFilterChain::create($this,$action,$filters)->run();

293             $this->_action=$priorAction;

294         }

295     }

296 

297     /**


#5 	

–

 C:\xampp\htdocs\yii\framework\web\CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl"))


261         {

262             if(($parent=$this->getModule())===null)

263                 $parent=Yii::app();

264             if($parent->beforeControllerAction($this,$action))

265             {

266                 $this->runActionWithFilters($action,$this->filters());

267                 $parent->afterControllerAction($this,$action);

268             }

269         }

270         else

271             $this->missingAction($actionID);


#6 	

–

 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(276): CController->run("")


271         {

272             list($controller,$actionID)=$ca;

273             $oldController=$this->_controller;

274             $this->_controller=$controller;

275             $controller->init();

276             $controller->run($actionID);

277             $this->_controller=$oldController;

278         }

279         else

280             throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',

281                 array('{route}'=>$route===''?$this->defaultController:$route)));


#7 	

–

 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(135): CWebApplication->runController("post")


130             foreach(array_splice($this->catchAllRequest,1) as $name=>$value)

131                 $_GET[$name]=$value;

132         }

133         else

134             $route=$this->getUrlManager()->parseUrl($this->getRequest());

135         $this->runController($route);

136     }

137 

138     /**

139      * Registers the core application components.

140      * This method overrides the parent implementation by registering additional core components.


#8 	

–

 C:\xampp\htdocs\yii\framework\base\CApplication.php(162): CWebApplication->processRequest()


157      */

158     public function run()

159     {

160         if($this->hasEventHandler('onBeginRequest'))

161             $this->onBeginRequest(new CEvent($this));

162         $this->processRequest();

163         if($this->hasEventHandler('onEndRequest'))

164             $this->onEndRequest(new CEvent($this));

165     }

166 

167     /**


#9 	

–

 C:\xampp\htdocs\public_html\TOTS\index.php(13): CApplication->run()


08 defined('YII_DEBUG') or define('YII_DEBUG',true);

09 // specify how many levels of call stack should be shown in each log message

10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

11 

12 require_once($yii);

13 Yii::createWebApplication($config)->run();

See "Access Control Filter" of the guide:

http://www.yiiframework.com/doc/guide/1.1/en/topics.auth#access-control-filter

You need to modify the accessRules method of your controller:

from




array("deny",

	"users"

)



to




array("deny",

	"users"=>array('*')

)



You’re right! Thanks.