0

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I'm trying to display a form to create a new Donations within a view that renders Campaigns or rather a single campaign...

It worked fine displaying the Campaigns data passed to the view, but when I added the code to renderPartial I keep getting a PHP notice "Undefined variable: donations".

In the CampaignsController i am using this code:

        public function actionDisplay($id)
    {
            $campaign=$this->loadModel($id);
            $donations=$this->createDonations($campaign);
            $params = array('User_ID'=>$campaign->campaign_user);
            if(Yii::app()->user->checkAccess('displayCampaigns') || Yii::app()->user->checkAccess('displayOwnCampaigns',$params)){
                    $this->parts=array(
                            'banner'=>Images::model()->getImageUrl($campaign->campaign_banner),
                            'avatar'=>Images::model()->getImageUrl($campaign->campaign_avatar),
                            'copy'=>$campaign->campaign_copy,
                            'campaign_name'=>$campaign->campaign_name,
                    );

                    $parts = $this->parts;
                    $this->layout='//layouts/campaignlayout1';
                    $this->render('display',array(
                            'model'=>$campaign,
                            'parts'=>$parts,
                            'donations'=>$donations,
                    ));

            } else {
                    throw new CHttpException(403,"You can only display your own Campaign");                 
            }       

    }

here is the function createDonations() which is also in CampaignsController, and is called by $donations=$this->createDonations($campaign)

        protected function createDonations($campaign) {
            $newdonation = new Donations; 
            if(isset($_POST['Donations'])) {
                    $newdonation->attributes=$_POST['Donations']; 
                    if($issue->addDonations($newdonation)) {
                            Yii::app()->user->setFlash('donationsSubmitted',"Your Donation has been added." );
                            $this->refresh(); 
                    }

            }
            return $newdonation;
    }

In the layout campaignlayout1 I first render data from $campaign and $parts. That worked fine, so I then added a renderPartial() to display the _form from Donations - here is that code:

<?php $this->renderPartial('/donations/_form',array(
            'model'=>$donations, )); ?>

I get a PHP notice: Undefined variable: donations

here is a portion of the stack trace:

/protected/controllers/CampaignsController.php(232): CController->render("display", array("model" => Campaigns, "parts" => array("banner" => "http://imsgonline.com/yiitest/images/genericbanner.jpg", "avatar" => "http://imsgonline.com/yiitest/images/genericmale.jpg", "copy" => "

Send Money

", "campaign_name" => "Joes Fundraising"), "donations" => Donations))

Any idea what I'm doing wrong here??

Community
  • 1
  • 1
dzogchen
  • 383
  • 3
  • 10
  • 1
    You're passing your donations variable as 'model' - this is the name which will be accessible in view, not 'donations'. – Anton Sergeyev Jan 18 '13 at 17:52
  • $donations is passed to the view named "display". I then want to pass it to the renderPartial '/donations/_form' – dzogchen Jan 18 '13 at 18:14

1 Answers1

0

It's a little unclear from your question, but it sounds like the renderPartial is what's causing the undefined variable issue. For reference, the full error message would be useful (i.e. include the line number and file name portions).

Anything you pass to your view via render or renderPartial will have a variable name that's the same as the key in the param array. Note that renderPartial does not inherit anything, even if it's called from a view itself.

For example, you've currently got:

<?php $this->renderPartial('/donations/_form',array(
            'model'=>$donations, )); ?>

In this case, in your view, you'll need to use $model to access whatever you passed via the array (i.e. $donations).

If this renderPartial is in your display view, you're actually setting model to whatever you passed in to that view, in this case donations. Note that this is very confusing as you passed in a model, parts, and donations, and then you're going to call another view that juggles the variable names around.

You're getting an error because your view is trying to use $donations, and you didn't pass anything with "donations" as a key in your array.

ernie
  • 6,356
  • 23
  • 28
  • $donations was passed to the view in the first code chunk: $this->render('display',array( 'model'=>$campaign, 'parts'=>$parts, 'donations'=>$donations, )); – dzogchen Jan 18 '13 at 18:12
  • @dzogchen - if you want the view that you're generating via `renderPartial` to have access, you'll need to pass donations in the `renderPartial` call. It sounds as if you're calling `renderPartial` from your view and expecting variables to be passed automatically. This doesn't happen, and you need to pass the items you need explicitly. I've edited my answer to try and address some of this. – ernie Jan 18 '13 at 18:18