Php 从Zend framework中的控制器访问模型中声明的函数中的变量
我对OOPS和Zend是个新手。我正在将一部分web应用程序代码转换为Zend framework,我无法在这里发布所有代码,因为它相当长。我在下面的代码中创建了一个模型(我只能发布两个函数),我想从控制器访问函数中的变量,然后在HTML表中打印出来(我想我必须使用“视图”,下面是我的模型:Php 从Zend framework中的控制器访问模型中声明的函数中的变量,php,oop,zend-framework,methods,Php,Oop,Zend Framework,Methods,我对OOPS和Zend是个新手。我正在将一部分web应用程序代码转换为Zend framework,我无法在这里发布所有代码,因为它相当长。我在下面的代码中创建了一个模型(我只能发布两个函数),我想从控制器访问函数中的变量,然后在HTML表中打印出来(我想我必须使用“视图”,下面是我的模型: public function getVenueTypes() { $poiTypes = array(); if($this->_cache) { $key
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);
}
}
}
非常感谢您提供的任何帮助,我希望我说得足够清楚,并提前表示感谢。我不确定这是否会对您有所帮助,但您有几个选项可以访问这些值 选项1:返回包含所需变量的数组
return array($myVar1, $myVar2, ..., $myVarN);
选项2(模块化程度较低):在该函数中创建html字符串,然后回显或返回该字符串。我假设您对Zend有基本的了解 Zend框架的基本思想是连接控制器、模型和视图 您的模型文件BlaBla.php
Class Namespace_models_BlaBla extends Zend_Db_Table_Abstract{
public function foo(){
return 'hello world';
}
}
你的控制器说BarController,动作说test
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 **/
/**这将输出hello world**/
是的,我有基本的理解,所以可能是我不够清楚,我只发布了一个函数,没有在model和controller中声明类,以使其简短。我如何访问变量,例如$count_type[$poiType['id'][$this->metro_id](来自上面的代码)来自控制器?如果我的问题不成熟,我很抱歉,我正在尝试学习OOPS和Zend。谢谢你的回答,希望听到更多。