# table of units of products DROP TABLE IF EXISTS `units`; CREATE TABLE `units` ( `unit_id` INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, `unit_name` VARCHAR (256) NOT NULL DEFAULT '', PRIMARY KEY (`unit_id`) ) ENGINE = MYISAM AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8; INSERT INTO `units` SET` unit_name` = 'gram'; INSERT INTO `units` SET` unit_name` = 'milliliter'; # product groups table DROP TABLE IF EXISTS `groups`; CREATE TABLE `groups` ( `group_id` INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, `group_name` VARCHAR (256) NOT NULL DEFAULT '', PRIMARY KEY (`group_id`) ) ENGINE = MYISAM AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8; INSERT INTO `groups` SET` group_name` = 'Vegetables'; INSERT INTO `groups` SET` group_name` = 'Fruits'; INSERT INTO `groups` SET` group_name` = 'Dairy'; INSERT INTO `groups` SET` group_name` = 'Meat'; # product table DROP TABLE IF EXISTS `products`; CREATE TABLE `products` ( `product_id` INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, `group_id` INT (10) DEFAULT NULL, `unit_id` INT (10) DEFAULT NULL, `product_name` VARCHAR (256) NOT NULL DEFAULT ', PRIMARY KEY (`product_id`) ) ENGINE = MYISAM DEFAULT CHARSET = utf8; # Vegetables INSERT INTO `products` (` group_id`, `unit_id`,` product_name`) VALUES (1, 1, 'Potatoes'); INSERT INTO `products` (` group_id`, `unit_id`,` product_name`) VALUES (1, 1, 'Tomato'); # Fruits INSERT INTO `products` (` group_id`, `unit_id`,` product_name`) VALUES (2, 1, 'Apricot'); INSERT INTO `products` (` group_id`, `unit_id`,` product_name`) VALUES (2, 1, 'Apple'); # Dairy INSERT INTO `products` (` group_id`, `unit_id`,` product_name`) VALUES (3, 1, 'Cheese'); INSERT INTO `products` (` group_id`, `unit_id`,` product_name`) VALUES (3, 2, 'Milk'); # Meat INSERT INTO `products` (` group_id`, `unit_id`,` product_name`) VALUES (4, 1, 'Veal'); INSERT INTO `products` (` group_id`, `unit_id`,` product_name`) VALUES (4, 1, 'Pork');
class Products extends Zend_Db_Table_Abstract
{
protected $ _name = 'products';
protected $ _primary = array ('product_id');
}
class Units extends Zend_Db_Table_Abstract
{
protected $ _name = 'units';
protected $ _primary = array ('unit_id');
}
class Groups extends Zend_Db_Table_Abstract
{
protected $ _name = 'groups';
protected $ _primary = array ('group_id');
}
$ productsTable = new Products;
$ productsRowset = $ productsTable-> fetchAll ();
foreach ($ productsRowset as $ row) {
echo '<pre>'. print_r ($ row-> toArray (), true). '</ pre>'. PHP_EOL;
}
if ($ row-> product_name == 'Pork') {
$ row-> product_name = 'Pork Kebab';
$ row-> save ();
}
protected $ _dependentTables = array ('Products');
protected $ _referenceMap = array (
'refUnits' => array (
self :: COLUMNS => 'unit_id',
self :: REF_TABLE_CLASS => 'Units',
self :: REF_COLUMNS => 'unit_id',
self :: ON_DELETE => self :: CASCADE,
self :: ON_UPDATE => self :: CASCADE
),
'refGroups' => array (
self :: COLUMNS => 'group_id',
self :: REF_TABLE_CLASS => 'Groups',
self :: REF_COLUMNS => 'group_id',
self :: ON_DELETE => self :: CASCADE,
self :: ON_UPDATE => self :: CASCADE
)
);
class Products extends Zend_Db_Table_Abstract
{
protected $ _name = 'products';
protected $ _primary = array ('product_id');
protected $ _referenceMap = array (
'refUnits' => array (
self :: COLUMNS => 'unit_id',
self :: REF_TABLE_CLASS => 'Units',
self :: REF_COLUMNS => 'unit_id',
self :: ON_DELETE => self :: CASCADE,
self :: ON_UPDATE => self :: CASCADE
),
'refGroups' => array (
self :: COLUMNS => 'group_id',
self :: REF_TABLE_CLASS => 'Groups',
self :: REF_COLUMNS => 'group_id',
self :: ON_DELETE => self :: CASCADE,
self :: ON_UPDATE => self :: CASCADE
)
);
}
class Units extends Zend_Db_Table_Abstract
{
protected $ _name = 'units';
protected $ _primary = array ('unit_id');
protected $ _dependentTables = array ('Products');
}
class Groups extends Zend_Db_Table_Abstract
{
protected $ _name = 'groups';
protected $ _primary = array ('group_id');
protected $ _dependentTables = array ('Products');
}
$ productsTable = new Products;
$ productsRowset = $ productsTable-> fetchAll ();
foreach ($ productsRowset as $ row) {
echo '-----------------------'. PHP_EOL;
echo '<pre>'. print_r ($ row-> toArray (), true). '</ pre>'. PHP_EOL;
echo '<pre>'. print_r ($ row-> findParentRow ('Units') -> toArray (), true). '</ pre>'. PHP_EOL;
echo '<pre>'. print_r ($ row-> findParentRow ('Groups') -> toArray (), true). '</ pre>'. PHP_EOL;
echo '-----------------------'. PHP_EOL;
}
$ groupsRowset = $ groupsTable-> fetchAll ();
foreach ($ groupsRowset as $ row) {
echo '-----------------------'. PHP_EOL;
echo '<pre>'. print_r ($ row-> toArray (), true). '</ pre>'. PHP_EOL;
echo '<pre>'. print_r ($ row-> findDependentRowset ('Products') -> toArray (), true). '</ pre>'. PHP_EOL;
echo '-----------------------'. PHP_EOL;
}
Source: https://habr.com/ru/post/127271/
All Articles