Accessing block database columns from PHP script in "tools" directory

Permalink 1 user found helpful
Hi,
This is I guess is a fairly basic question but I have not found any documentation or examples showing how to solve it. I'm trying to develop a fairly basic new block type, and I'm having trouble accessing the block database columns (i.e. the block "configuration" info) from a PHP file in the tools directory. What is the recommended way of doing this?

Background:
I have in my db.xml defined a couple of database fields for my block. As described in the "creating a new block type" how-to "In our typical block controller, the value of the database columns are automatically extracted and placed in the local scope.". So from for example my view.php file in the root directory of my block I can easily access the database columns with something like "$column_name".

When I then wanna make my data requests asynchronous I have moved the database connectivity into a separate PHP file, which I put into my "tools" block directory (please correct me if this is not the recommended way of doing this). And now I can no longer access the database columns using a simple variable matching the column name.

What is the recommended way of accessing these columns from a PHP script in the blocks "tools" directory?

Thanks,
Alexander

 
jordanlev replied on at Permalink Reply
jordanlev
Load up the block controller and get them through there. This requires that your "tools" file receives the Block ID as one of its GET arguments (if you don't know how to pass the Block ID to the "tools" file, let me know and I can explain).

Here's what the tools file code might look like:
if (empty($_GET['bID']) || (intval($_GET['bID']) != $_GET['bID'])) {
    echo "ERROR: Invalid Block ID";
} else {
    $b = Block::GetById(intval($_GET['bID']));
    $bc = new YourCustomBlockController($b);
    $someValue = $bc->someValue;
    $anotherField = $bc->anotherField;
    //do something with your data...
}
exit;