$this->;未使用CakePHP 1.3设置模型id

$this->;未使用CakePHP 1.3设置模型id,cakephp,model,Cakephp,Model,我试图在cns_controller.php文件的index()方法中检索数据库中记录的id。我想用它来查找()。但是,$this->Cn->id没有返回值,因此find()也不起作用 $uses = array('Cn', 'Release'); /*check non-null value in released_user_id, showing Release tab has been signed off*/ $releaseSignedOff = $this->Releas

我试图在cns_controller.php文件的index()方法中检索数据库中记录的id。我想用它来查找()。但是,$this->Cn->id没有返回值,因此find()也不起作用

$uses = array('Cn', 'Release');

/*check non-null value in released_user_id, 
 showing Release tab has been signed off*/
$releaseSignedOff = $this->Release->find('all', array(
    'conditions' => array('Release.cn_id =' => $this->Cn->id,
                    'Release.released_user_id !=' => null)
));
(sizeof($releaseSignedOff) > 0) ? $releaseSignedOff = true : $releaseSignedOff = false;
$this->set('releaseSignedOff', $releaseSignedOff);
有什么想法吗


谢谢

我最终没有使用我的
索引
视图,而是在我的
cns
数据库表中查找最新记录,并使用返回值作为参数重定向到
视图
视图:

if (!$id) {
    $most_recent_change_note = $this->Cn->find('first', array(
        'order' => array('Cn.id DESC')
    ));
    $this->redirect(array('controller' => 'cns', 
        'action' => 'view/'.$most_recent_change_note['Cn']['id']));
}
对于分页,我最终使用了CakePHP的
$neights
功能:

function get_neighbors($id) {
    $neighbors = $this->Cn->find('neighbors', array('field' => 'id', 
        'value' => $id));
    $this->set('neighbors', $neighbors);
}
然后在我的
视图中
视图中,我使用这些值创建链接:

<div class="paging">
    <?php echo ($neighbors['next']) ? $html->link('<< Prev', 
         array('controller'=>'cns',
         'action'=>'view/'.$neighbors['next']['Cn']['id'])).' | ' : '<< Prev | ';
    echo $html->link('Next >>', array('controller'=>'cns', 
         'action'=>'view/'.$neighbors['prev']['Cn']['id'])); ?>
</div>


感谢查尔斯和罗斯帮我得出这个结论。:)

您是否正在将记录保存到
$this->Cn
?如果没有,则没有要返回的ID。另外,应该非常确定的是
$uses=array('Cn','Release')
不,我当时正在做的是收集数据,以便推送到“index”视图。是的,它在我的代码中是一个数组,只是在上面键入了错误-现已修复。我的view()方法与index()方法的工作原理基本相同,只是它接受一个“cn_id”参数,然后我在find()中使用它。显然,index()没有此参数:|好的,用Cake将
$this->Model->id
作为MySQL的别名
last\u insert\u id()
。此属性仅存储上次保存的记录的ID。如果您试图使用的ID已保存在
cns
db表中,则需要调用
$this->Cn->find()
方法来获取您要查找的ID。此外,“视图”和“索引”视图的查看和操作方式相同。唯一的区别是“索引”视图具有分页功能。两个页面以相同的方式构建的原因是,即使在“视图”和“索引”视图中切换,用户也会认为它们位于同一页面上。“索引”视图是登录页,但一旦它们与站点交互(保存、编辑等),就会使用id作为参数重定向到特定的“视图”。