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 8289997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:39:59+00:00 2026-06-08T12:39:59+00:00

I struggle organising my code, and I would like to share my problem with

  • 0

I struggle organising my code, and I would like to share my “problem” with you by using a simple example: the calculation of the area of a rectangle. I put the code for example, but reading the first intro on each class section explains the situation easily.

Entity Rectangle:

The entity Rectangle contains two inportant properties $length and $width.

// src/Acme/CalculationBundle/Entity/Rectangle.php

/**
 * @ORM\Entity(repositoryClass="Acme\CalculationBundle\Repository\RectangleRepository")
 * @ORM\Table(name="rectangle")
 */
class Rectangle
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $length;

    /**
     * @ORM\Column(type="integer")
     */
    protected $width;

FORM

Of course, the user can set length and width via a form

CONTROLLER

CreateRectangleAction: renders the form on GET request and work on the data on a POST request.

ViewRectangleAction: shows the rectangle.

RECTANGLE MANAGER

Now, to make sure the controller doesn’t do too much stuff, I use a RectangleManager to realise common operation on Rectangle objects and use it as a service (injecting the appropriate elements).

// src/Acme/CalculationBundle/Entity/RectangleManager.php
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Acme\EmployerBundle\Entity\Manager;

class rectangleManager
{
    /**
     * Doctrine entity manager
     * @var EntityManager
     */
    protected $em;

    /**
     * @var EntityRepository
     */
    protected $repository;

    /**
     * 
     * @var string
     */
    protected $class;

    public function __construct(EntityManager $em, $class)
    {
        $this->em = $em;
        $this->class = $class;
        $this->repository = $em->getRepository($class);
    }

    /**
     * @param $id
     * @return Rectangle
     */
    public function find($id)
    {
        $rectangle =  $this->repository->find($id);
    }

THE PROBLEM: WHAT IF?

What if I need to do some calculations on the rectangle? For example, if I need to add an area property so that I can render the area directly in the template without doing the calculation (length*width) in the template?

Not knowing how to do this properly, I went for this pretty bad solution:

I created a RectangleDisplay class (where I inject the rectangle entity) and display that entity instead of the Rectangle entity when calling ViewRectangleAction in the controller.

// src/Acme/CalculationBundle/Entity/
class RectangleDisplay
{
    /**
     * @var Rectangle $rectangle
     */
    protected $rectangle;

    /**
     * @var Integer 
     */
    protected $area;

    /**
     * @param Rectangle $rectangle
     */
    public function __construct(rectangle $rectangle){

        $this->rectangle = $rectangle;
        //calculate are
        $area = this->calculateArea($this->rectangle->getLength(),$this->rectangle->getWidth());
        $this->area = $area;

    }

    /**
     * @return Integer $area
     */
    public function calculateArea($length,$width)
    {
         return $length * $width;
    }

Now the property area is directly accessible in the template. In the case of a rectangle, that is fine, but in the case of a more complex element (cone, …), I might want to use a service that is crazy at doing calculation. I am not gonna inject the container in my RectangleDisplayEntity, am I?

  • 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-06-08T12:40:00+00:00Added an answer on June 8, 2026 at 12:40 pm

    This belongs in your entity.

    class Rectangle
    {
        // ...
    
        public function getArea()
        {
            return $this->length * $this->width;
        }
    }
    

    The area of a rectangle is a property of a rectangle. It’s not some crazy calculation that should be delegated to another service, and it’s especially not something display-related (though you may think that based on it being displayed in your application).

    Now, if you were calculating something a lot more complex, it’s probably worth moving to a service / another class. (Services = verbs, Entities = nouns and their properties).

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

Sidebar

Related Questions

Last two nights I am struggle with below code. The problem is I need
It's very simple task, but I struggle for hours. I have parse xml from
I want to implement fileUpload like in BalusC's example . Unfortunately I already struggle
I'm sure this will be a simple question but I still struggle with DOM
In our web applications we struggle with a problem reoccurring constantly. An easy example
I had a bit of a struggle trying to understand why my code was
I'm trying to create a virtual wiki but I already struggle with In your
I'm a Scala beginner and this piece of code makes me struggle. Is there
I'm building a blog page. But I'm starting to struggle on the archive section.
After a long struggle with jQuery countdown plugin I still cannot solve it, but

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.