不能从cakephp中的HABTM获取所有用户数据 [英] Cant' get all User Data from HABTM in cakephp
问题描述
我花了三天时间处理这个问题,但已经到了死胡同,无法继续。我知道如何使用非cakephp方法获取所需的数据,但我更喜欢使用cakephp的模型关系。
I've spent three days working this issue but has come to a dead end and can't proceed. I know how to get the needed data using non cakephp methods, but I prefer to use cakephp's model relation.
MY MODELS
MY MODELS
class User extends AppModel {
var $hasOne = 'Profile';
}
class Profile extends AppModel {
var $belongsTo = 'User';
var $hasAndBelongsToMany = array('Quad' => array('className' => 'Quad'));
}
class Quad extends AppModel{
var $belongsTo = 'User';
var $hasAndBelongsToMany = array ('Performer' => array ('className' =>'Performer'));
}
class Performer extends AppModel {
var $name = 'Performer';
var $hasAndBelongsToMany = array('Quad' => array('className' => 'Quad'));
}
MY HABTM表
(perfomers:id | name | image | created | modified)
(quads:id | user_id | etc .....)
(performers_quads:id | performer_id | quad_id)
(perfomers:id|name|image|created|modified) (quads:id|user_id|etc.....) (performers_quads:id|performer_id|quad_id)
我的个人资料控制器索引方法
public function index($uid) {
$user = $this->Profile->User->find('all', array(
'contain' => array(
'Quad' => array('contain'=>array('Performer'))),
'conditions' => array(
'User.id' => $uid)));
$this->set(compact('user'));
}
我的结果数组
$ b
My Result Array
UserArray (
[0] => Array (
[User] => Array (
[id] => 19
[email] => *****@gmail.com
[password] => ***
[created] => 2013-06-13 23:35:42
[modified] => 2013-06-13 23:35:42 )
[Profile] => Array (
[id] => 21
[user_id] => 19
[username] => ****
[first_name] => ****
[last_name] => ****
[imageId] => ****.jpg
[created] => 2013-06-13 23:35:43
[modified] => 2013-06-17 01:05:22 )
[Quads] => Array (
[0] => Array (
[id] => 65 [user_id] => 19
[title] => Test Event
[date] => 2013-06-15 16:53:00
[price] => 25.00
[venueID] => 1
[created] => 2013-06-17 01:05:22 )
[1] => Array (
[id] => 66
[user_id] => 19
[title] => Test Event 2
[date] => 2013-06-12 17:12:00
[price] => 50.00
[venueID] => 1
[created] => 2013-06-17 01:05:22 ) ) ) )
/ strong>
What I need
UserArray (
[0] => Array (
[User] => Array (
[id] => 19
[email] => *****@gmail.com
[password] => ***
[created] => 2013-06-13 23:35:42
[modified] => 2013-06-13 23:35:42 )
[Profile] => Array (
[id] => 21
[user_id] => 19
[username] => ****
[first_name] => ****
[last_name] => ****
[imageId] => ****.jpg
[created] => 2013-06-13 23:35:43
[modified] => 2013-06-17 01:05:22 )
[Quads] => Array (
[0] => Array (
[id] => 65 [user_id] => 19
[title] => Test Event
[date] => 2013-06-15 16:53:00
[price] => 25.00
[venueID] => 1
[created] => 2013-06-17 01:05:22
[Performer] => Array(
[0] => Array(
[id] => ***
[name] => ****
etc....)))
[1] => Array (
[id] => 66
[user_id] => 19
[title] => Test Event 2
[date] => 2013-06-12 17:12:00
[price] => 50.00
[venueID] => 1
[created] => 2013-06-17 01:05:22
[Performer] => Array(
[0] => Array(
[id] => ***
[name] => ****
etc....)))))))
我想要完成的是让Performers属于属于ProfilesController中User的Quads。
What I'm trying to accomplish is to get the Performers belonging to Quads belonging to User in the the ProfilesController. Any help with this will be greatly appreciated.
推荐答案
这是我最终结束了解决我的问题
This is how I finally ended up solving my problem
型号:
class AppModel extends Model {
var $actsAs = array(
'Containable');
}
class Performer extends AppModel {
var $name = 'Performer';
}
class Quad extends AppModel {
var $belongsTo = 'User';
var $hasAndBelongsToMany = array ('Performer');
}
class Profile extends AppModel {
var $belongsTo = 'User';
}
class User extends AppModel {
var $hasOne = 'Profile';
var $hasMany = 'Quad';
}
在个人资料控制器中:
In the Profile Controller:
public function index($uid)
{
$user = $this->Profile->User->find('all', array(
'contain' => array('Quad' =>array('Performer'), 'Profile'),
'conditions' => array('User.id' => $uid)));
$this->set(compact('user'));
}
结果: b
$ b
AND THE RESULT:
UserArray
(
[0] = Array
(
[User] = Array
(
[id] = 19
[email] = *****@gmail.com
[password] *****
[created] = 2013-06-13 23:35:42
[modified] = 2013-06-13 23:35:42
)
[Profile] = Array
(
[id] = 21
[user_id] = 19
[username] = ********
[first_name] = *********
[last_name] = **********
[imageId] = id8fkwd_b81a24524aa20f2c4156277d1b6a7c27_1371445522.jpg
[created] = 2013-06-13 23:35:43
[modified] = 2013-06-17 01:05:22
)
[Quad] = Array
(
[0] = Array
(
[id] = 65
[user_id] = 19
[title] = Test Event
[date] = 2013-06-15 16:53:00
[price] = 25.00
[venueID] = 1
[Performer] = Array
(
[0] = Array
(
[id] = 1
[name] = ********
[verified] = 1
[photo] = 1
[photoname] = *****1370810034.jpg
)
[1] = Array
(
[id] = 2
[name] = ******
[verified] = 1
[photo] = 1
[photoname] = ******_1370810315.jpg
)
)
[created] = 2013-06-15 16:53:00
)
[1] = Array
(
[id] = 66
[user_id] = 19
[title] = Test Event 2
[date] = 2013-06-12 17:12:00
[price] = 50.00
[venueID] = 1
[Performer] = Array
(
[0] = Array
(
[id] = 1
[name] = *******
[verified] = 1;
[photo] = 1
[photoname] = *****_1370810034.jpg
)
[1] = Array
(
[id] = 2
[name] = ******
[verified] = 1
[photo] = 1
[photoname] = ******_1370810315.jpg
)
[2] = Array
(
[id] = 3
[name] = ********
[verified] = 1
[photo] = 1
[photoname] = *****_1370812455.jpg
)
)
[created] = 2013-06-15 16:53:00
)
)
)
)
否则通过CakePHP的HABTM隧道....
Hope this help somebody else going through CakePHP's HABTM tunnel....
这篇关于不能从cakephp中的HABTM获取所有用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!