Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6797681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:38:19+00:00 2026-05-26T18:38:19+00:00

I have a somewhat complex pricing mechanism in my application- Here are some of

  • 0

I have a somewhat complex pricing mechanism in my application- Here are some of my business rules to set the stage (Entities are bolded):

  • A Product may have unique Price Points for a given Customer, Website, or Customer Group.
  • A Product can sometimes have one or more additional Options that may have their own Price Points or Price Rules.
  • A Product has one Unique Addition selected by the user, which is essentially a price and an integer.

Right now, I have an EntityRepository for Price Points to essentially determine the correct price point for the base product. The same goes for the Unique Addition and the Options.

PricePointRepository

public function getThePrice($Product, $qty, $Website, $Customer = null) 
{
    //all logic to get product price for this given instance goes here. Good.
}

Controller (simplified)

public function indexAction() 
{
    $Product = $em->dostuffwithpostdata;
    $qty = POST['qty']; //inb4insecure trolls
    $Website = $em->dostuff();
    $Customer = (if user is logged in, return their object with $em, otherwise null as it is a guest or public person); // No business logic here, just understanding the request.

    $price = $em->getRepository(PricePointRepository)->getThePrice($Product,$qty,Website,$Customer);

    $Options[] = $em->dostuffwithPOSTdata;
    $optionsPrice = 0;
    //Below is some logic directly related to pricing the product. 
    foreach($Options as $option) {
        if($option->hasRule()) {
            $optionsPrice += $ruleprice; //after some other stuff of course)
        } else {
            $optionsPrice += $em->getRepository(OptionPricePoints)->getPrice($option->getID(),$qty);
        }
    }

    $uniqueAdditionPrice = $em->stuff;

    $finalprice = $price + $optionsPrice + $uniqueAdditionPrice; //This is logic related to how I price this type of product!
    $unitprice = $finalprice / $qty;

    //twig stuff to render and show $finalprice, $unitprice, $uniqueAdditionPrice
}

That’s just for the product page. What happens when I get to the cart, saving the order, etc, when this logic needs to be reused. As you can see, I use Doctrine throughout to pull data based on my business logic in the repository classes.

I gladly welcome urdoingitwrong answers, because I really do think this is wrong. How do I go about fixing this? Something beautiful would be a service that essentially goes like this:

$pricer = getPricerService->Pricer($Entities,$postdata,$etc);
$unitPrice = $pricer->getUnitPrice();
$totalPrice = $pricer->getTotalPrice();
$optionsPrice = $pricer->getOptionsPrice();

But I have no idea how to go about doing that inside of Symfony/Doctrine, especially the way Doctrine and Repositories are accessed in Controllers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:38:19+00:00Added an answer on May 26, 2026 at 6:38 pm

    You’re correct that you should have all your re-usable business logic farmed off to a service so that different controllers can re-use the code.

    Have you checked out the “how to create a service” documentation:

    Service Container Documentation

    I’ll give you the speed run-down though.

    In config.yml you need to define your service:

    services:
        pricing_service:
            class: Acme\ProductBundle\Service\PricingService
            arguments: [@doctrine]
    

    Then you just need to make a bog standard PHP class to represent your service:

    namespace Acme\ProductBundle\Service;
    
    class PricingService {
    
        private $doctrine;        
    
        function __construct($doctrine) {
            $this->doctrine = $doctrine; // Note that this was injected using the arguments in the config.yml
        }
    
        // Now the rest of your functions go here such as "getUnitPrice" etc etc.
    }
    

    Lastly to get your service from a controller you just need to do:

    $pricingService = $this->get('pricing_service');

    There are other ways you can modularise the service such as not dumping all your services into config.yml but all of that is explained in the documentation. Also note that you can inject any other service you wish into your service so if you need stuff like arguments: [@doctrine, @security.context, @validator] you can do all that stuff or even: [@my_other_service].

    I suspect from your other question on injecting the EntityManager you may have already gleamed this was the way to go though!

    Hopefully this was still useful to you!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a somewhat complex data model in my iPad application (an OpenGL drawing
I have a Java main application with somewhat complex command line arguments. These arguments
I have a somewhat-long-running stored procedure being called from a PB application. I want
I have a somewhat complex custom user control, created in C# .NET, which is
I have an ObservableCollection of ChildViewModels with somewhat complex behaviour. When I go to
I have a PHP web app that generates a somewhat complex report from our
I have a somewhat complex query with multiple (nested) sub-queries, which I want to
I have a somewhat complex regular expression which I'm trying to match against a
I have a very basic question however the solution might be somewhat complex. How
Currently I have a service that downloads about 500 records of somewhat complex data,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.