0

我对 OOPS 和 Zend 很陌生。我正在将 Web 应用程序代码的一部分转换为 Zend 框架,我不能在这里发布所有代码,因为它很长。我在下面的代码中创建了一个模型(我只能发布两个函数),我想从控制器访问函数内部的变量,然后在 HTML 表中打印出来(我想我将不得不使用“视图”那”,下面是我的模型:

  public function getVenueTypes()
 {
$poiTypes = array();
    if($this->_cache)
    {

        $key = $this->getCacheKey()."poiTypes_stats:{$this->language}";
    }
    if(!$poiTypes)
    {
        $sql = "SELECT poi_type_id, poi_type_name_".$this->language."
                AS
                poi_type_name
                FROM
                poi_types
                ORDER BY
                poi_type_name_".$this->language." ASC";
        $result = mysql_query($sql);
        if(!$result)
        {
            $message  = 'Invalid query: ' . mysql_error() . "\n";
            $message .= "Whole query: \n\n$sql\n";
            die($message);
        }
        while($row = mysql_fetch_array($result))
        {
            $poiTypes[] = array('id' => $row['poi_type_id'] ,'name' => $row['poi_type_name']);
        }
        print_r($poiTypes);
        if ($this->_cache)
        {
            $this->_cache->save($key, $poiTypes);               

        }    
    } 
foreach($poiTypes as $poiType)
    {
        $count_type = null;
        if($this->_cache)
        {
    $key = $this->getCacheKey()."poiTypeCount:{$poiType['id']}:{$this->metro_id}";
            $count_type = $this->_cache->load($key);
        }
        if(!$count_type)
        {
    $sql = "SELECT COUNT(*) FROM
                poi
                WHERE
                find_in_set('{$poiType['id']}', poi_type_id_array)
                AND poi_status = 1 AND poi_address_prefecture_id = '$this->metro_id'";
            $result = mysql_query($sql) or die(mysql_error());
        }  
        if(!$result)
            {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= "Whole query: \n\n$sql\n";
                die($message);
            }

        while($count = mysql_fetch_array($result))
        {
            $count_type[$poiType['id']][$this->metro_id] = $count['COUNT(*)'];
        }
        if($this->_cache)
        {
    $this->_cache->save($key, $count_type);
        }
    } 
 } 

任何帮助都非常感谢,我希望我足够清楚并提前感谢。

4

2 回答 2

0

I'm noy sure if this will help you, but you have several options to access those values.

Option 1: return an array with the variables you need

return array($myVar1, $myVar2, ..., $myVarN);

Option 2 (less modular): create the html string inside that function and then echo or return the string.

于 2012-10-17T04:42:42.733 回答
0

我假设你对 Zend 有基本的了解

Zend 框架的基本思想是控制器、模型和视图之间的链接

你的模型文件,BlaBla.php

Class Namespace_models_BlaBla extends Zend_Db_Table_Abstract{

   public function foo(){
       return 'hello world';
    }
}

你的控制器说 BarController 和一个动作说测试

class BarController extends extends Zend_Controller_Action{  

      public function testAction(){
           $class = new Namespace_models_BlaBla();
           $this->view->string = $class->foo(); 

         }

    }

你的 html 端 // test.phtml

<?php echo $this->string; ?> /**this will output hello world **/
于 2012-10-17T04:53:53.980 回答