【问题标题】:Kohana 3.2 select issueKohana 3.2 select issue
【发布时间】:2013-07-13 16:06:14
【问题描述】:

我的数据库中有带有“服务器”名称的表。因此,当我想使用该代码添加一些数据时,就可以了:

    public function action_add()
    {
        $serverGameId = (int)Arr::get($_POST, 'serverGameId', '');
        $newServerName = Arr::get($_POST, 'newServerName', '');

        try
        {
           $server = new Model_Server();
           $server->Name = $newServerName;
           $server->GameId = $serverGameId;
           $server->save();
        }
        catch(ORM_Validation_Exception $e)
        {
           echo json_encode(array("success" => false, "errors" => $e->errors('validation')));
           return false;
        }

        $this->request->headers['Content-Type'] = 'application/json';
        echo json_encode(array("success" => true, "serverId" => $server->Id));
        return true;
    }

这是一个模型:

class Model_Server extends ORM {

protected $_table_name = 'servers';

public function rules()
{
    return array(
        'Name' => array(
            array('not_empty'),
        )
    );
}

}

但是当我尝试从表格中选择它时遇到问题:

    public function action_servers()
{
        $gameId = (int)Arr::get($_POST, 'gameId', '');

        if($gameId == -1) return false;

        try
        {
            $servers = ORM::factory('server')
                        ->where('GameId', '=', $gameId)
                        ->find_all();
        }
        catch(ORM_Validation_Exception $e)
        {
           echo json_encode(array("success" => false, "errors" => $e->errors('validation')));
           return false;
        }

        $this->request->headers['Content-Type'] = 'application/json';
        echo json_encode(array("success" => true, "servers" => $servers, "gameId" => $gameId));
        return true;
    }

我已经尝试通过在 try 块中更改代码来解决问题:

$servers = DB::select('servers')->where('GameId', '=', $gameId);

即使我尝试从 db 中获取所有服务器而没有 '->where' 它也不起作用。

有什么想法吗?

【问题讨论】:

    标签: mysql ajax kohana


    【解决方案1】:

    try 块中尝试print_r($servers);,看看你从模型中得到了什么。

    $servers 是一些有结果的类 - 使用 foreach 获取结果(一个接一个)

    $results = array();
    
    foreach($servers as $row) {
       //echo $row->Name;
       $results[] = $row->Name;
    }
    

    或者

    $results = array();
    
    foreach($servers as $row) {
       //echo $row->as_array();
       $results[] = $row->as_array();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多