0

我对 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') 时问题就开始了

我将不胜感激任何提示或建议。此致 !!

4

2 回答 2

1

您需要阅读 Cake 手册以了解约定。根据您将一个表与另一个表关联的方式,它期望一个表中表单的外键与relatedtable_id另一个表相关:http: //book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

此外,正如@thecodeparadox 指出的那样,Infos_com它不是一个有效的模型。该约定适用于表示两个表之间的连接表InfoCom为两个表保存外键的模型。我强烈建议您bake在确保您的数据库设置为约定后使用该实用程序:http: //book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html

于 2013-11-13T20:29:35.777 回答
0
  1. info_id你的Com桌子上应该有一个。

  2. 此处 $this->Infos_com->find('all'),Infos_com不是有效的型号名称。

于 2013-11-13T20:23:29.943 回答