Search code examples
zfcuser

How to Pass value from controller to view in zend framework2


i am beginner to zend.I dont no how to pass vale from controller to update by table using ZfcUser in zend framework2.Here is my code in UserController.php

public function doneAction()

   {
    $user = "4";
    $planname="checking";
    $billamount="$89";
    $post=array("planname"=>$planname,"billamount"=>$billamount);
    $service = $this->getUserService();
    $service->done($user,$post);
    return new ViewModel();
}

And in ZfcUser/Serivce/User.php [For storing the value in DB]

public function done($user, array $post)

{

    $data=array('planname'=>$post['planname'],'billamount'=>$post['billamount']);
    $where = $user->getAdapter()->quoteInto('user_id = 4');
    $user->update($data, $where);
return true;
}

Thanks


Solution

  • You can pass values to your view from the controller when creating a new ViewModel instance like so:-

        $view = new ViewModel(array(
            'user' => $user,
        ));
        return new ViewModel();
    

    Alternatively you can set them against the instance:-

        $view = new ViewModel();
        $view->user = $user;
        return $view;