2013-01-16 21 views
5

मैं ज़ेंड 2 में दो टेबल के बीच एक साधारण INNER JOIN करना चाहता हूं।टेबल से कई टेबल के साथ टेबलगेटवे

Concretely, मैं Zend2 में यह करने के लिए करना चाहते हैं:

SELECT * FROM foo, bar WHERE foo.foreign_id = bar.id;

मैं एक FooTable:

class FooTable 
{ 
    protected $tableGateway; 

    public function __construct(TableGateway $tableGateway) 
    { 
    $this->tableGateway = $tableGateway; 
    } 

    public function get($id) 
    { 
    $rowset = $this->tableGateway->select(function (Select $select) { 
     $select->from('foo'); 
    }); 
    } 
} 

$select->from('foo'); रिटर्न एक त्रुटि:

==>चूंकि यह ऑब्जेक्ट कन्स्ट्रक्टर में एक टेबल और/या स्कीमा के साथ बनाया गया था, यह केवल पढ़ा जाता है।

तो, मैं FooTable और BarTable के बीच एक साधारण आंतरिक जुड़ने के लिए अपने विवरण से ट्विक नहीं कर सकता।

उत्तर

12

मुझे आशा है कि यह अपनी यात्रा के साथ तुम्हारी मदद करेगा के रूप में इस एक काम उदाहरण मेरे पास है:

namespace Pool\Model; 

use Zend\Db\TableGateway\AbstractTableGateway; 
use Zend\Db\Sql\Select; 

class IpaddressPool extends AbstractTableGateway 
{ 
    public function __construct($adapter) 
    { 
     $this->table = 'ipaddress_pool'; 

     $this->adapter = $adapter; 

     $this->initialize(); 
    } 

    public function Leases($poolid) 
    { 
     $result = $this->select(function (Select $select) use ($poolid) { 
      $select 
       ->columns(array(
        'ipaddress', 
        'accountid', 
        'productid', 
        'webaccountid' 
       )) 
       ->join('account', 'account.accountid = ipaddress_pool.accountid', array(
        'firstname', 
        'lastname' 
       )) 
       ->join('product_hosting', 'product_hosting.hostingid = ipaddress_pool.hostingid', array(
        'name' 
       )) 
       ->join('webaccount', 'webaccount.webaccountid = ipaddress_pool.webaccountid', array(
        'domain' 
       ))->where->equalTo('ipaddress_pool.poolid', $poolid); 
     }); 

     return $result->toArray(); 
    } 
} 
+0

ऊपर के उदाहरण में 'use' मतलब क्या है/हैं? क्या यह एक Lambda समारोह है? घोषित फ़ंक्शन के भीतर बाहरी चर का उपयोग करने के लिए 'उपयोग' का उपयोग किया जाता है? –

+1

मुझे लगता है कि आप 'उपयोग ($ पूलिड)' अनुभाग के बारे में बात कर रहे हैं। चूंकि यह $-> चयन एक फ़ंक्शन() को कॉल कर रहा है, इसलिए मुझे अपनी क्वेरी में एक चर का उपयोग करने की भी आवश्यकता है, 'उपयोग ($ poolid)' उस क्षमता को अनुमति देता है। सामान्य कार्य 'फ़ंक्शन नाम ($ var1, $ var2) ', लेकिन इस मामले में हम – Diemuzi

+0

का चयन करने से फ़ंक्शन कॉल कर रहे हैं, मुझे कोई जवाब नहीं है ... मुझे जवाब मिला है ... मेरे पास 4 साल से बहुत कुछ बदल गया है इससे दूर हो गया यहां पर अच्छी पोस्ट है: http://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-the-function-use-identifier-should-a-sane-programmer-use –