Home » Php » php – Zend Framework, run query without a view?

php – Zend Framework, run query without a view?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am currently building a small admin section for a website using Zend Framework, this is only my second time of using the framework so I am a little unsure on something things. for example are I have an archive option for news articles where the user will hopefully click a link and the article will be archived however I cannot work out how to get this to run without having a view?

this is my controller

public function archiveNewsAction()
{

    //die(var_dump($this->_request->getParam('news_id')));
    $oNews = new news();
    $this->_request->getParam('news_id');
    $oNews->archiveNewsArticle($news_id);
    //die(var_dump($oNews));
    $this->_redirect('/admin/list-all');
}

and this is my model

public function archiveNewsArticle($news_id)
{
    //die($news_id);
    $db = Zend_Registry::get('db');
    $sql = "UPDATE $this->_name SET live = '0' WHERE news_id = '$news_id' LIMIT 1";
    die($sql);
    $query = $db->query($sql);
    $row = $query->fetch();

    return $row;
}

I would appreciate any help any one can give.

Thanks

Sico

How to&Answers:

I use this with calls to AJAX-only actions that I either don’t want output or I’m using some other output, like XML or JSON:

// Disable the main layout renderer
$this->_helper->layout->disableLayout();
// Do not even attempt to render a view
$this->_helper->viewRenderer->setNoRender(true);

This has the added benefit of no overhead of redirection if what you are doing has no output/non-HTML output.

Answer:

To disable view rendering in an action (put this in the specific action. If you want it for the entire controller put it in the init method):

$this->_helper->viewRenderer->setNoRender();

If you are using the layout component of ZF also add this:

$this->_helper->layout->disableLayout();

Answer:

I could not figure out your code there. in your model you are calling die(). why?
it will stop the execution. are you sure about that line? anyway, if you have a controller in Zend Framework and do not need any view, you can turn the view off by this line:

// code in your controller
$this->_helper->viewRenderer->setNoRender(true);
// the rest of the controller

now the controller will not search for a view script to show to the user.
make sure you will call

$this->_redirect() 

after all of your controller job is done.

Answer:

Orignal Answer:

Your call to:

$this->_redirect();

Calls the Redirector action helper, which (unless you’ve configured it not to) will automatically exit the script as soon as the headers are written, so the view will never be called or rendered, there’s no need for a view script.


Follow-up Answer:

In order to call the action without sending the user to the other “page” and then redirecting back again you’ll need to use an XMLHttpRequest (AJAX) call. These links should provide the information you need:

http://developer.mozilla.org/en/AJAX
http://www.ibm.com/developerworks/xml/library/wa-ajaxintro1.html
http://www.oracle.com/technology/pub/articles/schalk-ajax.html

Also take a look at some JS frameworks that make using XMLHttpRequest cross-browser much easier:

http://www.prototypejs.org/
http://mootools.net/

Zend Framework actually has built in support for the Dojo JS framework, which you may find easier:

http://framework.zend.com/manual/en/zend.dojo.html
http://www.dojotoolkit.org/