2016-09-30 8 views
1

私は30個以上の列がある注文テーブルを持っています。私はordersテーブルからattribute列以外のすべての列を表示したいと思います。CakePHPの1つの列を除くすべての列を選択しますか?

我々は、すべてのテーブルから特定の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 */ 
関連する問題