Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
Aetherdan Posted November 26, 2017 Posted November 26, 2017 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();
Ryan Ashbrook Posted November 26, 2017 Posted November 26, 2017 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).
bfarber Posted November 27, 2017 Posted November 27, 2017 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.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.