Cakephp 发现没有按预期工作

发布于 2025-01-03 09:22:35 字数 1387 浏览 1 评论 0原文

我正在开发一个小应用程序来帮助学习 cakephp 2.0。它是一个简单的任务管理器应用程序。

  • 用户
  • 任务
  • 任务类型

我在任务表中设置了一个名为“user_id”的外键 因为任务属于用户。

现在我想做的就是显示某个用户的任务,尽管当我测试它时 sql 查询得到了正确的结果,但它根本不起作用,任何帮助都会很棒。

在我的任务控制器中我有方法;

//show the tasks of the current user
public function mytasks(){

    $this->set('title_for_layout', 'Gerrys TaskManager: Display Tasks');

    $this->Task->recursive = 0;         

    $tasks=$this->Task->find('all', array('conditions'=>array('username'=>'admin')));       

    $this->set('tasks', $this->paginate());     
}

我正在尝试查找用户“管理员”的任务或“相关任务”我做错了什么?

我怀疑问题可能出在我的模型上;

usermodel

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Task' => array(
            'className' => 'Task',
            'foreignKey' => 'user_id'
        )
    );

任务模型;

public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Tasktype' => array(
        'className' => 'Tasktype',
        'foreignKey' => 'tasktype_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

I'm working on a small app to help learn cakephp 2.0. Its a simple task manager app.

Tables

  • users
  • tasks
  • tasktypes

I've a set up a foreign key in the tasks table called 'user_id'
As a task belongs to a user.

Now all I'm trying to do is display the tasks of a certain user and it wont work at all despite the sql query getting correct results when I tested it any help would be great.

In my taskscontroller I've the method;

//show the tasks of the current user
public function mytasks(){

    $this->set('title_for_layout', 'Gerrys TaskManager: Display Tasks');

    $this->Task->recursive = 0;         

    $tasks=$this->Task->find('all', array('conditions'=>array('username'=>'admin')));       

    $this->set('tasks', $this->paginate());     
}

I'm trying to find the tasks of the user 'admin' or the "realted tasks" what am I doing wrong?

I suspect the problem maybe to with my models;

usermodel

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Task' => array(
            'className' => 'Task',
            'foreignKey' => 'user_id'
        )
    );

task model;

public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Tasktype' => array(
        'className' => 'Tasktype',
        'foreignKey' => 'tasktype_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月隐月明月朦胧 2025-01-10 09:22:35

要获取属于用户的任务,在 UsersController 中执行此操作要简单得多。当然,您必须在 UserModel 中设置 User hasMany Task。

现在您可以创建一个函数,例如通过 id 查看用户及其任务:

public function view($id = null) {
        $this->User->id = $id;
        $this->set('user', $this->User->read(null, $id));
    }

任务现在应该可以通过 $user['tasks'] 在您的视图中使用。您必须在网址中传递 id,例如: /path_to_your_app/users/view/1

您始终可以使用 debug() 来查看数组或变量的内容。要查看您所获得的内容,请将 添加到您的视图中。

您很可能希望在视图中列出带有 foreach 的任何任务,非常简单的示例::

foreach($user['Task'] as $task) {
    echo $task['field1'] . ": " . $task['fied2'];
    echo "<br />";
}

当然,您可能希望在表格或其他内容中显示任务。

学习 cakePHP 的一个很好的起点是博客教程:

http: //book.cakephp.org/2.0/en/getting-started.html#blog-tutorial

To get the tasks that belong to a user, it's much simpler to do that in the UsersController. Of course you have to set User hasMany Task in your UserModel.

Now you can create a function, for example to view the User and it's Tasks by id:

public function view($id = null) {
        $this->User->id = $id;
        $this->set('user', $this->User->read(null, $id));
    }

The Tasks should now be available in your view by $user['tasks']. You have to pass the id in the url like: /path_to_your_app/users/view/1

You can always use debug() to see contents of an array or variable. To see what you've got add <?php debug($user);?> to your view.

You most likely want to list any tasks with a foreach in your view, very simple example::

foreach($user['Task'] as $task) {
    echo $task['field1'] . ": " . $task['fied2'];
    echo "<br />";
}

of course you might want to display the tasks in tables or something.

A good starting point for learning cakePHP is the Blog-Tutorial:

http://book.cakephp.org/2.0/en/getting-started.html#blog-tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文