kohana框架中in_array函数中的数据类型错误?

时间:2012-09-02 14:04:36

标签: php kohana

我有两个控制器,一个是基本控制器,另一个是基本控制器中的subject我从Model中定义了一些数组,看看:

class Controller_Base extends Controller_Template {

    public $template = 'main';

    public function before()
    {
        parent::before();

        $webs = array();
        $apps = array();

        $app = new Model_Application();
        $apps = $app->get_all();

        $web = new Model_Web();
        $webs = $web->get_all();

        $this->template->content = '';
        $this->template->styles = array('style');
        $this->template->scripts = '';

        $this->template->webs = $webs;
        $this->template->apps = $apps;

    }

}

在控制器主题中我正在使用函数in_array

class Controller_Subject extends Controller_Base {

public function action_all()
{

    $url = $this->request->param('url');

    $this->template->caption = $url;


    if (in_array($url,$this->template->webs)) { 
        echo "web";
    }
        elseif (in_array($url,$this->template->apps)) { 
        echo "apps";
    }

    $links = array("a"=>"1","b"=>"2");

    $view = View::factory('subject')
                ->set('links',$links);

    $this->template->content = $view;

}

}

但Kohana给我一个错误:

ErrorException [ Warning ]: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument

怎么了?

2 个答案:

答案 0 :(得分:2)

您需要数组变量而不是Database_Result对象:

...
$apps = $app->get_all()->as_array();
...
$webs = $web->get_all()->as_array();
...

答案 1 :(得分:1)

模型返回的数据,即Database_MySQL_Result,需要先更改为数组。 你可以用

foreach($query as $v) 
    $arr[]=$v; 
return $arr;

get_all()