joomla2.5 各种数据操作的写法
//数据查询 $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->select('contact.user_id, MAX(contact.id) AS id, contact.language'); $query->from('#__contact_details AS contact'); $query->where('contact.published = 1'); $query->group('contact.user_id, contact.language'); $query->order('contact.ordering'); $db->setQuery($query); $rows = $db->loadObjectList(); //SQL数据查询 $db = JFactory::getDbo(); $sql = 'SELECT * FROM #__users WHERE state=1'; $db->setQuery($sql); $rows = $db->loadObjectList(); foreach($rows as $row) { echo $row->name; echo $row->email; } //所有UserID的数组 $db = JFactory::getDbo(); $sql = 'SELECT id FROM #__users WHERE state=1'; $db->setQuery($sql); $userIds = $db->loadResultArray(); echo implode(', ', $userIds); //SQL更新记录 $db = JFactory::getDbo(); $title = '????'; $sql = 'UPDATE #__content SET title='.$db->Quote($title).' WHERE id=1'; $db->setQuery($sql); $db->query(); //user存取数据 $user = new JUser(); $user->load($this->userId); $user->name = 'newname'; $user->save(); //parameter存取数据 $table = JTable::getInstance( 'mytable'); $table->load($id); $params = new JParameter($table->params); $params->set($key,$value); $table->params = $params->toString(); $table->store(); //基于JTable存取数据,必须有JTable实例 $table = JTable::getInstance( 'mytable', 'XxxTable'); $table->load($id); $table->title = '???'; $table->store(); //如果table与model类在同一个组件里,可以用JModel::getTable $table = $this->getTable( 'mytable', 'XxxTable'); $table->load($id); //get item attribute $articleParams = new JRegistry; $articleParams->loadString($item->attribs); $item->alternative_readmore = $articleParams->get('alternative_readmore'); //set item attribute $table = JTable::getInstance( 'mytable'); $table->load($id); $articleParams = new JRegistry; $table->attribs = $articleParams->toString(); $table->store(); //基于JModel存取数据,必定存在JModel实例 $model = JModel::getInstance('Articles', 'ContentModel'); $model->setState('filter.state', 1); $model->setState('list.ordering', 'publish_up'); $items = $model->getItems();