2016-09-30 52 views
1

我有一個訂單表有30多列。我想從我的orders表中顯示除attribute列以外的所有列。選擇除CakePHP中的一列以外的所有列?

我們都知道,從表中選擇特定ID的所有列,我們可以用

$data = $this->Order->find('first', array('conditions'=>array('Order.id'=>1))); 

你能告訴我怎麼寫在CakePHP的2.x的查詢?

回答

2

我懷疑你會從你的查詢中排除列獲得很多。如果你真的需要做到這一點,那麼你可以得到model's schema,然後從刪除列,然後通過定義你想要查詢返回的字段使用剩餘的列在你的find(): -

// Get the model’s schema. 
$schema = $this->Order->schema(); 
// Remove the `attribute` column from the schema. 
unset($schema['attribute']); 
// Determine the remaining columns from the schema. 
$cols = array_keys($schema); 

// Now call your query specifying the fields you want back using the 
// columns we’ve just determined. 
$data = $this->Order->find('first', 
    array(
     'fields' => $cols, 
     'conditions' => array('Order.id' => 1) 
    ) 
); 
+0

好的。讓我檢查你的答案 – Chinmay235

1

你應該使用getColumnTypes()這裏:

$fields = array_keys($this->Order->getColumnTypes()); /* Returns an associative array of field names and column types*/ 
$key = array_search('attribute', $fields);    /* Search the key having attribute field */ 
unset($fields[$key]);   /* Remove the key value pair corresponding to attribute */ 
$this->Order->find('all', array('fields' => $fields)); /* Apply search specifying the fields */ 
相關問題