Jump to content

How do I load and return a database record object?


Aetherdan

Recommended Posts

Hi,

I am incredibly novice when it comes to PHP and usually I am able to solve most issues by research/trial & error, but this has currently stumped me:

How do I load this database record array as a content object?

$record = \IPS\Db::i()->select('*', 'cms_custom_database_2', array('record_topicid=?', $content->tid))->first();

 

Link to comment
Share on other sites

try
{
	$record = \IPS\cms\RecordsX::constructFromData( \IPS\Db::i()->select( '*', 'cms_custom_database_X', array( 'record_topicid=?', Y ) )->first() );
}
catch( \UnderflowException $e )
{
	// ... Record doesn't exist
{

Replace X with the ID of the database (note that there are two instances), and then Y with the ID of the topic associated with the record (so 2, and then $content->tid, in your example).

If it doesn't exist, then this will throw an UnderflowException (per \IPS\Db\Select::first() - /system/Db/Select.php), and if it does exist, then $record will contain an \IPS\cms\Records object for that particular record (\IPS\cms\Records - /applications/cms/Records/Records.php).

Link to comment
Share on other sites

Note that record_topicid is listed as a valid id column in the Records class, so you can just do this instead:

try
{
	$record = \IPS\cms\RecordsX::load( Y, 'record_topicid' );
}
catch( \OutOfRangeException $e )
{
	// ... Record doesn't exist
{

Note that when you attempt to load something and it doesn't exist an OutOfRangeException is thrown instead, but otherwise the code is fairly similar here.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...