Reputation: 539
I am developing an application in the CakePHP3 framework and many times (perhaps philosophically speaking) I have naming questions about the ORM convention that leaves me with some doubt.
Suppose the following tables:
A 'user' can have multiple 'coupons', but one coupon belongs exclusively to one user. From a users standpoint, it's a hasMany relationship, and from a Coupon standpoint, it's a belongsTo relationship. Well, in CakePHP3, relationships are treated with the plural table name. So in Users, we have a $this->hasMany("Coupons") relationship, but is it correct to use $this->belongsTo("Users")? (I mean the "Users" inside belongsTo()). If I am referencing A Coupon of a User, wouldn't it be $ this-> belongsTo ("User")?
Of course i can change the name of the relationship and specify the className relationship, but it is correct to do that with his naming convention?
Upvotes: 0
Views: 53
Reputation: 5098
A table is plural, because it holds lots of records. So the table names are Coupons and Users. And when associating them, it's the tables that you're associating: the Coupons table is associated with the Users table, not a Coupon record associated with a User record.
The type of association defines the relation between the entities. The association here is of the "belongsTo" variety, meaning that the Coupon record (CakePHP entity) "belongs to" the User record, but they're not "associated".
One record from the Users table is a User, so when the Coupons -> Users association is used, you get a singular property name, but that record still came from a big list of Users.
Hope that helps to make the naming scheme a little more intuitive for you. :-)
Upvotes: 1