0

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>
1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

debug($products); is probably more useful - since that definitely exists.
Thank's very much! You are excelent!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.