Question

I can not display the data.

My model (app/Model/Product.php)

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

My controller (app/Controller/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());
  }
}

My view (app/View/ver.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>
Was it helpful?

Solution

You are not using the default primary key: id. Define your primary key in your model:

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

... and assing requesting product id to id in your controller. id will match to primary key id_producto in your database table:

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());
}

Some notes:

In your controller you assign the results to plural 'products':

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

But in your view you use the variable in singular $product:

Try to use <?php debug($products['Product']['nombre']); ?> instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top