【问题标题】:Accessing a variable in the function declared in model from the controller in Zend framework从 Zend 框架中的控制器访问模型中声明的函数中的变量
【发布时间】:2012-10-07 06:07:33
【问题描述】:

我对 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);
        }
    } 
 } 

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

【问题讨论】:

    标签: php oop zend-framework methods


    【解决方案1】:

    我不确定这是否会对您有所帮助,但您可以通过多种方式访问​​这些值。

    选项1:返回一个包含你需要的变量的数组

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

    选项 2(较少模块化):在该函数中创建 html 字符串,然后回显或返回该字符串。

    【讨论】:

      【解决方案2】:

      我假设你对 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 **/
      

      【讨论】:

      • 是的,我有基本的了解,所以可能是我不够清楚,我只发布了一个没有在模型和控制器中声明类的函数以使其简短。如何从控制器访问变量,例如 $count_type[$poiType['id']][$this->metro_id](来自上面的代码)?如果我的问题不成熟,我很抱歉,我正在尝试学习 OOPS 和 Zend。感谢您的回复,并希望听到更多。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多