使用CakePHP HABTM时遇到问题。
我有以下型号。
class Repositorio extends AppModel{
var $name="Repositorio";
var $hasAndBelongsToMany = array(
'Sesion' =>
array(
'joinTable' => 'sesions_repositorios',
'dependent' => true
)
);
var $order=array('Repositorio.name'=>'ASC');
}
class Sesion extends AppModel{
var $name="Sesion";
var $belongsTo=array(
'SesionsEstado',
'Asignatura',
'User'
);
var $hasAndBelongsToMany = array('Repositorio'=>
array(
'joinTable'=>'sesions_repositorios',
'dependent' => true
)
);
var $order=array('Sesion.ffin'=>'ASC');
}
以下数据库表。
CREATE TABLE sesions (
id INT(11) NOT NULL AUTO_INCREMENT,
user_id INT(11) NOT NULL,
sesions_estado_id INT(11) NOT NULL,
asignatura_id INT(11) NOT NULL,
name VARCHAR(100) NOT NULL,
finicio DATETIME NOT NULL,
ffin DATETIME NOT NULL,
created DATETIME NOT NULL,
modified DATETIME NOT NULL,
PRIMARY KEY(id),
INDEX sesions_FKIndex1(sesions_estado_id),
INDEX sesions_FKIndex2(asignatura_id),
INDEX sesions_FKIndex3(user_id)
);
CREATE TABLE repositorios (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
created DATETIME NOT NULL,
modified DATETIME NOT NULL,
PRIMARY KEY(id)
);
CREATE TABLE sesions_repositorios (
id INT(11) NOT NULL AUTO_INCREMENT,
sesion_id INT(11) NOT NULL,
repositorio_id INT(11) NOT NULL,
PRIMARY KEY(id),
INDEX sesions_repositorios_FKIndex1(sesion_id),
INDEX sesions_repositorios_FKIndex2(repositorio_id)
);
当我在存储库中保存数据时,所有工作都正常,也就是说,它在表“repositorios”上执行INSERT并在表“sesions_repositorios”上执行相应的INSERT。
当我获得特定用户的存储库列表时,我的问题出现了。这个代码就是。
class RepositoriosController extends AppController{
...
$r=$this->Repositorio->Sesion->find('all', array('conditions'=>array('user_id'=>$this->Session->read('Auth.User.id'))));
var_dump($r);
...
}
$ r变量不包含user_id的过滤数据,为什么?,我做错了什么?
我没有设置外键,这可能是问题吗?
感谢您的帮助。
答案 0 :(得分:0)
我认为您需要添加类似'recursive' => 1
或其他任何深度的内容,以便将链接的模型搜索到您的查询中。
$r=$this->Repositorio->Sesion->find('all', array('conditions'=>array('user_id'=>$this->Session->read('Auth.User.id')),'recursive'=>1));
答案 1 :(得分:0)
对不起,代码实际上非常正确。失败了其他问题。
感谢您的一切。
问候!