Yii Framework Forum: Generating Active Record Option From Existing Database? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Generating Active Record Option From Existing Database? Rate Topic: -----

#1 User is offline   Ryan Zec 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 15-January 10

Posted 15 January 2010 - 01:45 PM

Let say I have a database of 100 tables and I want to create active record classes for everyone of them, is there a way to do this?
0

#2 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 15 January 2010 - 02:40 PM

View PostRyan Zec, on 15 January 2010 - 01:45 PM, said:

Let say I have a database of 100 tables and I want to create active record classes for everyone of them, is there a way to do this?


Quote

If the class name ends with '*', then a model class will be generated
for EVERY table in the database.

php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#3 User is offline   Ryan Zec 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 15-January 10

Posted 15 January 2010 - 03:00 PM

Where is that quote from because out of context I have no idea what that means.
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 15 January 2010 - 03:12 PM

From your app directory type the command:
protected/yiic shell

You will see the following

Quote

Yii Interactive Tool v1.1 (based on Yii v1.1.0)
Please type 'help' for help. Type 'exit' to quit.
>> help
At the prompt, you may enter a PHP statement or one of the following commands:
- controller
- crud
- help
- model
- module

Type 'help <command-name>' for details about a command.

To expand the above command list, place your command class files
under 'protected/commands/shell', or a directory specified
by the 'YIIC_SHELL_COMMAND_PATH' environment variable. The command class
must extend from CConsoleCommand.


Type "help model"

Quote

>> help model
USAGE
model <class-name> [table-name]

DESCRIPTION
This command generates a model class with the specified class name.

PARAMETERS
* class-name: required, model class name. By default, the generated
model class file will be placed under the directory aliased as
'application.models'. To override this default, specify the class
name in terms of a path alias, e.g., 'application.somewhere.ClassName'.

If the model class belongs to a module, it should be specified
as 'ModuleID.models.ClassName'.

If the class name ends with '*', then a model class will be generated
for EVERY table in the database.

If the class name contains a regular expression deliminated by slashes,
then a model class will be generated for those tables whose name
matches the regular expression. If the regular expression contains
sub-patterns, the first sub-pattern will be used to generate the model
class name.

* table-name: optional, the associated database table name. If not given,
it is assumed to be the model class name.

Note, when the class name ends with '*', this parameter will be
ignored.

EXAMPLES
* Generates the Post model:
model Post

* Generates the Post model which is associated with table 'posts':
model Post posts

* Generates the Post model which should belong to module 'admin':
model admin.models.Post

* Generates a model class for every table in the current database:
model *

* Same as above, but the model class files should be generated
under 'protected/models2':
model application.models2.*

* Generates a model class for every table whose name is prefixed
with 'tbl_' in the current database. The model class will not
contain the table prefix.
model /^tbl_(.*)$/

* Same as above, but the model class files should be generated
under 'protected/models2':
model application.models2./^tbl_(.*)$/



/Tommy

This post has been edited by tri: 15 January 2010 - 03:17 PM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   Ryan Zec 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 15-January 10

Posted 15 January 2010 - 03:46 PM

Thanks, that makes a lot more sense.

One other question. I generally name my tables plural (since they hold multiple records) and name my objects singular (since they only hold 1 record). Is they a way to use some sort of configuration file (XML or whatever) where I can say tablename=blog_posts and classname=blog_post or do I just have to build each model one at a time using the model <class-name> [table-name] syntax?

Also, are there any database naming conventions that yii assumes? I generally have everything (including table name and field name) lowercase with underscores.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users