Accessing the database using JDatabase: Difference between revisions
From Joomla! Documentation
New page: Can anyone provide more info regarding insertObject()? I just have no idea of how to insert a row into a table. I tried to do the following but it just doesn't work: $db = & JFactory::get... |
Bembelimen (talk | contribs) 3 ways of saving values into the database |
||
| Line 11: | Line 11: | ||
Thanks! | Thanks! | ||
<source lang="php"> | |||
/** | |||
* | |||
* $object->fieldname1 = 'value1'; | |||
* $object->fieldname2 = 'value2'; | |||
* $object->fieldname3 = 'value3'; | |||
* $object->fieldname4 = 'value4'; | |||
* $object->fieldname5 = 'value5'; | |||
* | |||
* $table = '#__tablename'; | |||
* | |||
*/ | |||
$db = JFactory::getDBO(); | |||
$db->insertObject($table, $object, 'id'); | |||
/** | |||
* | |||
* The query will be generated: | |||
* | |||
* INSERT INTO `#__tablename` ( 'fieldname1', 'fieldname2', 'fieldname3', 'fieldname4', 'fieldname5' ) VALUES ( 'value1', 'value2', 'value3', 'value4', 'value5' ); | |||
* | |||
* and executed | |||
* | |||
*/ | |||
echo $object->id; // will output the last insert ID | |||
</source> | |||
If you wanna use "your" syntax: | |||
<source lang="php"> | |||
$db = JFactory::getDOB(); | |||
$query = "INSERT INTO `#__tablename` ( 'field1', 'field2' ) VALUES ( '".$value1."', '".$value2."')"; | |||
/** | |||
* | |||
* $db->setQuery($query); | |||
* | |||
* if ($dump) { | |||
* echo $db->getQuery(); | |||
* } | |||
* | |||
* $db->Query(); | |||
* | |||
*/ | |||
$db->Execute($query); | |||
</source> | |||
But it's recommend to use a controller/model/table construct: | |||
<source lang="php"> | |||
// controller | |||
$model = $this->getModel('modelname') | |||
$model->store($data); | |||
// Model | |||
public function store($data) { | |||
[...] | |||
$row = JTable::getInstance('KomponentNameTableName'); | |||
//save it in the db | |||
if (!$row->save($data)) { | |||
JError::raiseError( 500, $row->_db->getError() ); | |||
return false; | |||
} | |||
[...] | |||
} | |||
// table/komponentnametablename.php | |||
class JTableKomponentNameTableName extends JTable { | |||
/** | |||
* Primary Key | |||
* @var int | |||
*/ | |||
var $id = null; | |||
/** @var int */ | |||
var $field1 = null; | |||
/** @var char */ | |||
var $field2 = null; | |||
public function __construct(& $db) { | |||
parent::__construct('#__tablename', 'id', $db); | |||
} | |||
public function check () { | |||
// Check all fields $this->fieldname | |||
return true; | |||
} | |||
} | |||
</source> | |||
-- [[User:Bembelimen|Bembelimen]] 12:34, 16 March 2009 (UTC) | |||
Revision as of 12:34, 16 March 2009
Can anyone provide more info regarding insertObject()? I just have no idea of how to insert a row into a table. I tried to do the following but it just doesn't work:
$db = & JFactory::getDBO(); $query = "insert into #__my_table(field1, field2) values ('".$value1."','".$value2."')"; echo $query; $db->setQuery( $query ); return($db->Query());
Any ideas?
Thanks!
/**
*
* $object->fieldname1 = 'value1';
* $object->fieldname2 = 'value2';
* $object->fieldname3 = 'value3';
* $object->fieldname4 = 'value4';
* $object->fieldname5 = 'value5';
*
* $table = '#__tablename';
*
*/
$db = JFactory::getDBO();
$db->insertObject($table, $object, 'id');
/**
*
* The query will be generated:
*
* INSERT INTO `#__tablename` ( 'fieldname1', 'fieldname2', 'fieldname3', 'fieldname4', 'fieldname5' ) VALUES ( 'value1', 'value2', 'value3', 'value4', 'value5' );
*
* and executed
*
*/
echo $object->id; // will output the last insert ID
If you wanna use "your" syntax:
$db = JFactory::getDOB();
$query = "INSERT INTO `#__tablename` ( 'field1', 'field2' ) VALUES ( '".$value1."', '".$value2."')";
/**
*
* $db->setQuery($query);
*
* if ($dump) {
* echo $db->getQuery();
* }
*
* $db->Query();
*
*/
$db->Execute($query);
But it's recommend to use a controller/model/table construct:
// controller
$model = $this->getModel('modelname')
$model->store($data);
// Model
public function store($data) {
[...]
$row = JTable::getInstance('KomponentNameTableName');
//save it in the db
if (!$row->save($data)) {
JError::raiseError( 500, $row->_db->getError() );
return false;
}
[...]
}
// table/komponentnametablename.php
class JTableKomponentNameTableName extends JTable {
/**
* Primary Key
* @var int
*/
var $id = null;
/** @var int */
var $field1 = null;
/** @var char */
var $field2 = null;
public function __construct(& $db) {
parent::__construct('#__tablename', 'id', $db);
}
public function check () {
// Check all fields $this->fieldname
return true;
}
}
-- Bembelimen 12:34, 16 March 2009 (UTC)