Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6949085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:54:30+00:00 2026-05-27T13:54:30+00:00

Is there a way to use a regular mysql select statement with Zend Framework

  • 0

Is there a way to use a regular mysql select statement with Zend Framework without having to use Zend_Db::factory

Code below is in my Model classes which extends Zend_Db_Table_Abstract

I can currently do this (which works):

    $db = Zend_Db::factory('Pdo_Mysql', array(
                'host' => 'localhost',
                'username' => 'dbuser',
                'password' => 'dbuserpass',
                'dbname' => "somedbname"
            ));

    $select = "SELECT iv.* 
                FROM image_variations AS iv
                LEFT JOIN images AS i ON (i.image_id = iv.image_id)
                LEFT JOIN product_images AS pi ON (pi.image_id = iv.image_id)
                WHERE pi.pid = '$pid'
                && iv.image_type_id = '$image_type_id' ";

    $stmt = $db->query($select);
    $rows = $stmt->fetchAll();
    return $rows;

I would like to do this:

    $select = "SELECT iv.* 
                FROM image_variations AS iv
                LEFT JOIN images AS i ON (i.image_id = iv.image_id)
                LEFT JOIN product_images AS pi ON (pi.image_id = iv.image_id)
                WHERE pi.pid = '$pid'
                && iv.image_type_id = '$image_type_id' ";

    $stmt = $this->query($select);
    $rows = $stmt->fetchAll();
    return $rows;

So I dont always have to define db variables in each method and have mysql passwords all over the place. Is there a way to use $this instead of having to instantiate a new $db Zend_DB Factory??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:54:31+00:00Added an answer on May 27, 2026 at 1:54 pm

    Ok figured it out. The solution is not very elegant buts works and is portable.

    My setup consists of multiple DBs, so in Bootstrap.php I have:

    protected function _initDatabase() {
        $resource = $this->getPluginResource('multidb');
        $resource->init();
    
        Zend_Registry::set('db', $resource->getDb('db'));
        Zend_Registry::set('shopdb', $resource->getDb('shopdb'));
    }
    

    In Application.ini

    resources.multidb.db.adapter = PDO_MYSQL
    resources.multidb.db.host = 127.0.0.1
    resources.multidb.db.username = user
    resources.multidb.db.password = userpassword
    resources.multidb.db.dbname = dbone
    resources.multidb.db.isDefaultTableAdapter = true
    resources.multidb.db.default = true
    
    
    resources.multidb.shopdb.adapter = PDO_MYSQL
    resources.multidb.shopdb.host = 127.0.0.1
    resources.multidb.shopdb.username = user
    resources.multidb.shopdb.password = userpassword
    resources.multidb.shopdb.dbname = dbtwo
    resources.multidb.shopdb.isDefaultTableAdapter = false
    

    in index.php

    $config = new Zend_Config_Ini(APPLICATION_PATH . "/configs/application.ini");
    Zend_Registry::set('settings', $config);
    

    Then (the not very elegant bit) in Models add an init() method

    protected $db;
    
    public function init() {
        $settings = Zend_Registry::get('settings');
        $adapter = $settings->production->resources->multidb->shopdb->adapter;
        $params = array(
            'host' => $settings->production->resources->multidb->shopdb->host,
            'username' => $settings->production->resources->multidb->shopdb->username,
            'password' => $settings->production->resources->multidb->shopdb->password,
            'dbname' => $settings->production->resources->multidb->shopdb->dbname,
        );
        $this->db = Zend_Db::factory("$adapter", $params);
    }
    

    And then you can use $this->db in other methods in the Model class, eg:

    public function getProductImages($pid, $image_type_id) {
    
        $select = "SELECT iv.* 
                    FROM image_variations AS iv
                    LEFT JOIN images AS i ON (i.image_id = iv.image_id)
                    LEFT JOIN product_images AS pi ON (pi.image_id = iv.image_id)
                    WHERE pi.pid = '$pid'
                    && iv.image_type_id = '$image_type_id' ";
    
        $stmt = $this->db->query($select);
        $rows = $stmt->fetchAll();
        return $rows;
    }
    

    It works but it still seems like there should be an easier way…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to use Enum values inside a JSP without using scriptlets.
Can I use a Regular Expression with Hashtable.containsKey(Object value); Is there any way to
I was wondering if there was a way to use Regular expressions to remove
Good morning guys Is there a good way to use regular expression in C#
Good morning is there a clever way to use a javascript regular expression to
Is there any way to use inheritance in database (Specifically in SQL Server 2005)?
Is there a way to use JQuery to cloak or encrypt email addresses on
Is there any way to use a constant as a hash key? For example:
Is there a way to use constants in JavaScript ? If not, what's the
Is there a way to use form fields that does not correspond to database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.