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

Help retrieving related record

posted by bret 9 years ago

I have a database setup as follows:

CREATE TABLE `images`( 
   `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, 
   `name` VARCHAR(20) NOT NULL, 
   `description` VARCHAR(255), 
   PRIMARY KEY (`id`)
 )  ENGINE=INNODB COMMENT='' ROW_FORMAT=DEFAULT;


CREATE TABLE `images_sizes` (
   `image_id` INT(11) UNSIGNED NOT NULL,
   `size` VARCHAR(7) NOT NULL,
   PRIMARY KEY  (`image_id`,`size`),
   CONSTRAINT `images_size_image_fk` FOREIGN KEY (`image_id`) REFERENCES `images` (`id`)
 )  ENGINE=INNODB COMMENT='' ROW_FORMAT=DEFAULT;

CREATE TABLE `advertisers`( 
   `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, 
   `name` VARCHAR(50) NOT NULL,
   `website` VARCHAR(255),
   `contact_name` VARCHAR(50) NOT NULL,
   `contact_phone` VARCHAR(50) NOT NULL,
   `contact_email` VARCHAR(50) NOT NULL, 
   PRIMARY KEY (`id`)
 )  ENGINE=INNODB COMMENT='' ROW_FORMAT=DEFAULT;

CREATE TABLE `ad_blocks` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `image_id` int(11) unsigned default NULL,
  `title` varchar(25) default NULL,
  `text` varchar(70) NOT NULL,
  `link_text` varchar(15) default NULL,
  `link_href` varchar(100) NOT NULL,
  `advertiser_id` int(11) unsigned NOT NULL,
  `is_active` tinyint(1) default '0',
  PRIMARY KEY  (`id`),
  KEY `image_id` (`image_id`),
  KEY `advertiser_id` (`advertiser_id`),
  CONSTRAINT `ad_block_advertiser_fk` FOREIGN KEY (`advertiser_id`) REFERENCES `advertisers` (`id`),
  CONSTRAINT `ad_block_image_fk` FOREIGN KEY (`image_id`) REFERENCES `images` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `ad_block_activities` (
  `ad_block_id` int(11) unsigned NOT NULL,
  `user_name` varchar(32) NOT NULL,
  `action` varchar(32) NOT NULL,
  `activity_ts` int(11) NOT NULL,
  PRIMARY KEY  (`ad_block_id`,`activity_ts`),
  CONSTRAINT `activity_ad_block_fk` FOREIGN KEY (`ad_block_id`) REFERENCES `ad_blocks` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

When working with an AdBlock object how can I retrieve the associated Image? The fActiveRecord::build method is returning an error (as it should in this case). Do I need to change my design or is there a method I'm not seeing?

As of right now I've just added a getImage method to the AdBlock class.

posted by bret 9 years ago

Okay, apparently this is documented in the Related Record Operations section of fActiveRecord http://flourishlib.com/docs/fActiveRecord#RelatedRecordsOperations

I need to do the following:

$adblock = new AdBlock(1);
$image = $adblock->createImage();

I kept glancing over that section since the method is "create" which seems odd to me. "get", "fetch" or "retrieve" would make more sense since the object already exists in the database. Anywho, just thought I'd clarify in case anyone else runs into the same issue.

posted by bret 9 years ago

The MethodNaming page has details about the conventions that are used in Flourish for the names of methods.

posted by wbond 9 years ago