[Joomla3] [database] [teaching] JOOMLA3 the Dbo object

Date:2016/10/16

 

everyone!
Because the use of joomla3 development system, this side in particular about   joomla3 DBO objects, DBO object is the library object , In general, basically all frameworks will have their own DBO , so joomla is no exception, there is   JDatabase this object.
that's where I introduce his simple usage
 
 // Get DBO object
$Db=JFactory :: getDBO ();

// ############################# query a piece of information
$Query=$db- > getQuery (true); // get the query object
$Query- > select ('t. *, Q. *') // field
    - > from ('#__ test_table AS t') // from
    - > join ('LEFT', '# __ test_join AS q ON t.id=q.id') // join mode
    - > where ('t.id > 10') // condition
    - > order ('t.id ASC'); // sort
$Db- > setQuery ($query, 0,10); // set the query string with start and limit
$Rows=$db- > loadObjectList (); // get the return data
$Row=$db- > loadObject (); // get the return (one)

// ############################# Using subquery
$QueryL=$db- > getQuery (true); // Get the subquery query object
$QueryL- > select ('p.id')
    - > from ('#__ test_little AS p')
    - > where ('p.id > 99');

$QueryN=$db- > getQuery (true); // Get the query object
$QueryN- > select ('p. *') // field
    - > from ('#__ test_table2 AS p') // from
    - "subquery, direct call subquery object will become the output string ().
$Db- > setQuery ($queryN, 0,10); // set the query string with start and limit
$RowNs=$db- > loadObjectList (); // get the return data

// ############################ Add a new item
$Data=array (
    'Id' => '1',
    'Name' => 'zhang xiaoming',
    );
$Data=(object) $data; // insertObject can only use object write
$Db- > insertObject ('#__ test_table', $data);

// ############################# modify the updated data (simple)
$Data=array (
    'Id' => '1',
    'Name' => 'zhang xiaoming renamed',
    );
$Data=(object) $data; // insertObject can only use object write
$Db- > updateObject ('#__ test_table', $data, 'id'); // table name, data, update based on the key (can only set one)

// ############################# modify the updated data
$QueryU=$db- > getQuery (true); // get the query object
$QueryU- > update ("#__ test_table")
    - > set ('name ='. $Db- > quote ("zhang xiaoming renamed"))// set value, quote for the escape symbol function
    - > where ('id=1'); // condition
$Db- > setQuery ($query); // set query
$Db- > execute (); // execute query

// delete a piece of information
$Query- > delete ('#__ test_table')
    - > where ('id=1');
$Db- > setQuery ($query);
$Db- > execute ();

// use transaction (cancel with an error)
$Db-> transactionStart ();
Try
{
    $Query=$db- > getQuery (true);
    $Data=array (
        'Id' => '1',
        'Name' => 'Zhang Xiaoming renamed 2',
        );
    $Data=(object) $data;
    $Db- > updateObject ('#__ test_table', $data, 'id');
    $Db-> transactionCommit ();
}
Catch (Exception $e)
{
    $Db- > transactionRollback ();
    JError :: raiseError (500, $e);
} 


 
basically will be commonly used by these
#__ is the table prefix
$db- > quote is used in the query jump out of the character letter (to avoid SQL injection )
 
Questions can be asked.
Thanks!

 

Tag Articles

[joomla] how to modify the website template offline

[PHP] easy to use picture watermark function of teaching

Message Board

Feel free to leave suggestions and share! Thanksgiving!

comments powered by Disqus