【发布时间】:2013-11-13 20:09:32
【问题描述】:
我对 cakephp 名称约定有疑问,但在这里找不到我的错误。
我有两个模型,它们已连接(稍后我将包含代码),在索引文件中我无法显示来自两个不同表的数据。发生错误。
数据库错误 错误:SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“Com.info_id”
型号
(信息.php)
<?php
class Info extends AppModel
{
public $hasMany = array('Com');
public $validate = array(
'title'=>array(
'rule'=>'notEmpty'
),
'body'=>array(
'rule'=>'notEmpty'
)
);
}
?>
(com.php)
<?php
class Com extends AppModel
{
public $belongsTo = array('Info');
public $validate = array(
'mail'=>array(
'requierd'=>array(
'rule'=>array('notEmpty'),
'message'=>'Write your email'
)
),
'body'=>array(
'required'=>array(
'rule'=>array('notEmpty'),
'messages'=>'Write smth'
)
)
);
}
?>
控制器 (InfosController.php)
<?php
class InfosController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public function index()
{
$this->Info->recursive = 1;
$this->set('inform', $this->Info->find('all'));
}}
(ComsController.php)(我什至会从中删除索引,不起作用)
<?php
class ComsController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public function index()
{
$this->set('com', $this->Infos_com->find('all'));
}}
至少(View/Infos/index.ctp)(不重要,因为索引即使为空,错误仍然会发生),正文部分
<?php if (isset($inform)) {
foreach($inform as $info) {
echo $info['Info']['title']; echo '<br>';
echo $info['Info']['body'];
foreach($info['Com'] as $comment) {
echo $comment['Com']['body'];
}
}
} ?>
我的数据库中的表是 Infos 和 Coms。我之前只在 Info 表上工作,我没有任何问题,问题开始于我使用 $public $hasMany = array('Com')
我将不胜感激任何提示或建议。 最好的问候!!
【问题讨论】:
-
我不知道您的问题是什么,但是您的“Col”表中有一个名为“info_id”的列吗?
标签: php mysql sql cakephp model