I am a little confused by some of your answers and I think I am not explaining myself very good. So I am going to try to explain again.
I have 3 tables opportunities, contacts, organizations ( I have more tables but these are the ones that apply to this problem).
here are the model realtions. I have removed the ones that don’t apply to this problem
model/opportunity.php
return array(
'organization' => array(self::BELONGS_TO, 'Organizations', 'organization_id'),
'contact' => array(self::BELONGS_TO, 'Contacts', 'contact_id', 'together'=>true),
...
model/organization.php
return array(
...
'primary' => array(self::BELONGS_TO, 'Contacts', 'primary_contact'),
);
model/contacts.php
return array(
'organization' => array(self::BELONGS_TO, 'Organizations', 'organization_id'),
'opp'=> array(self::HAS_MANY, 'Opportunities', 'contact_id'),
);
organization controller
$dataProvider = new CActiveDataProvider('Organizations');
$this->render('index',array('dataProvider'=>$dataProvider,));
organization view/index
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'id'=>'organizations-list',
'itemView'=>'_view',
'template'=>"{summary}{sorter}{pager}{items}\n{pager}",
)); ?>
organization view/_view
I am able to print the contacts first name by using the relation primary.
<div class="view">
<h3><?php echo CHtml::encode($data->name); ?></h3>
<p><?php echo CHtml::encode($data->mission_statement); ?></p>
<h4>Contact Info</h4>
[b]<?php echo $data->primary->first_name; echo ' '.$data->primary->last_name; ?>[/b]
- <?php echo CHtml::encode($data->phone); ?>
<br />
<?php echo CHtml::encode($data->primary->email); ?><br />
<?php echo CHtml::link(CHtml::encode('View'), array('view', 'id'=>$data->id)); ?>
Now when I try and do the exact same thing but with the opportunities_view I get the non-object error
<div class="view">
<h3><?php echo CHtml::encode($data->job_title); ?></h3>
<h4><?php echo CHtml::encode($data->organization->name); ?></h4>
<p><?php echo CHtml::encode($data->job_description); ?></p>
<p>Contact</p>
<p><?php echo CHtml::encode($data->contact->first_name);?></p>
I don’t understand how trying to access the contacts first_name field the exact same way I did for the organization _view will give me a accessing a non object error. To me they look pretty much the same so I am confused why one would work while the other doesn’t.
here are the tables
Opportunities Table
CREATE TABLE IF NOT EXISTS `opportunities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`organization_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (organization_id) REFERENCES organizations(id)',
`contact_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (contact_id) REFERENCES contacts(id)',
`contact_hours` varchar(200) NOT NULL,
`street_address` varchar(200) NOT NULL,
`city` varchar(100) NOT NULL,
`job_description` text NOT NULL,
`job_category_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (job_category_id) REFERENCES job_categories(id)',
`vehicle` tinyint(1) NOT NULL DEFAULT '0',
`background_check` tinyint(1) NOT NULL DEFAULT '0',
`wheelchair` tinyint(1) NOT NULL DEFAULT '0',
`transit` tinyint(1) NOT NULL DEFAULT '0',
`removal_date` date DEFAULT NULL,
`work_with_age_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (work_with_age_id) REFERENCES ages(id)',
`time_of_day_time_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (time_of_day_id) REFERENCES time_of_days(id)',
`age_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (age_id) REFERENCES ages(id)',
`post_length_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (post_length_id) REFERENCES post_lengths(id)',
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`job_title` varchar(255) NOT NULL,
`number_of_volunteers` int(11) NOT NULL,
`time_of_day_day_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (time_of_day_day_id) REFERENCES time_of_days(id)',
`transportation` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=106 ;
Contacts Table
CREATE TABLE IF NOT EXISTS `contacts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`organization_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (organization_id) REFERENCES organizations(id)',
`first_name` varchar(200) NOT NULL,
`last_name` varchar(200) NOT NULL,
`phone` char(12) NOT NULL,
`email` varchar(200) NOT NULL,
`primary_contact` tinyint(4) NOT NULL DEFAULT '0',
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=334 ;
Organization table
CREATE TABLE IF NOT EXISTS `organizations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (user_id) REFERENCES users(id)',
`primary_contact` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (primary_contact) REFERENCES contact(id)',
`name` varchar(255) NOT NULL,
`mission_statement` text NOT NULL,
`phone` char(12) NOT NULL,
`fax` char(12) DEFAULT NULL,
`street_address` varchar(200) NOT NULL,
`postal_code` char(7) NOT NULL,
`city` varchar(100) NOT NULL,
`province_id` int(11) NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (province_id) REFERENCES provinces(id)',
`approved` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`website` varchar(200) DEFAULT NULL,
`renew_date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=218 ;