【发布时间】:2014-03-22 04:23:39
【问题描述】:
我是 cakephp 新手,在从另一个模型中检索数据时遇到了一点问题。
我的表关联是这样的:
Items hasMany Stocks
Stocks belongsTo Items
Items belongsTo UnitMeasurement
UnitMeasurement hasMany Items
我的问题是我想在 Stocks index html 表格视图中显示 UnitMeasurement 名称。
当前 Html 表格视图:
----------------------------------------------------------------------
Item | Stock Balance| Unit | Created |
----------------------------------------------------------------------
Microprocessor | 12 | DISPLAY UNIT NAME HERE!| 19/1/2014 |
Microcontroller| 20 | DISPLAY UNIT NAME HERE!| 19/1/2014 |
CPU | 12 | DISPLAY UNIT NAME HERE!| 19/1/2014 |
----------------------------------------------------------------------
如何进行成功的查询?
谢谢。
编辑:
这是我在 StocksController 索引函数中的查找查询:
public function index() {
$this->Stock->recursive = 0;
$order = "Stock.id DESC";
//set orderby
$stock = $this->Stock->find('all',array('order'=>$order));
$this->set('stocks', $stock);
//query to find UnitMeasurement Name for Stocks Item
foreach ($stock as $stocks) {
//debug($stocks);
$this->loadModel('Items');
$unitMeasurement = $this->Items->find('first',array('fields'=>'unit_measurement_id','conditions'=>array('Items.id'=>$stocks['Stock']['item_id'])));
$this->set('units',$unitMeasurement);
}
}
再次感谢。
【问题讨论】:
-
您可以粘贴您的查找查询吗?在哪个模型上应用 find ?
-
@sismaster 我已经编辑了我的问题。