/**
 * Method to cache the last query constructed.
 *
 * This method ensures that the query is constructed only once for a given state of the model.
 *
 * @return  DatabaseQuery  A DatabaseQuery object
 *
 * @since   1.6
 */
protected function _getListQuery()
{
    // Compute the current store id.
    $currentStoreId = $this->getStoreId();
    // If the last store id is different from the current, refresh the query.
    if ($this->lastQueryStoreId !== $currentStoreId || empty($this->query)) {
        $this->lastQueryStoreId = $currentStoreId;
        $this->query = $this->getListQuery();
    }
    return $this->query;
}