简体   繁体   中英

Print variable from action zend2 framework

I am trying to print a variable fromm saveAction so i can see what is the value of that variable. I am learning Zend2 and i have continued where other developer stoped so i am trying to understand it better...This application is also using Doctrin.

I have this action which will save some data to database.

public function saveAction() {
    $view = new ViewModel();
    $logedUser = $this->getLogedUser();

    print_r($_POST);

    $shopId = (int) $this->params()->fromPost('shop_id', null);

    print_r($shopId);

    $shop = $this->getServiceLocator()
                    ->get('Catalog\Model\Shop')
                    ->getRepository()
                    ->findOneBy(array('id' => $shopId, 'user' => $logedUser->getId()));
    print_r($shop);

    return $logedUser;
    return $shop;
    return $view;
} 

I can print values from post, and variable shopId, but print_r($shop); doesn't show anything. How can i see the value of shop variable?

尝试这个

   echo $shop->__toString();

Hi there you might want to try this one:

print_r($this->getAllParams());

instead of using $_POST

$shop = $this->getServiceLocator()
->get('Catalog\Model\Shop')
->findOneBy(array('id' => $shopId, 'user' => $logedUser->getId()));

please remove ->getRepository() from your code, then you will get your result.

At this point we dont know what the Service/Factory 'Catalog\\Model\\Shop' does. I assume it get's the Entity manager. The query looks ok to me 2. But having 3 return's is just wrong!

Your view will never have the $shop; nor the $view; variable in your .phtml view File. In ZF2 you can just return a Array to your view like so:

return array('shop' => $shop, 'logedUser' => $logedUser);

This will save those variables within your save.phtml in /yourmodule/view/yourcontroller/save.phtml

If you want to set the .phtml matching yourself you indeed could use the view model like you tried in your code the procedure is a litle diffrent then. It could look like this:

$viewModel = new ViewModel(array(
            'logedUser' => $logedUser,
            'shop' => $shop,
            ));
$viewModel->setTemplate('yourmodul/yourcontroller/save');
return $this->getContainerViewModel($viewModel);

Once you have that you can just access your variables in the save.phtml template file like so:

<?php echo $shop->getSomething(); ?>

You did not show us the Shop entity but I assume it has methods like getName() , __toString() etc. You can just call these Object methods within your view.

If you still have trouble understanding the mvc concept of zf2 you might want to re-read the zf2 documentation.

You have to show us what Catalog\\Model\\Shop and what query are you using for getting your data. Is there possibility that your query just getting null because it fails to get anything from database ? Do you have any kind of checks in your model to find out is your result from query good?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM