2013-07-07 6 views
6

अपने मॉडल में हालत के लिए कैसे में:ZF2 निष्पादित या कहां खंड

SELECT * FROM member WHERE username='<< ENTERED VALUE >>' OR id_customer='<< ENTERED VALUE >>'; 
:

SELECT * FROM member WHERE username='<< ENTERED VALUE >>'; 

लेकिन मैं निम्न क्वेरी निष्पादित हैं:

$rowset = $this->tableGateway->select(array('username' => $identifier)); 
$row = $rowset->current(); 
return $row; 

यह निम्न क्वेरी निष्पादित करता है

मॉडल फ़ाइल में मुझे क्या बदलाव करना है?

और कृपया इसके बारे में उपयोगी ब्लॉग का सुझाव दें। मैं ZF2 दस्तावेज़ीकरण में इसके लिए उत्तर नहीं ढूंढ सकता।

उत्तर

4

ऐसा करने के लिए सबसे आसान तरीका स्पष्ट OR कीवर्ड:

$where = new Zend\Db\Sql\Where; 
$where->equalTo('username', $identifier); 
$where->OR->equalTo('id_customer', $customerId); 

$rowset = $this->tableGateway->select($where); 
$row = $rowset->current(); 
return $row; 
2

मैं जेडएफ 2 के साथ तुलना में जेडएफ 1 के साथ और अधिक अनुभव है इसलिए वहाँ अन्य (बेहतर, सरल) समाधान हो सकता है, लेकिन इस चाल करना चाहिए: उपयोग कर रहा है

// Manually build the Select object 
$select = $this->tableGateway->getSql()->select(); 

// Create array containing the fields and their expected values 
$conditions = array('username' => 'foo', 'id_customer' => 123); 

// Add these fields to the WHERE clause of the query but place "OR" in between 
$select->where($conditions, \Zend\Db\Sql\Predicate\PredicateSet::OP_OR); 

// Perform the query 
$rowset = $this->tableGateway->selectWith($select); 
3

पार्टी के लिए एक छोटी सी देर हो गई, लेकिन पूर्णता मैंने सोचा कि मैं एक विकल्प है कि बहुत अच्छी तरह से मेरे लिए काम किया प्रदान करते हैं चाहते हैं, और एक के लिए थोड़ा आसान कुछ डेवलपर्स के लिए लागू करने के लिए हो सकता है कि :

// '$gateway' is a Zend\Db\TableGateway\TableGateway object... 
$search_string = 'something'; 
$select = $gateway->select(function($select) use($search_string) { 
    $select->where->OR->like('first_name', '%'. $search_string .'%'); 
    $select->where->OR->like('last_name', '%'. $search_string .'%'); 
}); 

इसे चलाने के बाद, $select आपके परिणाम सेट को पकड़ने के लिए तैयार होगा, जो लूप के माध्यम से तैयार होगा।

आशा इस कोई मदद करता है! :)