[eluser]Stolz[/eluser]
Hi,
I'm using Codeigniter 2.1.0 and I'm working on a fictitious project in order to learn DataMapper 1.8.21. I'm trying to deal with one relationship but I still don't really understand DataMapper enough and how should I create the model.
The fake projet is a simple orders system with orders, products and extras for products. These are the columns and relationships between them:
EXTRAS:
- Each extra must have a name and a price.
PRODUCTS:
- Each product must have a name and price.
- A product can can have many extras (not required).
ORDERS:
- Each oder must have a creation datetime.
- An order must have at least one product (they can be repeated).
- The current price of both the product and its extras must be stored in the relationship for allowing price changes in the future without affecting the old orders.
So far the tables look like this: (pseudo-sql for the sake of simplicity)
Code:
TABLE products (
id smallint(1) unsigned NOT NULL auto_increment PRIMARY KEY,
name varchar(128) NOT NULL UNIQUE,
price float NOT NULL
)
TABLE extras (
id smallint(1) unsigned NOT NULL auto_increment PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE,
price float NOT NULL
)
TABLE extras_products (
id int(3) unsigned NOT NULL auto_increment PRIMARY KEY,
product_id smallint(1) unsigned NULL REFERENCES products(id) ON UPDATE CASCADE ON DELETE CASCADE,
extra_id smallint(1) unsigned NULL REFERENCES extras(id) ON UPDATE CASCADE ON DELETE CASCADE,
UNIQUE(product_id,extra_id)
)
TABLE orders (
id int(4) unsigned NOT NULL auto_increment PRIMARY KEY,
created datetime NOT NULL,
)
TABLE orders_products (
id int(3) unsigned NOT NULL auto_increment PRIMARY KEY,
product_id smallint(1) unsigned NULL REFERENCES products(id) ON UPDATE CASCADE ON DELETE RESTRICT,
order_id int(4) unsigned NULL REFERENCES orders(id) ON UPDATE CASCADE ON DELETE CASCADE,
qty tinyint unsigned NOT NULL,
price float NOT NULL
)
TABLE foo (
id int(3) unsigned NOT NULL auto_increment PRIMARY KEY,
orders_products_id int(3) unsigned REFERENCES orders_products(id) ON UPDATE CASCADE ON DELETE CASCADE,
extra_id smallint(1) unsigned REFERENCES extras(id) ON UPDATE CASCADE ON DELETE RESTRICT,
UNIQUE(orders_products_id,extra_id),
price float NOT NULL
)
And the models look like this:
Code:
class Product extends DataMapper {
public $has_many = array('extra','order');
public $validation = array(
'name' => array(
'label' => 'Name',
'rules' => array('trim','required','alpha','max_length'=>128,'unique')
),
'price' => array(
'label' => 'Price',
'rules' => array('trim','required','numeric','abs')
)
);
}
Code:
class Extra extends DataMapper {
public $has_many = array('product');
public $validation = array(
'name' => array(
'label' => 'Name',
'rules' => array('trim','required','alpha','max_length'=>64,'unique')
),
'price' => array(
'label' => 'Price',
'rules' => array('trim','required','numeric','abs')
),
);
}
Code:
class Order extends DataMapper {
public $has_many = array('product');
public $validation = array(
//Relationships validation
//Order must have at least one product
'product' => array(
'label' => 'Product',
'rules' => array('required','min_size' => 1)
),
);
}
The problem is I don't know the correct name for the table "foo" and how to define that relationship in the Order model.
Can anyone please guide me to the right direction?
Thanks.