This is a simple class, look at the insert statement.
// Author: Trabis
// URL: http://www.xuups.com
// E-Mail: lusopoemas@gmail.com
if (!defined('XOOPS_ROOT_PATH')) {
exit();
}
class DefacerMeta extends XoopsObject
{
/**
* constructor
*/
function DefacerMeta()
{
$this->XoopsObject();
$this->initVar("meta_id", XOBJ_DTYPE_INT, 0, true);
$this->initVar("meta_sitename", XOBJ_DTYPE_TXTBOX, null, false, 100);
$this->initVar("meta_pagetitle", XOBJ_DTYPE_TXTBOX, null, false, 100);
$this->initVar("meta_slogan", XOBJ_DTYPE_TXTBOX, null, false, 100);
$this->initVar("meta_keywords", XOBJ_DTYPE_TXTAREA, null, false);
$this->initVar("meta_description",XOBJ_DTYPE_TXTAREA, null, false);
}
}
class DefacerMetaHandler extends XoopsObjectHandler
{
function &create($isNew = true)
{
$meta = new DefacerMeta();
if ($isNew) {
$meta->setNew();
}
return $meta;
}
function &get($id)
{
$meta = false;
$id = intval($id);
if ($id > 0) {
$sql = 'SELECT * FROM '.$this->db->prefix('defacer_meta').' WHERE meta_id='.$id;
if (!$result = $this->db->query($sql)) {
return $meta;
}
$numrows = $this->db->getRowsNum($result);
if ($numrows == 1) {
$meta = new DefacerMeta();
$meta->assignVars($this->db->fetchArray($result));
}
}
return $meta;
}
function insert(&$meta)
{
if (strtolower(get_class($meta)) != 'defacermeta') {
return false;
}
if (!$meta->isDirty()) {
return true;
}
if (!$meta->cleanVars()) {
return false;
}
foreach ($meta->cleanVars as $k => $v) {
${$k} = $v;
}
if ($meta->isNew()) {
$sql = sprintf("INSERT INTO %s (meta_id, meta_sitename, meta_pagetitle, meta_slogan, meta_keywords, meta_description) VALUES (%u, %s, %s, %s, %s, %s)",
$this->db->prefix('defacer_meta'),
intval($meta_id),
$this->db->quoteString($meta_sitename),
$this->db->quoteString($meta_pagetitle),
$this->db->quoteString($meta_slogan),
$this->db->quoteString($meta_keywords),
$this->db->quoteString($meta_description));
} else {
$sql = sprintf("UPDATE %s SET meta_sitename = %s, meta_pagetitle = %s, meta_slogan = %s, meta_keywords = %s, meta_description = %s WHERE meta_id = %u",
$this->db->prefix('defacer_meta'),
$this->db->quoteString($meta_sitename),
$this->db->quoteString($meta_pagetitle),
$this->db->quoteString($meta_slogan),
$this->db->quoteString($meta_keywords),
$this->db->quoteString($meta_description),
intval($meta_id));
}
if (!$result = $this->db->queryF($sql)) {
return false;
}
if (empty($meta_id)) {
$meta_id = $this->db->getInsertId();
}
$meta->assignVar('meta_id', $meta_id);
return true;
}
function delete(&$meta)
{
if (strtolower(get_class($meta)) != 'defacermeta') {
return false;
}
$sql = sprintf("DELETE FROM %s WHERE meta_id = %u", $this->db->prefix('defacer_meta'), $meta->getVar('meta_id'));
if (!$result = $this->db->queryF($sql)) {
return false;
}
return true;
}
function getObjects($criteria = null, $id_as_key = false)
{
$ret = array();
$limit = $start = 0;
$sql = 'SELECT * FROM '.$this->db->prefix('defacer_meta');
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
$sql .= ' '.$criteria->renderWhere();
if ($criteria->getSort() != '') {
$sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
}
$limit = $criteria->getLimit();
$start = $criteria->getStart();
}
$result = $this->db->query($sql, $limit, $start);
if (!$result) {
return $ret;
}
while ($myrow = $this->db->fetchArray($result)) {
$meta = new DefacerMeta();
$meta->assignVars($myrow);
if (!$id_as_key) {
$ret[] =& $meta;
} else {
$ret[$myrow['meta_id']] =& $meta;
}
unset($meta);
}
return $ret;
}
function getCount($criteria = null)
{
$sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('defacer_meta');
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
$sql .= ' '.$criteria->renderWhere();
}
if (!$result =& $this->db->query($sql)) {
return 0;
}
list($count) = $this->db->fetchRow($result);
return $count;
}
function deleteAll($criteria = null)
{
$sql = 'DELETE FROM '.$this->db->prefix('defacer_meta');
if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
$sql .= ' '.$criteria->renderWhere();
}
if (!$result = $this->db->queryF($sql)) {
return false;
}
return true;
}
function getList($criteria = null)
{
$metas = $this->getObjects($criteria, true);
$ret = array();
foreach (array_keys($metas) as $i) {
$ret[$i] = $metas[$i]->getVar('meta_sitename');
}
return $ret;
}
function updateByField(&$meta, $field_name, $field_value)
{
$meta->unsetNew();
$meta->setVar($field_name, $field_value);
return $this->insert($meta);
}
}
?>
With XOOPS 2.3 you could have this methods and much more with less code! Here is an example of a XOOPS 2.3 class:
// Author: Trabis
// URL: http://www.xuups.com
// E-Mail: lusopoemas@gmail.com
if (!class_exists("XoopsPersistableObjectHandler")) {
include_once XOOPS_ROOT_PATH."/modules/myprojects/class/object.php";
}
class MyprojectsFavorite extends XoopsObject
{
/**
* constructor
*/
function MyprojectsFavorite()
{
$this->XoopsObject();
$this->initVar("fid", XOBJ_DTYPE_INT);
$this->initVar('projectid', XOBJ_DTYPE_INT, 0);
$this->initVar("uid", XOBJ_DTYPE_INT,0);
$this->initVar("date", XOBJ_DTYPE_INT,time());
}
}
class MyprojectsFavoriteHandler extends XoopsPersistableObjectHandler {
function MyprojectsFavoriteHandler($db) {
$this->XoopsPersistableObjectHandler($db, 'myprojects_favorite', 'MyprojectsFavorite', 'fid', 'projectid');
}
}
?>
To learn how to use XOOPS objects I recommend taking a look at smartfactory modules.