a s Fri Mar 09 21:46:58 -0500 2012

Subject: association help

Im using CI 2.1 sparks AR 0.0.2 and php 5.3
this is my DB structure

here is a link for a picture of it: http://i.imgur.com/2AHMy.png

CREATE TABLE IF NOT EXISTS `communities` (
  `id` INT NOT NULL auto_increment,
  `name` VARCHAR(50) NOT NULL DEFAULT '',
  `description` TINYTEXT,
  `type` VARCHAR(100) NOT NULL DEFAULT '',
  `street` VARCHAR(100) NOT NULL DEFAULT '',
  `city` VARCHAR(100) NOT NULL DEFAULT '',
  `zip` MEDIUMINT(5) NOT NULL DEFAULT '0',
  `state` VARCHAR(2) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `properties` (
  `id` INT NOT NULL auto_increment,
  `community_id` INT NOT NULL,
  `common_charge` DECIMAL(10, 4) NOT NULL DEFAULT '0',
  `street` VARCHAR(100) NOT NULL DEFAULT '',
  `city` VARCHAR(100) NOT NULL DEFAULT '',
  `zip` MEDIUMINT(5) NOT NULL DEFAULT '0',
  `state` VARCHAR(2) NOT NULL DEFAULT '',
  `balance` DECIMAL(10, 2) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  FOREIGN KEY (`community_id`) REFERENCES `communities` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

CREATE TABLE IF NOT EXISTS `owners` (
  `id` INT NOT NULL auto_increment,
  `email` VARCHAR(50) NOT NULL UNIQUE DEFAULT '',
  `hashed_password` VARCHAR(128) NOT NULL,
  `first_name` VARCHAR(100) NOT NULL DEFAULT '',
  `last_name` VARCHAR(100) NOT NULL DEFAULT '',
  `recover` VARCHAR(100) UNIQUE,
  `altstreet` VARCHAR(100),
  `altcity` VARCHAR(100),
  `altzip` MEDIUMINT(5),
  `altstate` VARCHAR(2),
  `phone1` VARCHAR(25) NOT NULL DEFAULT '',
  `phone2` VARCHAR(25),
  `phone3` VARCHAR(25),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `whoowns` (
  `property_id` INT NOT NULL,
  `owner_id` INT NOT NULL,
  FOREIGN KEY (`property_id`) REFERENCES `properties` (`id`) ON DELETE CASCADE,
  FOREIGN KEY (`owner_id`) REFERENCES `owners` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

This is what I have as my class set ups but I think this is where im having a problems. I dont think they are step up correctly. Im hoping someone can look over them and tell me if anything needs to be changed or if it my test code that is step up incorrectly.

 1 class Community extends ActiveRecord\Model
 2 {
 3     static $has_many = array(
 4         array('properties')
 5     );
 6 }
 7 
 8 class Property extends ActiveRecord\Model
 9 {
10     static $has_one = array(
11         array('whoown'),
12         array('owner', 'through' => 'whoown')
13     );
14 
15     static $belongs_to = array(
16         array('community')
17     );
18 }
19 
20 class Whoown extends ActiveRecord\Model
21 {
22     static $has_one = array(
23         array('owner')
24     );
25 
26     static $belongs_to = array(
27         array('property')
28     );
29 }
30 
31 class Owner extends ActiveRecord\Model
32 {
33     static $belongs_to = array(
34         array('whoown')
35     );
36 }

This is the error im getting it appears after owner: the code is $property->owner->first_name
Im trying to get the name of the owner who owns the property. The code works but this error appears once at the top of the page

A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: lib/Relationship.php
Line Number: 641

This is what im using for testing right now

 1     //whats in my controller
 2     //gets the properties of community by community id
 3     function by_community($community = FALSE)
 4     {
 5         $this->view_data['community'] = Community::find_by_id($community);
 6         if(! $this->view_data['community'])
 7         {
 8             $this->session->set_flashdata('message', 'The requested community could not be found');
 9             redirect('employee');
10         }
11     }
12 
13 //whats in my view file
14 <h1>Properties of <?=$community->name?></h1>
15 
16 <?if($community->properties):?>
17     <ul>
18         <?foreach ($community->properties as $property):?>
19             <li>
20                 ID: <?=$property->id?><br/>
21                 Common Charge: <?=$property->common_charge?><br/>
22                 Community: <?=$community->name?><br/>
23                 Owner: <?=$property->owner->email?>
24             </li>
25         <?endforeach?>
26     </ul>
27 <?else:?>
28     This community has no porperties assigned to it.
29 <?endif?>
30     


a s Mon Mar 26 20:56:38 -0400 2012

found this issue
https://github.com/kla/php-activerecord/issues/164

at the bottom of the issue it was linked to here with an update
https://github.com/dfyx/php-activerecord/commit/57b5c08f2f5e05e95518e4c6bff139861ea817e7

So i guess that means its fixed? apparently 2 months ago. sparks AR was last updated 5 months ago so I guess that my problem? I added an primary_key column for now and it stops the error.

(1-1/1)