2011-05-24 62 views
0

使用CakePHP HABTM時出現問題。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

什麼是會話讀取聲明?我無法全部閱讀 – Leo 2011-05-24 22:03:24

回答

0

對不起,代碼實際上是非常正確的。由於其他問題而失敗。

謝謝你的一切。

問候!

0

我相信你需要添加諸如'recursive' => 1之類的東西,或者你想讓它搜索鏈接模型到你的查詢中的任何深度。

$r=$this->Repositorio->Sesion->find('all', array('conditions'=>array('user_id'=>$this->Session->read('Auth.User.id')),'recursive'=>1));