视图 cdnauto/views/config/indexphp 复制代码 代码如下: echo CHtml::dropDownList(node CHtml::listData(Node::model()>findAll()namename)array(empty=>请选择节点 id => node ajax=>array( type=>POST url=>Yii::app()>createUrl(cdnauto/config/getNodeServersByNodeName) update=>#servers data=>array(node_name=>js:$("#node")val()) ) ) ); echo " "; echo CHtml::dropDownList(servers array(请选择服务器)); 控制器 cdnauto/controllers/ConfigControllerphp 复制代码 代码如下: public function actionGetNodeServersByNodeName(){ // if(!Yii::app()>request>isAjaxRequest) // throw new CHttpException(); $node_name = $_POST[node_name]; $nodeid = Node::model()>getNodeId($_POST[node_name]); //通过节点名称获取该节点ID $server = GossServer::model()>getServerByNodeid($nodeid); //通过节点ID获取服务器信息 //$server 为array类型形如 $server = array(array(name=>name)array(name=>name));所以需要两次foreach if(isset($server)){ foreach ($server as $k=>$v){ foreach($v as $kk => $vv){ echo CHtml::tag(option array(value=>$kk) CHtml::encode($vv) true); } } }else{ echo CHtml::tag(option array(value=>) servers true); } } 模型 GossServerphp 复制代码 代码如下: /** * 通过节点ID获取该节点下所有的服务器名称 * @author ysdaniel */ public static function getServerByNodeid($nodeid) { $sql = "SELECT name FROM OSS_Server WHERE nodeid = {$nodeid} "; ///$sql = "SELECT namenodeid FROM OSS_Server WHERE nodeid = {$nodeid} "; //both ok $cmd = Yii::app()>db>createCommand($sql); $ret = $cmd>queryAll(); if (!$ret){ throw new Exception("找不到这个节点对应的服务器"); } return $ret; } Nodephp 复制代码 代码如下: /** * 通过nodename获取nodeid名 * @author */ public static function getNodeId($name) { $sql = "SELECT id FROM OSS_Node WHERE name = {$name}"; $cmd = Yii::app()>db>createCommand($sql); $ret = $cmd>queryAll(); if (!$ret){ return null; //throw new Exception("找不到Node{$name}"); } return $ret[][id]; } 其它 数据表结构 效果 没有选择节点前 细节有空再补上了 |