示例#1
0
 /**
 * 
 $criteria =  new CDbCriteria();
 $criteria->condition = "id = 1 or id =1";
 $node = Node::find('post',$criteria); 
 Node::find('post',1); 
 */
 static function find($table, $criteria, $pager = false, $pageSize = 10)
 {
     if (!is_object($criteria)) {
         $id = $criteria;
     }
     //所以字段key以及对应的model
     $cache = cache('node__field_table');
     $now = $cache[$table];
     $realTable = "field_" . $table;
     //设置NodeModel的表名
     NodeModel::$_table = $realTable;
     $t = new NodeModel();
     $t->refreshMetaData();
     if (is_object($criteria)) {
         if (true === $pager) {
             $count = NodeModel::model()->count($criteria);
             $pages = new CPagination($count);
             $pages->pageSize = $pageSize;
             $pages->applyLimit($criteria);
         }
         $posts = NodeModel::model()->findAll($criteria);
         if (!$posts) {
             return null;
         }
         foreach ($posts as $r) {
             $allRows[] = static::find($table, $r->id);
         }
         if (true === $pager) {
             $out = array('posts' => $allRows, 'pages' => $pages);
             return $out;
         }
         if ($criteria->limit == 1) {
             return ArrHelper::first($allRows);
         }
         return $allRows;
     }
     $fields = cache('node__content_field');
     if (!fields) {
         return null;
     }
     $rows = NodeModel::model()->findByPk($id);
     foreach ($fields[$table] as $k => $v) {
         $row[$k] = $rows->{$k};
     }
     //没有缓存是有问题的
     if (!$cache) {
         return $row;
     }
     foreach ($now as $k => $v) {
         $name = trim($v->name);
         $_relation_table = $realTable . "_" . $name;
         //真实的第三方表的内容
         $deep = $v->_relation_table;
         //是多个值的,并且是关联其他表的情况
         if ($v->relation && $v->mvalue == 1) {
             $allR = CDB()->from($_relation_table)->where('nid=:nid', array(':nid' => $id))->queryAll();
             if ($allR) {
                 foreach ($allR as $key => $al) {
                     $value = $al['value'];
                     $values = CDB()->from($deep)->where('id=:id', array(':id' => $value))->queryRow();
                     $all[$name][$value] = $values;
                 }
                 foreach ($all as $key => $value) {
                     $row[$key] = $value;
                 }
             }
         } else {
             if ($v->relation) {
                 $value = CDB()->from($deep)->where('id=:id', array(':id' => $row[$name]))->queryRow();
                 $row[$name] = $value;
             }
         }
     }
     return $row;
 }