How to connect to database?

I would like to create a section on my current exhibit page which displays all the items which has the same Dublin Core metadata, say "subject" or "creator" with my current core item. The most efficient way I can come up with is to get connected to the database and do a query search to retrieve the information and create the links. However, I came across a problem on getting connection to the database. I don't know where the database is and how can I achieve this. Anybody helps me? Thank you.

Actually, we will soon be releasing a plugin that will let you do this. We still need to do a little internal testing, but if you want to be brave you can download SearchByMetadata on GitHub

Hi Patrick,
Thank you for your reply. I will download it and have a try. But does this mean there is no other way to achieve this? Can I use some built-in functions to list core items which have same metadata as my current core item? Thank you.

That's pretty much what the plugin does, making use of the built-in advanced search mechanisms. Pretty sure that anything else would ultimately be duplicating code.

If there's another twist to your use case that doesn't work with the plugin, the code there is still probably a good starting point.

Hi Patrick,
I would like to use query search to retrieve metadata of an item, but I found it hard to get the table name and column names, etc. Could you help me out with this? Thank you.

I mean retrieve data from the database,, all I can find now is the get_db() function, however it is not explained in detail. Thank you.

If possible could you provide me an example of how to retrieve metadata from the database by sql? say, retrieve all the items with metadata "subject" equals to "greetings". Thank you.

Omeka keeps the metadata a little separate from the items, using Elements and ElementTexts ("subject" and "greetings" in your case). That can make directly writing SQL queries a little cumbersome, but Omeka provides ways to make the search easier. The best example is what you'll find in the search_by_metadata_find_items function in the SearchByMetadata plugin.

Hi Patrick,
Thank you very much for your reply. I am right now developing a project on omeka 1.5. I would like to know if SearchByMetadata plugin will be compatible with my current omeka version? Also, how long will the internal test last and have you set a date to officially issue it? Last, What issues will we face in its current state or concerns we should be aware of? Thank you again for your help.

It should be just fine for 1.5. Don't really anticipate any issues. Testing is practically done, but haven't set a date for officially releasing it.

Hi Patrick,
I copied the contents of function.php from searchByMetadata into custom.php on my server, which is as follows:
function search_by_metadata_find_items($elementsArray)
{
$itemTable = get_db()->getTable('Item');
$elementTable = get_db()->getTable('Element');
$params = array('advanced_search' => array());
foreach($elementsArray as $elementSet=>$elements) {
foreach($elements as $element=>$terms) {
$element = $elementTable->findByElementSetNameAndElementName($elementSet, $element);
foreach($terms as $term) {
$params['advanced_search'][] = array('terms' => $term,
'type' => 'is exactly',
'element_id' => $element->id );
}
}
}
return $itemTable->findBy($params);
}

and then I called the function in my layout.php file trying to list all the items that has the same metadata('Creator'='Eric') with my current item. The code is as follows:
$currentCreator = item('Dublin Core', 'Creator')
$testArray = array('Dublin Core' => array('Creator' => array($currentCreator)));
$testResult = search_by_metadata_find_items($testArray);
echo $testResult;

However, my question is the output is just "Array". Did I miss something obvious?

Thank you very much for reading this.

The way the function is set up, it will return an array of Items. So what you'd want is something like:

foreach($testResult as $item) {
echo link_to($item, 'show', metadata($item, array('Dublin Core', 'Title')); //or however you want to display each item
}

Hi Patrick,
On our page, where we are running the search_by_metadata_find_items(), we are searching on 3 different Dublin Core elements. Is there a way for us to know, for each returned item in that search, what the element's terms were that resulted in those found items?
Thank you,
Eric

My first guess would be to say that, since you know the terms you are searching for, to check each item's metadata to see which terms are there. I can imagine, though, how that would get messy and end up running a lot of redundant code if you have a lot of terms to search for each element.

Since what is returned is just the array of items, the only other thing I can think of is breaking up the queries to keep track of the info. That would likely produce duplicate hits, though, and again might produce its own messiness.

So, which of those approaches seems best might depend on how many terms for each of the 3 elements are likely.