CakePHP 检索数据忽略 'hasMany'
CakePHP retrieve data ignoring 'hasMany'
我有 2 张桌子 - Box 和 Apple。苹果属于一个盒子,一个盒子里有很多苹果。在代码中看起来像这样:
class Apple extends AppModel {
public $useDbConfig = 'somedb';
public $useTable = 'apple';
public $belongsTo = array(
'Box' => array(
'className' => 'Box',
'foreignKey' => 'box_id'
)
);
}
class Box extends AppModel {
public $useDbConfig = 'somedb';
public $useTable = 'box';
public $hasMany = array(
'Apple' => array(
'className' => 'Apple',
'foreignKey' => 'box_id',
'dependent' => true
)
);
}
当我请求箱子时:$this->Box->find("all")
我收到所有箱子的列表 + 每个箱子的所有苹果列表:
(Array):
[0] => (Array):
[Box] => (Array):
// some data
[Apple] => (Array):
[0] => (Array):
// some data
[1] => (Array):
// some data
...
[1] => (Array):
[Box] => (Array):
// some data
[Apple] => (Array):
[0] => (Array):
// some data
[1] => (Array):
// some data
...
那么,我怎样才能只收到一排盒子,而不收到苹果呢?
更新:
正如@Ben 回答的那样,我需要输入 recursive = -1 (or 0).
但是现在,我有这个问题的延续:如果我有一个模型 - Room
,房间可以包含盒子,所以 Room
中应该有 $hasMany
选项对于 Box
,$belongsTo
选项对于 Box
中的 Room
:
class Room extends AppModel {
public $useDbConfig = 'somedb';
public $useTable = 'room';
public $hasMany = array(
'Box' => array(
'className' => 'Box',
'foreignKey' => 'room_id'
)
);
}
// Edited Box class
class Box extends AppModel {
public $useDbConfig = 'somedb';
public $useTable = 'box';
public $hasMany = array(
'Apple' => array(
'className' => 'Apple',
'foreignKey' => 'box_id',
'dependent' => true
)
);
public $belongsTo = array(
'Room' => array(
'className' => 'Room',
'foreignKey' => 'room_id'
)
);
}
那么,我怎样才能把苹果、盒子和房间的数据集中起来呢?比方说,我有一个 Room
的条件:where room.name like "room0%"
。我尝试了下一件事:
$some = $this->Apple->find("all", array(
'joins' => array(
array('table' => 'room',
'alias' => 'Room',
'type' => 'INNER',
'conditions' => array(
'Room.id = Box.room_id',
)
)
),
'conditions' => array('Room.name LIKE' => 'room0%')
));
但是我只收到了一组Apple-Box "pairs"。
更新: 目前我找到的唯一解决方案是下一个:
$temp = "room0%";
$db = $this->Apple->getDataSource();
$some = $db->fetchAll(
"SELECT *
FROM apple
JOIN box on apple.box_id = box.id
JOIN room on box.room_id = room.id
WHERE room.name LIKE '$temp'"
);
有没有办法用模型做同样的事情?
在 cakephp 2 中,包含的内容会自动加入,在您的查找调用中设置 recursive => -1
。
对于复杂的关联查询,建议使用 Containable. With it, you can go deeper 而不是在 model/find 调用中仅使用 recursive = -1
,而无需手动进行手动连接。
设置 Containable 后,您的查询可能类似于*
$this->Apple->find('all', array(
'contain' => array(
'Box' => array(
'Room' => array(
'conditions' => array('Room.name LIKE' => 'room0%')
)
)
)
));
*) 只是我记忆中的模型,不能保证按原样工作
我有 2 张桌子 - Box 和 Apple。苹果属于一个盒子,一个盒子里有很多苹果。在代码中看起来像这样:
class Apple extends AppModel {
public $useDbConfig = 'somedb';
public $useTable = 'apple';
public $belongsTo = array(
'Box' => array(
'className' => 'Box',
'foreignKey' => 'box_id'
)
);
}
class Box extends AppModel {
public $useDbConfig = 'somedb';
public $useTable = 'box';
public $hasMany = array(
'Apple' => array(
'className' => 'Apple',
'foreignKey' => 'box_id',
'dependent' => true
)
);
}
当我请求箱子时:$this->Box->find("all")
我收到所有箱子的列表 + 每个箱子的所有苹果列表:
(Array):
[0] => (Array):
[Box] => (Array):
// some data
[Apple] => (Array):
[0] => (Array):
// some data
[1] => (Array):
// some data
...
[1] => (Array):
[Box] => (Array):
// some data
[Apple] => (Array):
[0] => (Array):
// some data
[1] => (Array):
// some data
...
那么,我怎样才能只收到一排盒子,而不收到苹果呢?
更新: 正如@Ben 回答的那样,我需要输入 recursive = -1 (or 0).
但是现在,我有这个问题的延续:如果我有一个模型 - Room
,房间可以包含盒子,所以 Room
中应该有 $hasMany
选项对于 Box
,$belongsTo
选项对于 Box
中的 Room
:
class Room extends AppModel {
public $useDbConfig = 'somedb';
public $useTable = 'room';
public $hasMany = array(
'Box' => array(
'className' => 'Box',
'foreignKey' => 'room_id'
)
);
}
// Edited Box class
class Box extends AppModel {
public $useDbConfig = 'somedb';
public $useTable = 'box';
public $hasMany = array(
'Apple' => array(
'className' => 'Apple',
'foreignKey' => 'box_id',
'dependent' => true
)
);
public $belongsTo = array(
'Room' => array(
'className' => 'Room',
'foreignKey' => 'room_id'
)
);
}
那么,我怎样才能把苹果、盒子和房间的数据集中起来呢?比方说,我有一个 Room
的条件:where room.name like "room0%"
。我尝试了下一件事:
$some = $this->Apple->find("all", array(
'joins' => array(
array('table' => 'room',
'alias' => 'Room',
'type' => 'INNER',
'conditions' => array(
'Room.id = Box.room_id',
)
)
),
'conditions' => array('Room.name LIKE' => 'room0%')
));
但是我只收到了一组Apple-Box "pairs"。
更新: 目前我找到的唯一解决方案是下一个:
$temp = "room0%";
$db = $this->Apple->getDataSource();
$some = $db->fetchAll(
"SELECT *
FROM apple
JOIN box on apple.box_id = box.id
JOIN room on box.room_id = room.id
WHERE room.name LIKE '$temp'"
);
有没有办法用模型做同样的事情?
在 cakephp 2 中,包含的内容会自动加入,在您的查找调用中设置 recursive => -1
。
对于复杂的关联查询,建议使用 Containable. With it, you can go deeper 而不是在 model/find 调用中仅使用 recursive = -1
,而无需手动进行手动连接。
设置 Containable 后,您的查询可能类似于*
$this->Apple->find('all', array(
'contain' => array(
'Box' => array(
'Room' => array(
'conditions' => array('Room.name LIKE' => 'room0%')
)
)
)
));
*) 只是我记忆中的模型,不能保证按原样工作