Hi everyone,

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'=>1)));
var_dump($r);
...
}

The $r contains data but does not contain the information repository,
why?, what am I doing wrong?

This is the debug:

Array
(
    [0] => Array
        (
            [Sesion] => Array
                (
                    [id] => 3
                    [user_id] => 1
                    [sesions_estado_id] => 1
                    [asignatura_id] => 2
                    [name] => SesiĆ³n 1
                    [finicio] => 2011-05-23 00:00:00
                    [ffin] => 2011-05-27 00:00:00
                    [created] => 2011-05-22 16:01:48
                    [modified] => 2011-05-22 16:01:48
                )

            [SesionsEstado] => Array
                (
                    [id] => 1
                    [name] => No iniciada
                )

            [Asignatura] => Array
                (
                    [id] => 2
                    [name] => asdsada
                    [curso] => 1
                    [cuatrimestre] => 1
                    [created] => 2011-05-22 12:09:28
                    [modified] => 2011-05-22 12:09:28
                )

            [User] => Array
                (
                    [id] => 1
                    [users_tipo_id] => 2
                    [username] => marcos
                    [password] => c5e3539121c4944f2bbe097b425ee774
                    [created] => 2011-05-08 20:03:49
                    [modified] => 2011-05-08 20:03:52
                )

            [Repositorio] => Array
                (
                )

        )

)

I was able to show me the SQL:

SELECT `Sesion`.`id`, `Sesion`.`user_id`,
`Sesion`.`sesions_estado_id`, `Sesion`.`asignatura_id`,
`Sesion`.`name`,
`Sesion`.`finicio`, `Sesion`.`ffin`, `Sesion`.`created`,
`Sesion`.`modified`,
`SesionsEstado`.`id`, `SesionsEstado`.`name`,
`Asignatura`.`id`, `Asignatura`.`name`, `Asignatura`.`curso`,
`Asignatura`.`cuatrimestre`, `Asignatura`.`created`,
`Asignatura`.`modified`,
`User`.`id`, `User`.`users_tipo_id`, `User`.`username`,
`User`.`password`, `User`.`created`, `User`.`modified`
FROM `sesions` AS `Sesion`
LEFT JOIN `sesions_estados` AS `SesionsEstado` ON
(`Sesion`.`sesions_estado_id` = `SesionsEstado`.`id`)
LEFT JOIN `asignaturas` AS `Asignatura` ON (`Sesion`.`asignatura_id` =
`Asignatura`.`id`)
LEFT JOIN `users` AS `User` ON (`Sesion`.`user_id` = `User`.`id`)
WHERE `Sesion`.`user_id` = 1  ORDER BY `Sesion`.`ffin` ASC


Thanks for the help.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to