What is the right plural/singular table naming convention in CakePHP.
This:
posts
posts_pictures
posts_picture_captions <-- Not sure about this
Or this:
posts
posts_pictures
posts_pictures_captions <-- Not sure about this
I know CakePHP want the tables in alphabetical order but this should in this question ignored. Please only consider plural/singular table naming.
Cake Inflector Should do exactly what you are looking to do.
From the cakephp book:
Model class names are singular and CamelCased. Person, BigPerson, and
ReallyBigPerson are all examples of conventional model names.
Table names corresponding to CakePHP models are plural and
underscored. The underlying tables for the above mentioned models
would be people, big_people, and really_big_people, respectively.
You can use the utility library Inflector to check the singular/plural
of words. See the Inflector for more information.
Field names with two or more words are underscored: first_name.
Foreign keys in hasMany, belongsTo or hasOne relationships are
recognized by default as the (singular) name of the related table
followed by _id. So if a Baker hasMany Cake, the cakes table will
refer to the bakers table via a baker_id foreign key. For a table like
category_types whose name contains multiple words, the foreign key
would be category_type_id.
See http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions
So at first, your "posts_pictures" table should be named "post_pictures".
As a result, neither of the two is correct. The correct name according to the cake naming conventions is "post_picture_captions".
Related
I have two tables many to many, so i used mapping table for it
1st table: course that contains course_id, course_name, and credit_hrs attributes
2nd table: y_term that contains yt_id, year, and term attributes
mapping table is y_t_course that contains yt_id and course_id attributes
then i create a table, its name is topic, it is contains cpt_id, topic_text.
NOW i used y_t_course as a reference to topic table, it means has foreign key from y_t_course.
my question is how can i represent this relation in ER-Diagram
I would use a tool like Visual Paradigm to get the ER diagrams required. Eclipse also has a plugin that does this.
How i do a crud with a relationship between a user and services order? With the code, i can't add users in a team for service order. When cakebake, the controller TeamHasUsersController is created, but the form add don't have any field.
The crud has generated with cake bake.
In service_orders: change requester_users_id into user_id, the icon in front of the field will change to a red key (foreign key). Cake conventions need a foreignkey to be named [model]_id.
The name of table with associations should be "team_users".
Three database tables are required for a BelongsToMany association. In the example above we would need tables for articles, tags and articles_tags. The articles_tags table contains the data that links tags and articles together. The joining table is named after the two tables involved, separated with an underscore by convention. In its simplest form, this table consists of article_id and tag_id.
habtm relationship
I did a little research on tag clouds, and I ended up choosing a schema similar to the Wordpress one seen in this page: http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
Currently, the tables that I have created is: Posts, PostsTagMaps, PostsTags
1) Would I need to create a table for PostsTagMaps even if I don't plan on using a controller?
2) A Post hasMany PostTagMaps. I'm not sure where should I be defining this relationship. I think it should be the Post model, but then I would have to join the PostsTags table to PostTagsMaps then join it to Posts, so I wanted to ask for some advice.
The easy solution is to follow Cake conventions (which will look very similar to the solution you linked to). You'll have three tables:
posts, tags, posts_tags
Then your Post model HABTM Tag model. The join table will automatically be used by Cake to save and retrieve the information. Check the book for more information on setting up your schema.
Cake is flexible enough where you can do it however you want, but if it's a basic sort of posts-tags relationship, the convention method is the way to go.
If you want to use a custom model/table like PostsTagMaps, then do this by using the 'with' key in your HABTM relationship definition. The 'with' key tells Cake to use a specific model (and therefore a specific table) instead of an automatically generated version. In this case, your tag model sounds like it's PostsTags and your HABTM table is PostsTagMaps, so the 'with' key on Post HABTM PostsTag would be PostsTagMap.
I have a table familiars , a table mages and a table mages_familiars where I keep which familiar belongs to which Mage.
How can I model this cross reference table?
Thanks in advance
You don't need the intermediary table unless it's possible that a familiar can belong to multiple mages and mages can have multiple familiars (a HABTM relationship). In that case the table should have fields id (int), created (datetime), modified (datetime), mage_id (int) and familiar_id (int). Also, the table should be named familiars_mages.
If a familiar can belong to only one mage, a familiar belongsTo a mage and a mage hasMany familiars. The familiars table should have a mage_id field.
If a familiar can belong to only one mage, and a mage can have only one familiar, you can use a hasOne relationship. In that case either the mages table has a familiar_id field or the familiars table has a mage_id field.
I am learning the Model relationship types in cakephp.
I have built two tables and in one of the Table A,
I got these fields in it:
Table A {postID, topic, content}
Table B {replyID, content, postID}
And when I ran the web page, a bunch of error related to SQL popped up saying that
cakephp couldn't find post_id.
It is weird that I have already declared
the $primaryKey to be using postID in the tableA.php under Models folder,
but cakephp seemed want me to change the ID field to post_id instead of postID,
because the error disappeared after I have changed the primaryKey to post_id.
ANy ideas?
Cake expects your fields to be lower case, with words separated by underscores. See the CakePHP Model and Database Naming Conventions for more information.
By convention the field with the primary key is named id.