2014-04-30 52 views
0

我無法顯示數據。CakePHP模型顯示全部數據null

我的模型(APP /型號/ Product.php)

class Product extends AppModel { 
public $name = 'Product'; 
} 

我的控制器(應用程序/控制器/ ProductsController.php):

class ProductsController extends AppController { 
public $helpers = array('Html','Form','Session'); 
public $components = array('Session'); 
public $name = 'Products'; 

public function ver($id = null) { 
$this->Product->id_producto = $id; 
debug($this->Product->id_producto); //Show id with value Ex. 12,34,... 
$this->set('products', $this->Product->read()); 
    } 
} 

我的視圖(app /視圖/版本.ctp):

<?php debug($product['Product']['nombre']); ?> // Show 'null' 
<h2><?php echo $product['Product']['nombre']?></h2></td></tr> 
<strong>Descripci&oacute;n:</strong> <?php echo $product['Product']['descripcion']?> <br/> 
<small>Fecha de registro: <?php echo $product['Product']['fecha_reg']?></small> <br/> 
<small>Última modificación: <?php echo $product['Product']['Fecha_mod']?></small> 
+0

檢出http://book.cakephp.org/2.0/en/models/retrieving-your-data.html和http://stackoverflow.com/questions/21819451/fetching-and-displaying-data-with -cakephp-2 -x解決方案正如@nedstark所說,你應該使用'find' – cornelb

回答

2

您沒有使用默認主鍵:id。定義主鍵在模式

class Product extends AppModel 
{ 
    public $primaryKey = 'id_producto'; 
} 

...和assing請求的產品ID來id控制器id將匹配到主鍵id_producto在你的數據庫表:

public function ver($id = null) 
{ 
    $this->Product->id = $id; 
    debug($this->Product->id); //Show id with value Ex. 12,34,...; 
    $this->set('product', $this->Product->read()); 
} 

一些注意事項:

在你控制器分配結果,以複數的 '產品':

$this->set('products', $this->Product->read()); 

但在您的視圖您使用單數的變量$product

嘗試使用<?php debug($products['Product']['nombre']); ?>代替。

+1

'debug($ products);'可能更有用 - 因爲這肯定存在。 – AD7six

+0

非常感謝!你很棒! – hmaceves