How do you perform custom database queries?


I am able to select records from the database using get_records but if I need to perform a more complex query such as grouping or summing or such like how to I go about this?

I saw this post:

…and tried the following (just using a very basic query to test it):

$db = get_db();
$select = “SELECT id FROM omeka_items where id = 56”;
$test = $db->getTable(“Item”)>fetchObjects($select);

The result is an array of all kinds of information (including the database password) but not the actual data.
Is there another way I can write SQL queries and send them to the Omeka databse?


Typically I found the answer on my first search after posting this.
I used checked the zend documentation and saw that I should use fetchall instead:

$db = get_db();
$select = “SELECT * FROM omeka_items”;
$test = $db->getTable(“Item”)->fetchAll($select);

Hopefully this helps someone else.

By the way, for those who (previously like me) don’t know, the string “Item” represents the table ‘items’ to see all of the strings available to use here check the following directory in your Omeka installation: \application\models.
The file names here are the model names for the various Omeka database tables.

What you did previously was fine, you should have gotten back an array containing 1 Item object… it’s just that print_r is going to follow lots of connections and give you a bunch of junk.

The other fetch methods are fine too, of course, if you’re looking to deal with the data more directly.