PetrZ
PetrZ

Reputation: 1

Symfony - One to Many with one active

I would like to ask what is the best approach for this in Symfony.

E.g. there is one entity User with the One to Many relation to another entity Address.
Let's say the User can have multiple delivery addresses in the e-shop, while exactly one is active as preset, with the possibility to choose other stored addresses. Or "the active address" is seen as the billing address.

Without Symfony, I could use e.g. stored procedure or the view with two possibilities.
Having another column/property of the User, holding ID of "the active address" record, or having another (e.g. boolean) column/property of the Address saying if the record is the active one. Then the view or stored procedure can easily return a single record containing the User data including "the active address". It would depend on other requirements, e.g. if it's needed to get the list of all active addresses, without the need of joining a User table/entity.

How this could be achieved in the Symphony the clean way according to best practices?

And "a less urgent", related question, by the way. Does Symfony / Doctrine support some preferred way to work with stored procedures? E.g. when complete business logic, including validations, access rights, etc. is implemented at the DB level and multiple clients (e.g. desktop app, web app, mobile app, ...) are using stored procedures instead of reimplementing business logic in each of them?

Upvotes: 0

Views: 185

Answers (1)

craigh
craigh

Reputation: 2291

Welcome to SO! "Best Practice" questions are frowned upon here. Usually you want to present some code that doesn't work and ask for help.

There are probably several solutions to your issue. If I were implementing a solution, I would probably add a boolean active property to the Address entity. Then in the User entity I would add a method like

public function getActiveAddress(): Address
{
    $criteria = Criteria::create()
        ->where(Criteria::expr()->eq("active", true))
    ;

    return $this->getAddresses()->matching($criteria);
}

Upvotes: 1

Related Questions