Ver Mensaje Individual
  #5 (permalink)  
Antiguo 14/03/2016, 11:02
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 10 meses
Puntos: 379
Respuesta: Como hacer un ViewModel

creo que lo que necesitas implementar es un Presenter, y quiero suponer que en la entidad Usuario ya tienes un método que recupera la imagen del logo y solo necesitas la ruta donde esta almacenada esa imagen.
Te dejo esto como ejemplo sencillo.

Presenter
Código PHP:
Ver original
  1. /**
  2.  * Class AbstractPresenter
  3.  */
  4. abstract class AbstractPresenter
  5. {
  6.     protected $entity;
  7.  
  8.     public function __construct($entity)
  9.     {
  10.         $this->entity = $entity;
  11.     }
  12.  
  13.     public function __call($methodName, $arguments)
  14.     {
  15.         $methods = get_class_methods($this->entity);
  16.  
  17.         if (in_array($methodName, $methods)) {
  18.             return call_user_func_array(array($this->entity, $methodName), $arguments);
  19.         } else {
  20.             throw new \Exception("No such method " . $methodName);
  21.         }
  22.     }
  23. }
User Presenter
Código PHP:
Ver original
  1. /**
  2.  * Class UserPresenter
  3.  */
  4. class UserPresenter extends AbstractPresenter
  5. {
  6.     private $path = 'path/to/image';
  7.  
  8.     public function getLogo()
  9.     {
  10.         return $this->getPath().DIRECTORY_SEPARATOR.$this->getImage();
  11.     }
  12.  
  13.     private function getPath()
  14.     {
  15.         return $this->path;
  16.     }
  17. }
método del controlador
Código PHP:
Ver original
  1. /**
  2.      * Method Controller
  3.      *
  4.      * @return array
  5.      */
  6.     public function showAction()
  7.     {
  8.  
  9.         $user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id);
  10.  
  11.         return array(
  12.             'entity' => new UserPresenter($user),
  13.         );
  14.     }

vista
Código PHP:
Ver original
  1. {{entity.logo}}
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.

Última edición por hhs; 14/03/2016 a las 13:37