質問

I'm working with a database with an entity of type Store. Each store has opening hours for each day of the week defined in an OpeningHour entity. A store can only have a maximum of 7 OpeningHour entities. This is fine since I can control that in the business logic.

However, the opening hours for a store may or may not be defined explicitly. This means that when I update the opening hours I first need to check if there are opening hours for that specific day already defined. If they exist I need to update otherwise I need to add a new OpeningHour entity to the ArrayCollection.

To get the opening hours for a store I do the following:

$openingHours = $store->getOpeningHours();

The $openingHours variable should now have a collection of not more than 7 entities (but might be less).

To update the opening hours I have a simple data structure that looks something like this:

$values = array(
    array(
        'day' => "Monday",
        'start' => "00:00:00",
        'end' => "00:00:00",
    )
);

The $values data structure can have up to 7 entries for each of the 7 days. My OpeningHour entity has the same properties as this data structure.

My question is, how can I easily check within the $openingHours ollection if a entity with a specific day name already exist?

役に立ちましたか?

解決

inside Store:

public function hasOpeningHoursOnDay($day) {
    return $this->openingHours->filter(function($key, $element) use ($day) {
        return $element->getDay() === $day;
    })->count() > 0;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top