Talk

Difference between revisions of "Accessing the database using JDatabase"

From Joomla! Documentation

(→‎Subqueries: new section)
m (moved Talk:How to use the database classes in your script to Talk:Accessing the database using JDatabase: Adapted page title to the continuous tense used in most tutorial titles)
(No difference)

Revision as of 15:16, 7 February 2012

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)



I think, you can not repeat the call to loadAssoc to get further rows, because loadAssoc runs the query unconditionally, I got a infinite recursion using it this way:

    while($row = $db->loadAssoc()){
    // do something...
    }

See: [1] line: 392

Merging the 2 suggested articles[edit]

Discuss here (E-builds 17:38, 11 January 2010 (UTC))

While they have the same name one is 'supposed' to be a reference and the other a 'how to' tutorial. Given that I think that the two should be complementary and linked but not merged. GreyHead

I went ahead with the merge while emptying the Tutorial namespace. This article's contents were a superset of the tutorial's, and the style was exactly the same. The page API16:JDatabase can serve as a reference. Mvangeest 14:07, 15 January 2011 (UTC)

Subqueries[edit]

Ancient versions of MySQL are rare and becoming rarer. Please don't encourage people to break subqueries into two separate queries - it adds load to database servers! Signed, every DB admin on the planet. Rosuav 23:22, 6 February 2011 (UTC)