• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Reading from the database.

#1
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
 

Chris D

XenForo developer
Staff member
#2
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...

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.
 
#4
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...

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?
 

Jake Bunce

XenForo moderator
Staff member
#6
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.