I have a model Animal in Yii which has two relations, mainRace and secondaryRace, both of them being an instance of the Race model.
How can this be translated into the relations array of Animal model aswell as in the Race model?
Looks like you'd need a many to many relationship, so you'll need one more table (animal_race).
See the Yii Relational page for more info on how to setup a MANY_MANY
Edit: if you're wanting to limit the animal to only two races, then you'd probably want to have a main_race_id / secondary_race_id in the Animal model (assuming an animal can only ever be in two races, what if the animal is entered in multiple events?).
And then you'd use a BELONGS_TO setup. That way you can easily find all the animals for a given race using a HAS_MANY relationship.
Related
I'm curious why the Eloquent relationship for hasMany has a different signature than for belongsToMany. Specifically the custom join table name-- for a system where a given Comment belongs to many Roles, and a given Role would have many Comments, I want to store the relationship in a table called my_custom_join_table and have the keys set up as comment_key and role_key.
return $this->belongsToMany('App\Role', 'my_custom_join_table', 'comment_key', 'role_key'); // works
But on the inverse, I can't define that custom table (at least the docs don't mention it):
return $this->hasMany('App\Comment', 'comment_key', 'role_key');
If I have a Role object that hasMany Comments, but I use a non-standard table name to store that relationship, why can I use this non-standard table going one way but not the other?
hasMany is used in a One To Many relationship while belongsToMany refers to a Many To Many relationship. They are both distinct relationship types and each require a different database structure - thus they take different parameters.
The key difference is that in a One To Many relationship, you only need the two database tables that correspond to the related models. This is because the reference to the relation is stored on the owned model's table itself. For instance, you might have a Country model and a City model. A Country has many cities. However, each City only exists in one country. Therefore, you would store that country on the City model itself (as country_id or something like that).
However, a Many To Many relationship requires a third database table, called a pivot table. The pivot table stores references to both the models and you can declare it as a second parameter in the relationship declaration. For example, imagine you have your City model and you also have a Car model. You want a relationship to show the types of cars people drive in each city. Well, in one city people will drive many different types of car. However, if you look at one car type you will also know that it can be driven in many different cities. Therefore it would be impossible to store a city_id or a car_id on either model because each would have more than one. Therefore, you put those references in the pivot table.
As a rule of thumb, if you use a belongsToMany relationship, it can only be paired with another belongsToMany relationship and means that you have a third pivot table. If you use a hasMany relationship, it can only be paired with a belongsTo relationship and no extra database tables are required.
In your example, you just need to make the inverse relation into a belongsToMany and add your custom table again, along with the foreign and local keys (reversing the order from the other model).
Try to understand with text and a figure.
One to One(hasOne) relationship:
A user has(can have) one profile. So, a profile belongs to one user.
One to many(hasMany):
A user has many(can have many) articles. So, many articles belong to one user.
Many to many(BelongsToMany):
A User can belong to many forums. So, a forum belongs to many users.
I was wondering if there is a smart way of having several common columns to different tables in CakePhP. I could use a common table having relationships to these tables, but is there a behavior or similar mechanism for which I can have:
Users
Customers
CommonPersonalFields
And have some common fields in the third table, fetched automatically by cake. In this way you could also have common views for those fields, included in the other tables views.
This is done with Model Associations in CakePHP. In this scenario, your models would be User, Customer, and PersonalData, and your associations are User hasOne PersonalData, Customer hasOne PersonalData, and if you want the association to be linked from both directions, PersonalData belongsTo User and PersonalData belongsTo Customer.
I am still trying to understand polymorphic associations in Doctrine2.
As I understand it, basic polymorphic associations work by using inheritance. If, for example, I had tables/classes OWNER, CAT and DOG, then the way to enable $owner->pet to point at either the CAT or the DOG table, would be to have them each extend a fourth class, PET, which is known as a mapped superclass. Then $owner->pet could return either a CAT or a DOG depending on what had been assigned, and Doctrine2 would be able to distinguish them.
That's simple enough. But what if I want to have two polymorphic associations which can point at the same object? For example, lets say that I have a table of ADMIRALS, each of which could command a FLEET or a PLANET. Let's also say that I have a table of SECTORS, each of which could contain a PLANET or a MOON.
Let's assume that I want $admiral->command to reference both PLANETS and FLEETS, and that I want $sector->contents to reference both PLANETS and MOONS. PLANET can't extend both command and contents as mapped superclasses. Is there a different way to make this work?
you can try ResolveTargetEntityListener see
you can point admiral's command property to an Interface, which is implemented by both PLANETS
and FLEETS
the same is with sector's contents
How can you update multiple models and their relations at the same time?
For example:
EditPost is a model with a editor() relation belongsTo User model.
Now lets say I have to update the editor in all the EditPost objects with original_post_id
EditPost::where('original_post_id',4)->get()
Possible Solutions
a. To do it by referring the user by ID instead of by the Model User
EditPost::where('original_post_id',4)->update(array('editor_id',3));
b. To do it by a foreach and saving each model
However
Neither of these appeals to me as they don't gel in general with the object concept of Eloquent or they would mean doing multiple updates instead of one.
I was wondering if Eloquent itself had a more elegant solution
You don't specify the other end of the association, but I assume you are looking for something like this?:
$user = User::find(3)
EditPost::where('original_post_id', 4)->editor()->associate($user)->save();
I'm creating a project management system which projects are assigned to users
What's the difference between creating a Model ProjectsUser and defining 2 $belongsTo relationship and defining HABTM relationships in both Project and User models? What would be the most correct way, though? And how do I save the data in the projects_users table?
From my experience, if you want to be able to save or delete rows only from the join table (the one with 2 IDs), then it is much more simple using three models associated through both a hasMany and a belongsTo association.
You can also retrieve data from the join table directly and do the queries you want much more easily
This is what CakePHP documentation says refering to HABTM and saving data:
However, in most cases it’s easier to make a model for the join table and setup hasMany, belongsTo associations as shown in example above instead of using HABTM association.
Here you can find more the full text:
http://book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated
I have used this method for a "reads" table (with post_id and user_id) as well as for subscriptions and similar kind of relationships.
The first way is called "hasAndBelongsToMany" [details here].
The second is called "hasMany through" [details here].
The second link relating to "hasMany through" has details and a lengthy explanation about when and why you would want to use it.
Not sure about the specifics of cakephp, but in general defining the relation model explicitly gives you more control over it, for instance if you wanted to do some validation or add callbacks on creation of this relationship.