Doctrine 2: page, block, strategy template, and related object

My problem is precisely described in the strategy template article in the Doctrine documentation :

  • An object
  • A page can have several blocks
  • A block can be text, image, form, calendar, ... (strategy)
  • The page knows about the blocks it contains, but does not know about their behavior.
  • Block Inheritance Impossible

The described solution (strategy template) is similar to what I need (read the article for more information):

Page:

<?php

namespace Page\Entity;

class Page
{
    /**
     * @var int
     * @Id @GeneratedValue
     * @Column(type="integer")
     */
    protected $id;

    /**
     * @var string
     * @Column
     */
    protected $title;

    /**
     * @var string
     * @Column(type="text")
     */
    protected $body;

    /**
     * @var Collection
     * @OneToMany(targetEntity="Block", mappedBy="page")
     */
    protected $blocks;

    // ...
}

Block:

<?php

namespace Page\Entity;

class Block
{
    /**
     * @Id @GeneratedValue
     * @Column(type="integer")
     */
    protected $id;

    /**
     * @ManyToOne(targetEntity="Page", inversedBy="blocks")
     */
    protected $page;

    /**
     * @Column
     */
    protected $strategyClass;

    /**
     * Strategy object is instancied on postLoad by the BlockListener
     *
     * @var BlockStrategyInterface
     */
    protected $strategyInstance;

    // ...

}

Strategic Interface:

<?php

namespace Page\BlockStrategy;

interface BlockStrategyInterface
{
    public function setView($view);

    public function getView();

    public function setBlock(Block $block);

    public function getBlock();

    public function renderFrontend();

    public function renderBackend();
}

I can easily imagine what would be my strategy if I displayed a form or calendar; but what if my strategy is to display the contents of another object?

/id , .

entityClass entityId , , postLoad BlockListener. , ? postLoad.

, , , Block .

, , .

, ... , - ?

+3
1

, , , , .

, :

foreach ($block in $blocks) {
  $block->display();
}

.

function __destruct() {
   foreach ($block in $blocks) {
     /* call a common interface function that does all that need to be done, implemented on each block */
   }
}

: http://en.wikipedia.org/wiki/Composite_pattern

0

All Articles