Note that there are some explanatory texts on larger screens.

plurals
  1. POcalculation of the area of a rectangle
    primarykey
    data
    text
    <p>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.</p> <h2>Entity Rectangle:</h2> <p><strong>The entity Rectangle contains two inportant properties <code>$length</code> and <code>$width</code>.</strong> </p> <pre><code>// 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; </code></pre> <h2>FORM</h2> <p>Of course, the user can set length and width via a form</p> <h2>CONTROLLER</h2> <p><strong>CreateRectangleAction:</strong> renders the form on GET request and work on the data on a POST request.</p> <p><strong>ViewRectangleAction:</strong> shows the rectangle.</p> <h2>RECTANGLE MANAGER</h2> <p>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).</p> <pre><code>// 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-&gt;em = $em; $this-&gt;class = $class; $this-&gt;repository = $em-&gt;getRepository($class); } /** * @param $id * @return Rectangle */ public function find($id) { $rectangle = $this-&gt;repository-&gt;find($id); } </code></pre> <h2>THE PROBLEM: WHAT IF?</h2> <p><strong>What if I need to do some calculations on the rectangle?</strong> 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? </p> <p>Not knowing how to do this properly, I went for this pretty bad solution:</p> <p>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. </p> <pre><code>// src/Acme/CalculationBundle/Entity/ class RectangleDisplay { /** * @var Rectangle $rectangle */ protected $rectangle; /** * @var Integer */ protected $area; /** * @param Rectangle $rectangle */ public function __construct(rectangle $rectangle){ $this-&gt;rectangle = $rectangle; //calculate are $area = this-&gt;calculateArea($this-&gt;rectangle-&gt;getLength(),$this-&gt;rectangle-&gt;getWidth()); $this-&gt;area = $area; } /** * @return Integer $area */ public function calculateArea($length,$width) { return $length * $width; } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload