Is there a way in YII that I can replicate the "has many through" functionality of rails? Here is my schema:
CREATE TABLE invoices
(
id
int(11) unsigned NOT NULL AUTO_INCREMENT,
user_id
int(11) NOT NULL DEFAULT ‘0’,
number
int(11) NOT NULL DEFAULT ‘0’,
customer_id
int(11) DEFAULT ‘0’,
campaign_id
int(11) DEFAULT ‘0’,
po_number
varchar(255) DEFAULT ‘’,
created
date DEFAULT NULL,
terms
int(11) NOT NULL DEFAULT ‘0’,
due
date DEFAULT NULL,
subtotal
decimal(8,2) NOT NULL DEFAULT ‘0.00’,
discount_amount
decimal(8,2) NOT NULL DEFAULT ‘0.00’,
discount_type
enum(’$’,’%’) DEFAULT NULL,
discount
decimal(8,2) NOT NULL DEFAULT ‘0.00’,
tax
decimal(8,2) NOT NULL DEFAULT ‘0.00’,
total
decimal(8,2) NOT NULL DEFAULT ‘0.00’,
amount_paid
decimal(8,2) NOT NULL DEFAULT ‘0.00’,
is_paid
int(11) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (id
)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
CREATE TABLE applications
(
id
int(11) unsigned NOT NULL AUTO_INCREMENT,
payment_id
int(11) unsigned NOT NULL,
invoice_id
int(11) unsigned DEFAULT NULL,
amount
decimal(8,2) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
CREATE TABLE payments
(
id
int(11) unsigned NOT NULL AUTO_INCREMENT,
invoice_id
int(11) NOT NULL DEFAULT ‘0’,
method
enum(‘credit’,‘creditcard’,‘check’,‘cash’) DEFAULT NULL,
reference
varchar(255) DEFAULT NULL,
created
date DEFAULT NULL,
apply_credit
int(11) DEFAULT NULL,
amount
decimal(8,2) DEFAULT ‘0.00’,
PRIMARY KEY (id
)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
Specifically, I want to be able to apply multiple smaller payments to a single large invoice and apply one large payment to multiple smaller invoices. MANY_MANY would be fine, but I also want to be able to record the ‘amount’ applied to each invoice in the applications table. MANY_MANY doesn’t let you keep model information in the join table or if it’s there, the MANY_MANY relationship does not bring it back when you use an association. In Payments model:
public function relations() {
return array(
'invoices' => array(self::MANY_MANY, 'Invoice', 'applications(payment_id,invoice_id)'),
);
}
Any thoughts?