Ooops. Wrong button. Sorry! I wondered why the message didn't appear!

SQL:

CREATE TABLE `images` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `file_name` tinytext NOT NULL,
  `created` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Stored reference to
file system images' AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `images_tags`
--

CREATE TABLE `images_tags` (
  `image_id` int(10) unsigned NOT NULL,
  `tag_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`image_id`,`tag_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Link Table';

-- --------------------------------------------------------

--
-- Table structure for table `tags`
--

CREATE TABLE `tags` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `tag` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='Stores tags relating
to images in the images table' AUTO_INCREMENT=1 ;


CONTROLLER:
class ImagesController extends AppController
{
    var $layout = 'blank';

    var $helpers = array('cache', 'javascript');

    var $uses = array('Image', 'Tag');

    function generate_browser()
    {
        print_r($this->Image->findAll(););
    }

    function upload()
    {

        $image = $this->data['Image']['gallery_browse_file'];

        // Check for a file
        if (empty($image['name']))
        {
            $this->set('status',  'Upload Failed: No file specified');
            $this->render();
            return;
        }

        // What directory should we be in?
        $dir = date('Y/m');
        chdir('img/uploads');

        // If directory doesn't exist then make it
        if (!file_exists($dir))
        {
            $this->set('status', 'Making directory');
            $this->render();
            mkdir($dir, 0755, true);
        }

        // If file exists, stop and give instructions
        if (file_exists($dir . DIRECTORY_SEPARATOR . $image['name']))
        {
            $this->set('status', 'Upload Failed: Filename exists. Try
renaming then upload again.');
            $this->render();
            return;
        }

        // So far, so good. Now try to move it
        if (move_uploaded_file($image['tmp_name'], $dir . '/' .
$image['name']))
        {
            $this->set('status', 'Saving '. $image['name']);
            $this->render();

            // Finally insert a reference in the database, along with
the tags
            $this->data['Image']['file_name'] = $image['name'];
            $this->data['Tag']['Tag'] = explode(',',
trim(strtoupper($this->data['Image']['tag'])));

            // Check if it is a new tag, otherwise just use the
existing ID
            foreach ($this->data['Tag']['Tag'] as $item)
            {
                if ($id = $this->Tag->field('id', "tag='$item'"))
                {
                    $tag_id[] = $id;
                }
                else
                {
                    if($item)
                    {
                        $this->Tag->create();
                        $this->Tag->save(array('Tag' => array('tag' =>
$item)));
                        $tag_id[] = $this->Tag->getLastInsertId();
                    }
                }
            }

            // Fill ModelName/ModelName with an array of tag id's.
            // The HABTM will then auto-populate the join-table
            if (isset($tag_id))
            {
                $this->data['Tag']['Tag'] = $tag_id;
            }

            // Save the Image
            $this->Image->save($this->data);

            $this->set('last_image_id', $this->Image-
>getLastInsertId());

            $this->set('status',  $image['name'] . ' Uploaded
Successfully');
            $this->render();

            return;
        }

        $this->set('status',  'Upload Failed');
        $this->render();
    }
}

MODELS:
class Tag extends AppModel {

                var $name = 'Tag';

                var $table = 'tags';

}
class Image extends AppModel {

        var $name = 'Image';

        var $table = 'images';

        var $hasAndBelongsToMany = array( 'Tag' );
}


The generate_browser() function in the controller is where things are
falling over.

The goal is simply for an uploaded image to have many tags associated
with it, which the user can enter as comma separated list in a
textbox. In the future, I want to be able to search for a Tag and get
a list of Images with that Tag associated to them. I also just wish to
be able to get a list of Images (with or without associated Tag data).

(All the $this->render() is a bit of a dirty hack to update the status
in the Upload box via JS.)

Cheers for all the help. I really like CakePHP but there is a bit of a
learning curve!
Dave

On Sep 14, 4:32 pm, "Jon Bennett" <[EMAIL PROTECTED]> wrote:
> Hi Dave,
>
> > Yup :) Each Tag is unique. Uploads are working great now.
>
> If each Tag is unique, then I think a HABTM association is incorrect,
> as a HABTM is for linking 2 different models that are not unique to
> each other. You should be using either Image hasOne or Image hasMany
> Tag
>
> Could you paste your models, controller, and SQL so we can investigate 
> further.
>
> cheers,
>
> jon
> ps: let's keep onlist eh ;)
>
> --
>
> jon bennett -http://www.jben.net/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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