activeDropDownList form database

Hi,

An equivalent topic already exists, but I can’t find a solution.

I’m still playing with the blog demo and, as every blog, I would like to add categories. So, I want to add an [font=“Courier New”]activeDropDownList[/font] to the [font=“Courier New”]Post[/font] create view. And it doesn’t work.

I just added few lines to the actionCreate method :


public function actionCreate()

	{

		$post=new Post;

		if(isset($_POST['Post']))

		{

			$post->attributes=$_POST['Post'];

			if(isset($_POST['previewPost']))

				$post->validate();

			else if(isset($_POST['submitPost']) && $post->save())

				$this->redirect(array('show','id'=>$post->id));

		}

    $categories=Category::model()->findAll();

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

      'post'=>$post, 

      'categories'=>$categories,

    ));

	}

In the view :


<?php echo CHtml::activeDropDownList($post, 'category', 

  CHtml::listData(

    $categories,

    'id',

    'name'

)); ?>

And I get :


PHP Error

Description


Undefined variable: categories

Source File


J:\Developpement\serveur\web\blogyii\protected\backend\views\post\_form.php(21)


00009: </div>

00010: <div class="row">

00011: <?php echo CHtml::activeLabel($post,'content'); ?>

00012: <?php echo CHtml::activeTextArea($post,'content',array('rows'=>20, 'cols'=>50)); ?>

00013: <p class="hint">

00014: You may use <a href="http://daringfireball.net/projects/markdown/syntax" target="_blank">Markdown syntax</a>.

00015: </p>

00016: </div>

00017: <div class="row">

00018: <?php //echo CHtml::activeLabel($post,'category'); ?>

00019: <?php echo CHtml::activeDropDownList($post, 'category', 

00020:   CHtml::listData(

00021: $categories,

00022:     'id',

00023:     'name'

00024: )); ?>

00025: <p class="hint">

00026: Separate different tags with commas.

00027: </p>

00028: </div>

00029: <div class="row">

00030: <?php echo CHtml::activeLabel($post,'tags'); ?>

00031: <?php echo CHtml::activeTextField($post,'tags',array('size'=>65)); ?>

00032: <p class="hint">

00033: Separate different tags with commas.


Stack Trace


#0 J:\Developpement\serveur\yii\framework\web\CBaseController.php(119): require()

#1 J:\Developpement\serveur\yii\framework\web\CBaseController.php(88): PostController->renderInternal()

#2 J:\Developpement\serveur\yii\framework\web\CController.php(701): PostController->renderFile()

#3 J:\Developpement\serveur\web\blogyii\protected\backend\views\post\create.php(6): PostController->renderPartial()

#4 J:\Developpement\serveur\yii\framework\web\CBaseController.php(119): require()

#5 J:\Developpement\serveur\yii\framework\web\CBaseController.php(88): PostController->renderInternal()

#6 J:\Developpement\serveur\yii\framework\web\CController.php(701): PostController->renderFile()

#7 J:\Developpement\serveur\yii\framework\web\CController.php(640): PostController->renderPartial()

#8 J:\Developpement\serveur\web\blogyii\protected\backend\controllers\PostController.php(109): PostController->render()

#9 J:\Developpement\serveur\yii\framework\web\actions\CInlineAction.php(32): PostController->actionCreate()

#10 J:\Developpement\serveur\yii\framework\web\CController.php(300): CInlineAction->run()

#11 J:\Developpement\serveur\yii\framework\web\filters\CFilterChain.php(129): PostController->runAction()

#12 J:\Developpement\serveur\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#13 J:\Developpement\serveur\yii\framework\web\CController.php(952): CAccessControlFilter->filter()

#14 J:\Developpement\serveur\yii\framework\web\filters\CInlineFilter.php(59): PostController->filterAccessControl()

#15 J:\Developpement\serveur\yii\framework\web\filters\CFilterChain.php(126): CInlineFilter->filter()

#16 J:\Developpement\serveur\yii\framework\web\CController.php(283): CFilterChain->run()

#17 J:\Developpement\serveur\yii\framework\web\CController.php(257): PostController->runActionWithFilters()

#18 J:\Developpement\serveur\yii\framework\web\CWebApplication.php(332): PostController->run()

#19 J:\Developpement\serveur\yii\framework\web\CWebApplication.php(120): CWebApplication->runController()

#20 J:\Developpement\serveur\yii\framework\base\CApplication.php(133): CWebApplication->processRequest()

#21 J:\Developpement\serveur\web\blogyii\backend.php(11): CWebApplication->run()

It seems simple, where’s the error?

Kind regards,

Bruno.

Please refer stack. It says that in 6th line of view \views\post\create.php you are calling renderPartial.

Probably you missed pass there $categories into partial view. This renderPartial call should look something similar to following line of code




   $this->renderPartial( '_form',array('categories'=>$categories,'post'=>$post));

 

P.S. Just remember, that when you are using renderPartial, data passed into "parent" view are not automatically enabled in "child" view (here, called by renderPartial).

#?*&#?! you’re right… I totally forgot to check this part. Still learning, still learning.

Thanks a lot.