php - CAKEPHP - 自引用表覆盖变量

标签 php mysql cakephp cakephp-model

目前我正在创建一个应用程序来管理我的 TO-DO 事件。

在我的数据库模型中,我创建了一个引用任务表的表,这样我就可以为任务创建需求。这样,只要我完成了所需的任务,我就只能在列表中开始/显示任务。

型号: the model

该功能完美运行,但我对在您的 View 中接收变量的 cakephp 方式有疑问。

由于父子id都是[projecttasks][projecttasks_name],所以最后一个会覆盖第一个,导致页面上这样: the failed result

但是当我单击编辑时,您可以看到它实际上已正确保存: Correctly saved

现在,在代码中它在 View 中看起来像这样:

<?php foreach ($itemrequirements as $itemrequirement): ?>
    <tr>
        <td><?php echo h($itemrequirement['Projecttask']['projecttasks_name']); ?></td>
        <td><?php echo h($itemrequirement['Projecttask']['projecttasks_name']); ?></td>
        <td class="actions">
            <?php echo $this->Html->link('View',array('action' => 'detail', $itemrequirement['Itemrequirement']['itemreq_id'])); ?>
            <?php echo $this->Html->link('Edit',array('action' => 'edit', $itemrequirement['Itemrequirement']['itemreq_id'])); ?>
            <?php echo $this->Form->Postlink('Delete',array('action' => 'delete', $itemrequirement['Itemrequirement']['itemreq_id']), 
            null, sprintf('Are you sure you want to delete %s?', $itemrequirement['Itemrequirement']['itemreq_id'])); ?>
        </td>
    </tr>
    <?php endforeach; ?>

Controller :

public function index() {

    // write redirect information for screens into session
    $this->Session->write('Redirector.controllername', 'itemrequirements');
    $this->Session->write('Redirector.controllerfunction', 'index');
    $this->Session->write('Redirector.recordindex', NULL);

    // build index
    $this -> Itemrequirement -> recursive = 0;
    $this -> set('itemrequirements', $this -> paginate());

    // get total record count
    $totalItemrequirements = $this -> Itemrequirement -> find('count');
    // expose total-count to the view
    $this -> set('totalItemrequirements', $totalItemrequirements);

}

我问过我的老板,他指出了这部分(模型文件)。想我会分享。

/**
 * declare BelongsTo relations
 *
 * @var array $belongsTo
 * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto BelongsTo docs (CakePHP cookbook)
 */
public $belongsTo = array(
    'Projecttask' => array(
        'className' => 'Projecttask',
        'foreignKey' => 'itemreqs_rel_projectparents'
    ),
    'Projecttask' => array(
        'className' => 'Projecttask',
        'foreignKey' => 'itemreqs_rel_projectchilds'
    )
);

有什么方法可以让 View 正常工作吗?我是 MVC 模型的新手,我的大部分代码都是使用我们在工作中运行的代码生成器编写的。

最佳答案

只需让您的模型为父项和子项使用不同的名称,如下所示:

public $belongsTo = array(
    'ProjecttaskParent' => array(
        'className' => 'Projecttask',
        'foreignKey' => 'itemreqs_rel_projectparents'
    ),
    'ProjecttaskChild' => array(
        'className' => 'Projecttask',
        'foreignKey' => 'itemreqs_rel_projectchilds'
    )
);

另外,如果你看看 cake model conventions你会发现你还没有真正正确地设置以召唤蛋糕的最小配置的完整功能。它会起作用,但您需要做的比您应该做的更多。

关于php - CAKEPHP - 自引用表覆盖变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18870125/

相关文章:

php - 从异常处理程序中触发错误

php - 使用正则表达式阻止某些电子邮件提供商

php - 获取一列的所有条目

php - 我应该使用哪个框架来确保更好的长期升级/可维护性,CakePHP 还是 CodeIgniter?

php - (Cake)PHP - 使用来自主数据库的数据更新子数据库

javascript - 将MySQL结果放入php变量

php - 从 js 调用 php (w/ajax)

mysql - 无法增加 innodb_buffer_pool_size 值

mysql - SQL,查找两个给定名称在一列中是否具有相同数字的查询

CakePHP 在路由中使用正则表达式变量捕获?