E
E
Evgeny Bukharev2015-12-08 08:40:34
Yii
Evgeny Bukharev, 2015-12-08 08:40:34

Yii & Yii2. How do you manage related data?

Good afternoon, I have
a question regarding linked data.
Let's say we have a table with blog articles. There is also a table with tags. And an intermediate table for linking tags to articles.
When creating an article and attaching certain tags to it, everything is clear.
But what is the best way to work with changes to an already finished article and tag links?
Directly when changing, delete all links and re-register them, in accordance with the selected tags? Or check for the presence of existing links, remove unnecessary ones, and prescribe new ones?
How do you act in such a situation?
Thank you!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anton Natarov, 2015-12-08
@evgenybuckharev

The answer is at the very bottom, but I will describe how I do it, maybe they will also tell me in the comments.
There are usually two options:
1) You wo n't be using relays. Then just pull out the object and work with it.
2) You will use relays. Do join \ withJoin
In this respect, the doctrine in symphony is friendlier, the connection contains information, but does not contain an object, that is, something like an intermediate stage. As written above.
I make a save with a link:

$customer = Customer::findOne(123);
$order = new Order();
$order->subtotal = 100;
$order->link('customer', $customer);

In the above example, the method will set the customer_id attribute of the Order object to the value of the id attribute of the Customer object and then save it to the database. The same can be done with intermediate tables, the automaton link will insert rows into the intermediate table.
When deleting, you can use the unlink() method - it removes all links by setting object IDs to null, and if passed with the $delete = true attribute. It will delete the row on the link in the intermediate table.
Answer: Yes, delete and re-register.

A
Andrew, 2015-12-08
@R0dger

I don't know.. there is a similar functionality... in my transaction all connections are deleted and new ones are established. Why look for the difference if you still establish all the links .. maybe there is a difference if there are 100 of these tags and you make not for one request but for 100 .. then I’ll understand ...

V
Villarou, 2015-12-08
@VirmarY

"check for existing connections, delete unnecessary ones, and prescribe new ones" - you yourself answered

S
Sergey, 2015-12-08
Protko @Fesor

How do you act in such a situation?

I use Doctrine2 which handles stuff like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question