【问题标题】:CakePHP Model display all data nullCakePHP 模型显示所有数据 null
【发布时间】:2014-04-30 08:47:57
【问题描述】:

我无法显示数据。

我的模型(app/Model/Product.php)

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

我的控制器(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());
  }
}

我的观点(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>

【问题讨论】:

标签: cakephp model controller


【解决方案1】:

您没有使用默认主键:id。在您的模型中定义您的主键:

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

...并在您的控制器中将请求产品ID分配给idid 将匹配您数据库表中的主键 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

尝试改用&lt;?php debug($products['Product']['nombre']); ?&gt;

【讨论】:

  • debug($products); 可能更有用——因为它确实存在。
  • 非常感谢!你太棒了!
猜你喜欢
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
相关资源
最近更新 更多