如何在zend中从控制器访问模型?

时间:2014-04-14 10:53:33

标签: zend-framework model controller

这是控制器

class LocationController 
{
     .....
      public function testareAction(){ 
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $myarray =  $_POST['data_post'];
        $product_ids = $_POST['product_ids'];
        echo "test1";
        $model = new Application_Model_OrderNames(); 
        echo "test2";
        $model -> saveOrderNames();

    }
}

这是模型:

<?php
    class OrderNames {
        public function __construct(){

        }
        public function saveOrderNames(){
            return 1;
        } 
    }   
?> 

我做错了什么?为什么我无法在浏览器中访问“test2”字符串? THX

这是我的完整控制器类:

<?php

use Application\Model\Cronos,
    Application\Model\DbTable;

class LocationController extends \Lunchbox\Controller
{
    public function indexAction()
    {
        if (!($locationUrl = $this->_getParam('locationUrl'))) {
            throw new \Zend_Controller_Action_Exception('Location URL was not provided.', 404);
        }        

        $this->maiDivClass = "block clear step1";

        // get Location row
        $mapper = new DbTable\Location();
        $sql = $mapper->select()->where('url = ?', $locationUrl);

        if (!($this->view->location = $mapper->fetchRowActive($sql))) {
            throw new \Zend_Controller_Action_Exception('Location not found', 404);
        }

        // set active markup for location menu
        $this->_helper->layout()->locationMainMenuUrl = $this->view->location->url;

        // SEO
        $this->setSeo(
            $this->view->location->meta_title, 
            $this->view->location->meta_description, 
            $this->view->location->meta_keywords
        );

        // get day from param
        $dayName = $this->_getParam('day', Cronos::getCurrentDayName());

        $this->view->dayNo = Cronos::getWeekDayNumber($dayName);
        if ($this->view->dayNo === false) {
            throw new \Zend_Controller_Action_Exception('Invalid weekday provided.', 404);
        }

        $this->view->selectedDate = Cronos::getDateWithOffset($this->view->dayNo); // get date offset
        $this->view->categMapper  = new DbTable\Categories();

        $appCfg = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
        $this->view->s3 = "http://".$appCfg->custom->s3->bucket;
        $imgMapper = new Application_Model_Mapper_Images();
        $this->view->imgs = $imgMapper->fetchAllByLocationId($this->view->location->id);


        /**
         * Show products only for allowed admin emails. Testing only.
         * Admin must be logged in to see products, while the test is on.
         * 
         * To disable, change custom.test.isEnabled flag in app.ini.
         */
        $testCfg = Zend_Registry::get('Zend_Config')->custom->test->toArray();
        $isEnabled = (bool) $testCfg['isEnabled'];

        // test is enabled?
        if ($isEnabled) {
            $email = false; 
            $auth  = Zend_Auth::getInstance();
            // grab email, if logged in
            if ($auth->hasIdentity()) { $email = $auth->getIdentity()->email; }

            // has email? check it against config, else don't show products
            $showProducts = ($email) ? in_array($email, (array) $testCfg['email']) : false;

        // test is disabled: always show products
        } else {
            $showProducts = true;
        }

        if ($showProducts) {

            // get categories & products for this date and location
            $prodService = new \Application\Service\Products();
            $this->view->categsWithProds = $prodService->getProducts($this->view->location->id, $this->view->selectedDate); 

            //check days of the week with products
            $week = array();
            $currentDay = Cronos::getWeekDayNumberByDate();
            if($this->view->location->erp_name != "city lbox") {
                for($day=$currentDay;$day<7;$day++)
                {
                    $selectedDate = Cronos::getDateWithOffset($day);
                    $categswithprod = $prodService->getProducts($this->view->location->id, $selectedDate);
                    if(count($categswithprod))
                        $week[] = $day;

                }
                //we need to go to sunday and next week
                for($day=0;$day<$currentDay;$day++)
                {
                    $selectedDate = Cronos::getDateWithOffset($day);
                    $categswithprod = $prodService->getProducts($this->view->location->id, $selectedDate);
                    if(count($categswithprod))
                        $week[] = $day;
                } 
            } else {
                for($day = $currentDay; $day<7;$day++) {
                    $week[] = $day;
                }
                for($day = 0; $day<$currentDay; $day++) {
                    $week[] = $day;
                }
            }

            $this->view->week = $week;
            $specialMenuRows = $this->view->location->getSpecialMenus(new Zend_Date());
            $this->view->specialMenus = $this->view->partial('_partials/specialMenu.phtml', array('specialMenus' => $specialMenuRows));

            $this->view->categsWithProdsALaCarte =  $prodService->getProducts($this->view->location->id, $this->view->selectedDate, 1);

        // no xml; show "Download PDF"
        } else {
            $this->setPdfLink();
            $this->renderScript('location/no_xml.phtml');
        }
    }

    /**
     * Set pdfLink view var depending on current location .
     */
    protected function setPdfLink()
    {
        $this->view->pdfLink = false;
        if (strpos($this->view->location->url, 'city')) {
            $this->view->pdfLink = "http://meniu.lunchbox.ro/lunchbox.city.pdf";
        } elseif (strpos($this->view->location->url, 'polus')) {
            $this->view->pdfLink = "http://meniu.lunchbox.ro/lunchbox.polus.pdf";
        } elseif (strpos($this->view->location->url, 'sediu')) {
            $this->view->pdfLink = "http://meniu.lunchbox.ro/lunchbox.sediu.pdf";
        } elseif (strpos($this->view->location->url, 'iulius')) {
            $this->view->pdfLink = "http://meniu.lunchbox.ro/lunchbox.iulius.pdf";
        }        
    }
    public function testareAction(){ 
       // $this->getResponse()->setHeader('Content-Type', 'application/json');
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $myarray =  $_POST['data_post'];
        $product_ids = $_POST['product_ids'];
        $type = 1; 
        echo "tralalalal";
       // $model = new Application_Model_Mapper_Images();
        $model = new Application_Model_OrderNames();
        echo  $model -> saveOrderNames();

        echo "string";
        //$this->view->String = $model -> saveOrderNames();

    }
}

这是图像模型类:

<?php

class Application_Model_Mapper_Images extends Application_Model_Base_MapperAbstract
{
    protected $dbName = 'location_images';

    /**
     * @param int $locationId
     * @param array $imageNamesArr
     *      Array of basename files.
     * @return false|array
     *      Return boolean false on insert error.
     *      key: new image index, value: basename. I.e. array(1 => 'madonna.jpg')
     */
    public function batchInsertNewProductImages($locationId, array $imageNamesArr)
    {
        $newImgs = array();
        $this->getZDb()->getAdapter()->beginTransaction();

        try {
            foreach ($imageNamesArr as $baseImageName) {
                $newId = $this->getZDb()->insert(array('base_filename' => $baseImageName, 'location_id' => $locationId));
                $newImgs[$newId] = $baseImageName;
            }
            $this->getZDb()->getAdapter()->commit();
            return $newImgs;
        } catch (Zend_Exception $e) {
            $this->getZDb()->getAdapter()->rollBack();
            return false;
        }
    }

    /**
     * @param int $productId
     * @return array
     *      Container of Application_Model_Object_Image objects.
     */
    public function fetchAllByLocationId($locationId)
    {
        $locationId = (int) $locationId;
        $sql = $this->db->select()
                        ->from('location_images')
                        ->where('location_id = ?', $locationId)
                        ->order('disp_order')
                        ->order('base_filename'); // order alphabetically

        $out = array();
        foreach ($this->db->fetchAll($sql) as $imgRow) {

            $out[] = $imgRow;
        }
        return $out;
    }


    public function reOrderPhotos(array $photos)
    {
        $disp_order = 0;
        foreach ($photos as $photo_id) {
                $sql = $this->db->select()->from('location_images')->where('id = ?', $photo_id);
                //Verify if category exists in table
                if ($this->db->fetchRow($sql) != null) {
                ++$disp_order;
                    if ($photos != "") {
                    //Change menu order
                    $this->db->update('location_images',array('disp_order' => $disp_order), array('id = ?' => $photo_id));
                    }
                }
        }

    }

    public function test()
    {
        $sql = $this->db->insert("location_images", array('base_filename' => 'test.jpg', 'location_id'=>'1','disp_order'=>'22'));

    }

    /**
     * @param int $id
     * @return array
     */
    public function find($id) 
    {
        return $this->getZDb()->find($id)->current();

    }

    /**
     * @param string $imgBaseName
     * @return int
     */
    public function insertNew($imgBaseName)
    {
        $imgBaseName = trim($imgBaseName);

        // omit empty file names
        if (empty($imgBaseName)) { return 0; }

        try {
            return (int) $this->getZDb()->insert(array('base_filename' => (string) $imgBaseName));
        } catch (Exception $e) {
            return 0;
        }
    }
    public function saveOrderNames(){

            return 1;

   }
}

正如您所看到的,saveOrderNames函数同时位于OrderNames模型和Images模型中。任何人都可以解释我为什么如果我使用“图像”模型,我的字符串“字符串”来自控制器的testareAction函数它显示如果我使用OrderNames模型,它不是????

0 个答案:

没有答案