2012-12-17 10 views
6

देता है मैंने उस प्लगइन में WP में कस्टम प्लगइन (मेरे द्वारा बनाया गया) जोड़ा है, मेरे पास बेसमोडेल नामक कक्षा है, जो wpdb को बढ़ाती है।अन्य वर्ग में wpdb बढ़ाएं - चयन के लिए get_results का उपयोग करते समय मुझे शून्य

यहां समस्या तब होती है जब मैं क्वेरी चलाने की कोशिश करता हूं तो मुझे परिणामस्वरूप झूठी या शून्य या खाली सरणी मिलती है।

class BaseModel extends wpdb{ 

public function __construct(){ 
    parent::__construct(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
} 

function get_destinations($limit, $order){ 
    $query = "SELECT * FROM wp_relations"; 

    $result = $this->get_results($query, ARRAY_A); 
    var_dump($result); 
} 

function get_total_destinations(){ 
}} 

क्या कोई मुझे बता सकता है कि क्या गलत है?

धन्यवाद।

+0

क्या आप सुनिश्चित हैं कि डीबी प्रमाण-पत्र स्थिरांक परिभाषित किए गए हैं? –

उत्तर

5

वास्तव में यह एक पूर्ण ओओपी समाधान नहीं है लेकिन मैं अपने कार्यों में वैश्विक $ wpdb जोड़कर इसे हल करता हूं।

class BaseModel { 


function get_destinations($limit, $order){ 
    global $wpdb; 
    $query = "SELECT * FROM wp_relations"; 

    $result = $wpdb->get_results($query, ARRAY_A); 
    var_dump($result); 
} 

function get_total_destinations(){ 
}} 

मुझे आशा है कि आपको यह सहायक मिलेगा।

2

More Info WordPress tests with wpdb

<?php 
class testWPDB extends wpdb { 
function prepare($query, $arguments){ 
     return vsprintf($query, $arguments); 
    } 
} 

class UTCW_Test_Data extends WP_UnitTestCase { 
protected $utcw; 
function setUp(){ 
    $this->utcw = UTCW_Plugin::get_instance(); 
} 

function getWPDBMock(){ 
    return $this->getMock('testWPDB', array('get_results'), array(), '', false); 
} 

function test_author(){ 
    $instance[ 'authors' ] = array(1, 2, 3); 
    $config = new UTCW_Config($instance, $this->utcw); 
    $db = $this->getWPDBMock('get_results'); 

    $db->expects($this->once()) 
    ->method('get_results') 
    ->with($this->stringContains('post_author IN (1,2,3)')); 

    $data = new UTCW_Data($config, $db); 
    $data->get_terms(); 
    } 
} 
0

मुझे लगता है कि आप इसे से विस्तार करने के लिए चाहते हैं? यदि यह कक्षा हमेशा वर्डप्रेस फ़ाइलों के अंदर लोड की जाएगी तो आपके पास वैश्विक $ wpdb तक पहुंच होगी।

class RandomClass { 

    private $wpdb = false; 

    public function __construct() { 
     global $wpdb; 

     if (is_object($wpdb)) { 
      $this->wpdb = $wpdb; 
     } 
    } 

    public function get_results($data) { 
     return $this->wpdb->get_results($data); 
    } 
}  
संबंधित मुद्दे

 संबंधित मुद्दे