Do you have a database with foreign keys and just wish you could have something automatically create your ZF models from it? Well, today that was me. So as a little proof of concept, this is the code I came up with to do it for me...
But before we get to that, a few caveats:
- It's just a proof of concept
- The output needs updating for proper reference names, etc.
- Outputs everything to screen in one go and doesn't save the files.
However, it might be handy to someone, so I post it up for your comments.
PHP:
-
<?php
-
-
require_once 'Zend/Loader.php';
-
Zend_Loader::registerAutoload();
-
-
'host' => 'localhost',
-
'username' => 'myusername',
-
'password' => 'mypassword',
-
'dbname' => 'mydbname'
-
);
-
try {
-
$db = Zend_Db::factory('pdo_mysql', $config);
-
} catch (Zend_Db_Exception $e) {
-
die;
-
}
-
-
$model = <<<EOT
-
<?php
-
-
class %s extends Zend_Db_Table_Abstract
-
{
-
protected \$_name = '%s';
-
%s
-
}
-
EOT;
-
-
$refmap_outer = <<<EOT
-
%s
-
);
-
EOT;
-
-
$refmap_inner = <<<EOT
-
'refTableClass' => '%s',
-
)
-
EOT;
-
-
$toTable = new Zend_Filter_Inflector(
-
':tbl',
-
);
-
-
$toClass = new Zend_Filter_Inflector(
-
':tbl',
-
);
-
-
foreach ($db->listTables() as $table) {
-
$sql = "
-
select
-
tc.constraint_name,
-
kcu.table_name,
-
kcu.column_name,
-
kcu.referenced_table_name,
-
kcu.referenced_column_name
-
from
-
information_schema.table_constraints tc,
-
information_schema.key_column_usage kcu
-
where
-
tc.table_name = " . $db->quote($table) . "
-
and tc.constraint_type = 'FOREIGN KEY'
-
and kcu.constraint_name = tc.constraint_name
-
";
-
$keys = $db->fetchAll($sql);
-
-
$r = 0;
-
foreach ($keys as $key) {
-
'ref' . ++$r,
-
$key['column_name'],
-
$key['referenced_column_name']
-
);
-
}
-
}
-
);
-
}
So assuming I had a database structure like:
SQL:
-
--
-
-- Table structure for table `users`
-
--
-
CREATE TABLE `users` (
-
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
-
`added` datetime NOT NULL,
-
`username` varchar(32) NOT NULL,
-
`password` varchar(64) NOT NULL,
-
`name_first` varchar(32) NOT NULL,
-
`name_last` varchar(32) NOT NULL,
-
`name_nick` varchar(32) NOT NULL,
-
PRIMARY KEY (`id`),
-
KEY `username` (`username`)
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
-- --------------------------------------------------------
-
--
-
-- Table structure for table `user_tags`
-
--
-
CREATE TABLE `user_tags` (
-
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
-
`user_id` int(10) UNSIGNED NOT NULL,
-
`tag_user_id` int(10) UNSIGNED NOT NULL,
-
`tag` varchar(24) NOT NULL,
-
PRIMARY KEY (`id`),
-
KEY `tag` (`tag`),
-
KEY `fk_user_tags_user_id` (`user_id`),
-
KEY `fk_user_tags_tag_user_id` (`tag_user_id`)
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
--
-
-- Constraints for table `user_tags`
-
--
-
ALTER TABLE `user_tags`
-
ADD CONSTRAINT `fk_user_tags_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
-
ADD CONSTRAINT `fk_user_tags_tag_user_id` FOREIGN KEY (`tag_user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
it would output something like:
PHP:
-
<?php
-
-
class UserTags extends Zend_Db_Table_Abstract
-
{
-
protected $_name = 'user_tags';
-
'refTableClass' => 'Users',
-
),
-
'refTableClass' => 'Users',
-
)
-
);
-
}
-
-
<?php
-
-
class Users extends Zend_Db_Table_Abstract
-
{
-
protected $_name = 'users';
-
}




0 Responses to “Auto generating basic models for a Zend Framework app”
Leave a Reply
You must login to post a comment.