Tuesday, July 5, 2011

Re: separate class that prepares an IteratorAggregate object for View layer - where to place this?

Hello,

well, I would set the Arrays that should be converted to the Itearator
objects to a special viewVar, i.e.:

In your Controller action
<?php
$iterators = array();
$cars = $this->Car->find('all');
$trucks = $this->Truck->find('all');

$iterators = Set::merge($iterators, $cars, $trucks);
$this->set(compact('iterators'));
?>


In AppController::beforeRender(), this way you can use this everywhere
if you add to the iterators viewVar
<?php
$iterators = empty($this->viewVars['iterators']) ? array() :
$this->viewVars['iterators'];
foreach($iterators as $alias => $data) {
$iterators[$alias] = ArrayConverter::createAggregator($data);
}
$this->viewVars['iterators'] = $iterators;
?>

ArrayConverter is a library class in 'app/lib/array_converter.php' that
you can load with App::import().


Regards,
Thomas

Am Dienstag, den 05.07.2011, 04:20 -0700 schrieb Zeu5:
> Hi Thomas,
>
> let me state my concerns here. Perhaps you can tell me if your
> suggested answer is still good for the scenario.
>
> I have various models and controllers.
>
> Cars, Trucks, Tables, Chairs, Users, Passengers, etc.
>
> I have admin actions: Admin_view, admin_edit, admin_add,
> Actions for public are view, edit, add, etc.
>
> I will use Twig for Themes on views that are for public consumption.
>
> And i will not expose all the data models for designers to manipulate.
> Just to be clear, i will expose only cars, trucks and passengers in
> their respective index page.
>
> 1) Having said this, the IteratorAggregate class should still be in
> app lib?
>
> 2) the Controller::beforeRender you would mean in cars, trucks and
> passengers controllers only right?
>
> 3) so far your idea sounds good as i typed this response. I am liking
> it.
>
> On Jul 5, 6:53 pm, Thomas Ploch <profipl...@googlemail.com> wrote:
> > Well, you would not do it on the Behavior itself, since you want to pass
> > the object to the view.
> >
> > Probably a static method as in your example that converts the find array
> > into an object that implements IteratorAggregate (probably 'app/lib' is
> > the best place for this).
> >
> > Then in Controller::beforeRender you could iterate over the viewVars and
> > convert the arrays accordingly.
> >
> > Regards,
> > Thomas
> >
> > Am Dienstag, den 05.07.2011, 02:41 -0700 schrieb Zeu5:
> >
> >
> >
> >
> >
> >
> >
> > > Hi there,
> >
> > > i think you mean a Model Behavior?
> >
> > > If so, do i still need to create a separate class which implements the
> > > IteratorAggregate?
> >
> > > If so, is it done in the same file as the Behavior?
> >
> > > Thank you.
> >
> > > On Jul 5, 5:28 pm, euromark <dereurom...@googlemail.com> wrote:
> > > > i see
> > > > you would probably need a global app controller behavior which
> > > > translates everything right away
> >
> > > > php doesnt have the attributes itself
> > > > but you can acomplish that by using:
> > > > $size = count($cars);
> > > > $first = array_shift($cars);
> > > > $last = array_pop($cars);
> > > > and may others
> > > > etc
> >
> > > > On 5 Jul., 02:38, Zeu5 <kimc...@gmail.com> wrote:
> >
> > > > > I am using Twig so that I can make it as easy for designers to design
> > > > > themes as possible without knowing php.
> >
> > > > > Currently, Twig evaluates the strings, arrays, objects like thishttp://www.twig-project.org/doc/templates.html#variables
> >
> > > > > i want to keep things as simple and as brief as possible.
> >
> > > > > Hence I want to make it such that designers need only use
> > > > > {{ cars.size }} , {{ cars.first }} and {{ cars.last }}
> >
> > > > > So i guessed that cars cannot be usual php arrays, but objects with
> > > > > attributes for size, first and last.
> >
> > > > > If my understanding is wrong about php arrays, i apologise.
> >
> > > > > Please educate me on how usual php arrays can possibly have these
> > > > > attributes as well.
> >
> > > > > On Jul 5, 7:17 am, euromark <dereurom...@googlemail.com> wrote:
> >
> > > > > > why are the normal 1.3 arrays not working for you?
> > > > > > they also have all those functions built in (in plain php functions
> > > > > > then of course)
> >
> > > > > > On 5 Jul., 01:06, Zeu5 <kimc...@gmail.com> wrote:
> >
> > > > > > > I have cars controller, Car model and views for cars.
> >
> > > > > > > I am using Twig 1.0,http://www.twig-project.org/doc/templates.html#list-of-control-struct...
> > > > > > > Cake 1.3
> >
> > > > > > > I want to do the following in the controller:
> >
> > > > > > > function index() {
> >
> > > > > > > ...
> >
> > > > > > > $carData = $this->Car->find('all');
> >
> > > > > > > $cars = Car::prepareForView($carData);
> >
> > > > > > > $this->set('cars', $cars);
> >
> > > > > > > }
> >
> > > > > > > the prepareForView will turn the car data into an object that
> > > > > > > implements the IteratorAggregatehttp://php.net/manual/en/class.iteratoraggregate.php
> >
> > > > > > > the reason is because i want to be able to do the following in the
> > > > > > > view using Twig.
> >
> > > > > > > {{ cars.size }} // returns me the size
> > > > > > > {{ cars.first }} // returns me the first element of the array
> > > > > > > {{ cars.last }} // returns me the last element of the array
> >
> > > > > > > Am i right in wanting to prepare the array as an IteratorAggregate?
> > > > > > > If i do this for Car and other models, how should i do it?
> >
> > > > > > > Advice, please. Thank you.
>


--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: