Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Problem with a one-to-many relationship

posted by ben 9 years ago

Hi!

First of all...thank you for this wonderful un-framework! :-) It's very usable for me.

I have a problem with a one-to-many relationship. I'm no sure if i just don't understand how it works or if it's another problem. I have 2 tables (simplified):

CREATE TABLE `basic_img_profiles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `description` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
CREATE TABLE `basic_images` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `fk_basic_images_basic_img_profiles_id` (`basic_img_profiles_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

ALTER TABLE `basic_images`
  ADD CONSTRAINT `fk_basic_images_basic_img_profiles_id` FOREIGN KEY (`basic_img_profiles_id`) REFERENCES `basic_img_profiles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

and i have the classes with the override-tag:

fORM::mapClassToTable('ImageProfile', 'basic_img_profiles');
class ImageProfile extends fActiveRecord {}

fORM::mapClassToTable('Image', 'basic_images');
class Image extends fActiveRecord {}

If i'm loading images over a profile, everything works fine:

$p = new ImageProfile (1);		
$p->buildImages ();

But if i try to associate an image to a profile, flourish says 'The table basic_img_profiles is not in a *-to-manyrelationship with the table basic_images':

$p = new ImageProfile (1);
$i = new Image ();
$i->setName ("BlaBla");
$p->associateImage ($img);

Is it wrong what i'm trying to do? I'm a little bit confused at the moment.

Thanks for your help! Ben

Well, it doesn't appear to me that your two tables are related to each other. You are setting the basic_images table to have a foreign key on the basic_img_profiles_id column to basic_img_profiles(id), but that the column basic_img_profiles_id does not exist in the basic_images table.

Also, your tables should be set up as UTF-8 instead of Latin-1, otherwise many different extended characters from your users will be converted into ?s. The UTF-8 page has some more details about why using UTF-8 is a good idea and how to make sure everything is using the same character encoding.

posted by wbond 9 years ago

I'm sorry, i just deleted too much from my table. Of course, the column exists. Here is the complete table:

CREATE TABLE `basic_images` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL DEFAULT '',
  `image_type` int(1) unsigned NOT NULL DEFAULT '0',
  `width` int(10) unsigned NOT NULL DEFAULT '0',
  `height` int(10) unsigned NOT NULL DEFAULT '0',
  `has_thumbnail` tinyint(1) NOT NULL DEFAULT '0',
  `basic_img_profiles_id` int(11) DEFAULT NULL,
  `tn_width` int(10) DEFAULT NULL,
  `tn_height` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_basic_images_basic_img_profiles_id` (`basic_img_profiles_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
ALTER TABLE `basic_images`
  ADD CONSTRAINT `fk_basic_images_basic_img_profiles_id` FOREIGN KEY (`basic_img_profiles_id`) REFERENCES `basic_img_profiles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Thanks for your hint with UTF-8, i will change it. But that can't be the solution for my problem, can it?

posted by ben 9 years ago

Sorry it took so long to get back to you - work has been crazy busy recently.

I think the issue is just that you are calling associateImage() and not associateImages(). Since more than one Images can have the same ImageProfile, you need to use the plural form.

Let me know if this doesn't fix it.

posted by wbond 9 years ago

Yeah, it works! What a small issue... :-)

Thank you for your help!

posted by anonymous 9 years ago