CakePHP公约名称或smth连接

时间:2013-11-13 20:09:32

标签: php mysql sql cakephp model

我遇到了cakephp名称约定的问题,但我在这里找不到我的错误。

我有两个模型,并且它们已加入(我将在稍后包含代码),在索引文件中我有问题来显示来自两个不同表的数据。发生错误。

  

数据库错误   错误:SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'Com.info_id'

模型

(info.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')

时启动

我会感激任何提示或建议。 最诚挚的问候!!

2 个答案:

答案 0 :(得分:1)

您需要阅读约定的蛋糕手册。根据您将一个表与另一个表相关联的方式,它希望一个表中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

答案 1 :(得分:0)

  1. 您的info_id表格中应该有Com

  2. 下面 $this->Infos_com->find('all')Infos_com不是有效的模型名称。

相关问题