Reading from the database.

Renari

Member
I can't seem to find documentation anywhere on reading from the database.

PHP:
            $db = XenForo_Application::get('db');
            $result = $db->query("SELECT `user_id` FROM `xf_admin`");
            while ($result) {
                $admin .= $result['user_id'];
            }

Returns:
Fatal error: Cannot use object of type Zend_Db_Statement_Mysqli as array
 
Typically you would achieve this using the XenForo model.

You would create a Code Event Listener that listens to the load_class_model event.

Then you would create a new Model class that extends the main XenForo_Model class...

Rich (BB code):
<?php
class YourAddOnID_Model_YourAddOn extends XenForo_Model
{
public function getAdminUserId($userId)​
{​
return $this->_getDb()->fetchRow('​
SELECT *​
FROM xf_admin​
WHERE user_id = ?​
', $userId);​
}​
}

Now all you need to do is call that from your Listener or Controller:

Code:
$admin = XenForo_Model::create('YourAddOnID_Model_YourAddOn')->getAdminUserId($userId);

There is a way to do it like you said, but the code I've entered above is more typical of how it's done in XenForo which tends to adhere to MVC principles.
 
Typically you would achieve this using the XenForo model.

You would create a Code Event Listener that listens to the load_class_model event.

Then you would create a new Model class that extends the main XenForo_Model class...

Rich (BB code):
<?php
class YourAddOnID_Model_YourAddOn extends XenForo_Model
{
public function getAdminUserId($userId)​
{​
return $this->_getDb()->fetchRow('​
SELECT *​
FROM xf_admin​
WHERE user_id = ?​
', $userId);​
}​
}

Now all you need to do is call that from your Listener or Controller:

Code:
$admin = XenForo_Model::create('YourAddOnID_Model_YourAddOn')->getAdminUserId($userId);

There is a way to do it like you said, but the code I've entered above is more typical of how it's done in XenForo which tends to adhere to MVC principles.
Is there any specific reason it's done that way?
 
A model is basically an interface for a certain type of data. There is a user model, a thread model, etc. Models are good if many different areas will need to query the same information. You can create a function in the model and have everyone call that function.

Model or not, I suggest using one of the fetch functions when running your query. Instead of this:

Code:
$db->query("SELECT `user_id` FROM `xf_admin`");

Use this:

Code:
$db->fetchCol("SELECT `user_id` FROM `xf_admin`");

There are different fetch functions:

fetchAll = returns an array of rows
fetchRow = returns one row (which is an array)
fetchCol = returns an array of individual column values for multiple rows
fetchOne = fetches just one column for one row (not an array)

Which fetch function you use depends on the results of your query. But the fetch functions are nice because they allow you to deal in arrays and primitive types instead of mysql resources.
 
Thanks guys, I don't think the modal will be necessary since this query will only ever be run in this location.
 
Top Bottom