Tuesday, May 31, 2011

Re: Best practise? Creating a detailed data report from different linked models?

My two cents:

The recommendation to have your report be an action in the appropriate
controller good one assuming that fits your case. In my experience.
It's been my experience as the size/complexity of an application grows
you want multiple reports each of which spans your data to some
degree. In this case I would make a reports contoller. It seems like
the more natural solution to me, and I don't think it works against
the framework at all.

On May 31, 8:34 pm, dreamingmind <dreamingmin...@gmail.com> wrote:
> Michael,
>
> You could certainly have a Reports controller. The expectation would
> be that it is operating on the Report model. This seems pretty
> unlikely given the information so far. More likely you are wanting a
> report on data in some other model (like Project or Customer or ...)
> and it's related models.
>
> Starting with the assumption that you want to generate reports on the
> model Project, you would want a report action in your Projects
> controller. A simple example would be this:
>
> function report() {
>   if(!empty($this->data)) {
>      $report = $this->Project->find('all', array('conditions'=>'your
> conditions', 'contain'=>'fields you want'));
>      $this->set('report', $report);
>   }
>
> }
>
> In the folder app/views/projects/ you would store your file report.ctp
> This file would output a form so the user can control and modify
> reports. Also have this file output a formatted report if data is
> available to do so. User the Form Helper to generate the form on your
> page (http://book.cakephp.org/view/1383/Form).
>
> Given this setup the user would get to the page athttp://yoursite.com/projects/report
>
> The report action in your Project controller looks to see if it was
> being provided with posted data. On the first visit there would be
> none so the logic would just fall out the bottom of the action and
> report.ctp is rendered with only the form displayed. The user fills
> the form, submits the data back to projects/report and this time the
> if() is satisfied, the find() is done and data passed to the view in
> $report. Now the page shows the form AND the report!
>
> One action, one view, sweet!
>
> Your various models would be designed with hasMany, belongsTo,
> hasAndBelongsToMany, etc. as appropriate. Probably you would also want
> your models to use the Containable behavior (http://book.cakephp.org/
> view/1323/Containable) since you imply there is a lot of linking
> involved.
>
> I think you're fighting against the framework a bit. To become more
> familiar with where to store your files and how to name things, look
> these sections over again:http://book.cakephp.org/view/899/CakePHP-Folder-Structurehttp://book.cakephp.org/view/901/CakePHP-Conventions
>
> Regards,
> Don
>
> On May 31, 1:19 pm, mivogtGermanyLU <miv...@mivogt.net> wrote:
>
>
>
>
>
>
>
> > Don,
>
> > yes I need a form to offer the user a way to select the criteria.
>
> > As the SQL is a bit more complex, the use of find will not be my way.
> > I know it is a dirty way of using cake but a long sqlquery-string
> > works just better for me and offers me a nice result so far without
> > learning the complex find (in my case I am using nearly all my models
> > linked with each other)
>
> > I am not sure about naming files and functions this for and placing
> > them in the right directory.
>
> > Is it ok to create a "reports_controller.php" containing an action
> > "CreateReport($id,$date)" and 2 views "selectDataSet.ctp",
> > "showReport.ctp"
>
> > .. pleas feel free to correct my naming as it will not be within the
> > convention so far ;)
>
> > TIA
>
> > michael
>
> > On 31 Mai, 20:20, dreamingmind <dreamingmin...@gmail.com> wrote:
>
> > > Michael,
>
> > > I'm not sure I see what the problem is. Your link/button can certainly
> > > pass any search parameters you want in to your action. It sounds like
> > > you might be planning to offer a small form for the user to choose
> > > search params? The Form Helper will let you put all that together and
> > > the data will come back to you in $this->data in your action (http://
> > > book.cakephp.org/view/1383/Form). The action can make use of the
> > > parameters to build a $this->yourModel->find() call. The returned data
> > > will be be passed along to the view because actions by default render
> > > their associated views. All pretty standard stuff unless I'm
> > > misreading your message.
>
> > > If you're really planning on writing that sql by hand, you might want
> > > to review thishttp://book.cakephp.org/view/1017/Retrieving-Your-Data
>
> > > Regards,
> > > Don
>
> > > On May 31, 10:28 am, mivogtGermanyLU <miv...@mivogt.net> wrote:
>
> > > > Hi there,
>
> > > > my app has several models with defined relations.
> > > > Entering Data works fine also all the CRUD stuff is fine.
>
> > > > Now  I need to collect data from the database ostly leftjoined and
> > > > limited and sorted by some definitions... so far as first step I made
> > > > me an SQL statement trying insidephpmyadminand it works fine.
>
> > > > To get it more comfortable I would like to have a view offering to set
> > > > the data-limiters.
> > > > In my case it will be a month/year datafield and the id of one model
> > > > all the other stuff is linked somehow.
>
> > > > I am not sure about how to go on with this and would be happy to get a
> > > > helping comment...
>
> > > > I guess I will need a SelectWhatToReport-View to select month/year/
> > > > model_id
> > > > passing this to a controller-action CreateReport($month,$year,
> > > > $model_id)
>
> > > > in controller I might add the new action
> > > > CreateReport($month,$year, $model_id)
> > > > {sql='select ... where .. $month .. ,$year, ..  $model_id'
> > > > // . creating sql result array to be passed to a view
>
> > > > }
>
> > > > not sure about how to call a 2nd view from controller to display the
> > > > result from SqlResultArray using some while statements ...
>
> > > > ok maybe I better rewrite in short sentences what I want to know/need
> > > > to do:
> > > > - need to get a view offering to choose an entry of a model from
> > > > database (sems to be easy I hope)
> > > > - passing this selectioncriteria  to controller/action (hopefully just
> > > > by pressing a button)
> > > > - calling a view from controller side to display the result (sql
> > > > array)
>
> > > > Thanks in advance
>
> > > > Michael

--
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: