douxin2003 2014-02-13 22:28
浏览 9
已采纳

检查数据库中是否存在id,如果没有重定向到我的add cakephp [关闭]

thanks see I have this problem I want to check if my id (of my user), exist into a other table if this not exist redirect to my add.

table1

| users |

| id |

| username |

| password |

| creation |

| type |

table2

| user_azucar |

| id |

| id_user |

| cant |

| medaza |

| cachaza |

I don't want to use hasone because is not for this case.

I want compare id==id_user exist a session for this. controller

class IngeniosController extends AppController
{
    public function add()
    {

    }
    public function index()
    {
        $this->loadModel('Ingenio');
        $conditions = array(
            'conditions' => array(
                'UserAzucar.id_user' => $this->Session->read('User.id')
            )
        );
        $result = $this->User->find('first', $conditions);
        if (isset($result['User'])){
           $this->redirect(array('action' => 'add'));
        }
    }
    public function delete()
    {

    }
}

model

<?php
/**
* 
*/
class Ingenio extends AppModel
{
    public $name = 'UserAzucar';
    public $useTable = 'usuarios_azucar';
    public $primaryKey = 'id_azucarusuario';

}
?>
  • 写回答

3条回答 默认 最新

  • doufu2396 2014-02-14 07:29
    关注

    table2 is user_azucar or usuarios_azucar?

    And why the model Name is different from the $name attribute?

    You are using strange conventions for your names (why don't you follow cake conventions?)

    but if I understand your schema then you have to do this:

    $conditions = array(
        'conditions' => array(
            'id_user' => $this->Session->read('User.id')
         )
    );
    
    $result = $this->Ingenio->find('first', $conditions);
    
    if (empty($result)){
        $this->redirect(array('action' => 'add'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化