I am going to try to do my best to explain.  I  have three tables,
items and tags and items_tags.  Many items can belong to many tags,
and many tags can belong to many items.    When I add an Item via the
add() function in the items_controller.php below, I get the following
query (sorry for the lack of formatting):

1       SELECT COUNT(*) AS `count` FROM `tags` AS `Tag` WHERE 1 = 1
2       INSERT INTO `items`
(`title`,`picture`,`note`,`url`,`family`,`style`,`designer`) VALUES
('Title50','','','','','','')
3       SELECT LAST_INSERT_ID() AS insertID             1       1       0
4       SELECT `ItemsTag`.`id` FROM `items_tags` AS `ItemsTag` WHERE
`ItemsTag`.`item_id` = 47
5       INSERT INTO `items_tags` (`item_id`,`tag_id`) VALUES (47,'8'),
(47,'tag50')

There are two issues.  One resides in line 5 where two values are
INSERTed into the items_tags table VALUES (47,'8'), (47,'tag50'),  the
first value set is correct, the second value set (47 and tag 50) is
not.
The second issue is that no information is inserted into the 'tags'
table. (tag.id and tag.tag )

Here are my two models:

------------------------------------------------
tag.php
------------------------------------------------
<?php
    class Tag extends AppModel {
        var $name = 'Tag';
        var $hasAndBelongsToMany = array('Item');
}

?>

------------------------------------------------
item.php
------------------------------------------------
<?php
    class Item extends AppModel {
        var $name = 'Item';
        var $hasAndBelongsToMany = array('Tag');
}

?>


HERE ARE MY CONTROLLERS
------------------------------------------------
items_controller.php
------------------------------------------------
<?php
class ItemsController extends AppController {

    var $name = 'Items';
    var $helpers = array('Html');
    var $uses = array ('Tag', 'Item');
    var $components = array('Auth', 'Session');

    function index(){
    }

    function beforeFilter() {
     $this->_checkSession();
     $user_id = $this->getId();
     $this->set('user_id', $user_id);

     $tag_id = $this->Tag->find('count') + 1;
     $this->set('tag_id', $tag_id);

        if($user_id){
            $this->layout = 'loggedin';
        } else {
            $this->layout = 'guest';
        }
            $this->Auth->allow('index');
     } //before filter function

    function add(){
            if(!empty($this->data)) {
                $this->Item->save($this->data);
                $this->flash('Your registration information was
accepted.', '/items');

        }

    } //add function

}
?>

------------------------------------------------
tags_controller.php (pretty much blank)
------------------------------------------------
<?php
class TagsController extends AppController {

    var $name = 'Tags';
    var $helpers = array('Html');
    var $components = array('Auth', 'Session');


     function index(){
        $this->set('tag', $this->Tag->findAll());
     }


     function beforeFilter() {

     }
}

?>


------------------------------------------------
add.ctp  This is for the add function for the items_controller.php
------------------------------------------------
<?php echo $form->create('Item', array('action' => 'add')); ?>

 <?php
        echo $form->hidden('Tag.id', array('value' => $tag_id));
        echo $form->input('Item.title', array('label' => 'Item
Title'));  //text
        echo $form->input('Item.picture');   //text
        echo $form->input('Item.note');   //text
        echo $form->input('Item.url');   //text
        echo $form->input('Item.family');   //text
        echo $form->input('Item.style');   //text
        echo $form->input('Item.designer');   //text
        echo $form->input('Tag.tag', array ('label' => 'Add Tags
Here'));

    ?>
  <?php echo $form->end('add'); ?>


Thanks for any help.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to