Hello,

Here is a siplified representation of my data model:
*Photos
  -ID
  -etc

*Tags
  -ID
  -label
  -group_id

*Tags_Photos (table de jointure)
  -ID_Photo
  -ID_tag

*Groups
  -ID
  -label

In english:
* A Photo has N Tags (hasAndBelongsToMany with a joint table)
* A Tag belongs to a Tag group

Here is the code I use to retreive that list:

Voici le code que j'utilise pour récupérer cette liste:
$fields = array("Photo.id");
$contain["Tags"] = array(
      "fields" => array(
      "Tags.label",
      "Tags.id",
      "Tags.group_id",
    ),"Group"=>array("fields"=>array("label")));
$conditions= array("Photo.id=".$id);
$this->Photo->Behaviors->attach('Containable');
$queryArray = array(
            "contain"            =>    $contain,
            "fields"            =>    $fields,
            "conditions"        =>     $conditions
);
$tmp = $this->Photo->find("all",$queryArray);

With that query, Cake perform an additionnal query to retreive the name of
each group (and it's obviously possible to do it with a JOIN).

So...My question:
What I have to do to perform it in 1 single query?

Thanks,

Cedric

------------------------------------------
FYI, here are my models:

<?php
class Group extends AppModel{
    var $name = "Group";
    var $hasMany = array("Tags");
}
?>

<?php
class Tag extends AppModel{
    var $name = "Tag";
    var $hasAndBelongsToMany = array(
        'Photos' =>
            array(
            'className' => 'Photo',
            'joinTable' => 'tags_photos',
            'with' => 'TagsPhotos',
            'foreignKey' => 'tag_id',
            'associationForeignKey' => 'photo_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );
    var $belongsTo = array('Group');
}
?>

<?php
class Photo extends AppModel{
    var $name = "Photo";
    var $hasAndBelongsToMany = array(
        'Tags' =>
            array(
            'className' => 'Tag',
            'joinTable' => 'tags_photos',
            'with' => 'TagsPhotos',
            'foreignKey' => 'tag_id',
            'associationForeignKey' => 'photo_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );
?>

-- 
View this message in context: 
http://old.nabble.com/%22containable%22---query-numbers-tp27714138p27714138.html
Sent from the CakePHP mailing list archive at Nabble.com.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

Reply via email to