CakePHP HABTM 模型问题

发布于 2024-11-09 15:26:12 字数 2177 浏览 7 评论 0原文

我在使用 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,并在表“sessions_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 的过滤数据,为什么?我做错了什么?

我没有设置外键,这可能是问题吗

I have a problem when using CakePHP HABTM.

I have the following models.

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');
}

And the following database tables.

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)
);

When I save the data in a repository all work properly, that is, it performs an INSERT on the table "repositorios" and performs the corresponding INSERT on table "sesions_repositorios.

My problem comes when I get a list of repositories for a particular user. The code for this would be.

class RepositoriosController extends AppController{
...
$r=$this->Repositorio->Sesion->find('all', array('conditions'=>array('user_id'=>$this->Session->read('Auth.User.id'))));
var_dump($r);
...
}

The $r variable does not contain the filtered data for user_id, why?, what am I doing wrong?

I have not set foreign key's, could that be the problem?

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2024-11-16 15:26:12

我相信您需要添加类似 'recursive' =>; 1 或您希望它在查询中搜索链接模型的任何深度。

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

I believe that you need to add in something like 'recursive' => 1 or whatever depth you want it to search your linked models into your query.

$r=$this->Repositorio->Sesion->find('all', array('conditions'=>array('user_id'=>$this->Session->read('Auth.User.id')),'recursive'=>1));
面犯桃花 2024-11-16 15:26:12

抱歉,代码实际上是完全正确的。因其他问题而失败。

感谢一切。

问候!

I'm sorry, the code is actually quite correct. Was failing by other issues.

Thanks for everything.

Greetings!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文