Cakephp 发现没有按预期工作
我正在开发一个小应用程序来帮助学习 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取属于用户的任务,在 UsersController 中执行此操作要简单得多。当然,您必须在 UserModel 中设置 User hasMany Task。
现在您可以创建一个函数,例如通过 id 查看用户及其任务:
任务现在应该可以通过
$user['tasks']
在您的视图中使用。您必须在网址中传递 id,例如: /path_to_your_app/users/view/1您始终可以使用
debug()
来查看数组或变量的内容。要查看您所获得的内容,请将添加到您的视图中。
您很可能希望在视图中列出带有 foreach 的任何任务,非常简单的示例::
当然,您可能希望在表格或其他内容中显示任务。
学习 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:
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/1You 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::
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