【问题标题】:CakePHP Convention name or smth connectedCakePHP 约定名称或 smth 连接
【发布时间】: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


【解决方案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

【讨论】:

    【解决方案2】:
    1. 您的Com 表中应该有一个info_id

    2. 这里 $this-&gt;Infos_com-&gt;find('all')Infos_com 不是有效的型号名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多