Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've got something very similar in an app. I'm using the <a href="http://www.symfony-project.org/plugins/sfFormExtraPlugin" rel="nofollow">sfFormExtraPlugin</a> for fancy date widgets. </p> <p>My model is "Complaint" and my action is "Explore".</p> <p><em>lib/form/ExploreForm.php</em>:</p> <pre><code>class ExploreForm extends BaseForm { public function configure() { $this-&gt;setWidgets (array( 'explore_range' =&gt; new sfWidgetFormDateRange (array( 'from_date' =&gt; new sfWidgetFormJQueryDate(), 'to_date' =&gt; new sfWidgetFormJQueryDate(), 'label' =&gt; 'Date of Service ranging ', ) ) ) ); $this-&gt;setValidators(array( 'explore_range' =&gt; new sfValidatorDateRange (array( 'required' =&gt; true, 'from_date' =&gt; new sfValidatorDate(array('required' =&gt; false)), 'to_date' =&gt; new sfValidatorDate(array('required' =&gt; false)) )), 'from' =&gt; new sfValidatorPass(), 'to' =&gt; new sfValidatorPass() ) ); } } </code></pre> <p><em>apps/frontend/modules/complaint/templates/exploreSuccess.php</em>:</p> <pre><code>&lt;form action="&lt;?php echo url_for('complaint/explore') ?&gt;" method="GET"&gt; &lt;input type="submit" value="Change date range" style="float:right" /&gt; &lt;ul&gt; &lt;?php echo $form-&gt;renderUsing('list') ?&gt; &lt;/ul&gt; &lt;/form&gt; </code></pre> <p>In <em>apps/frontend/modules/complaint/actions/actions.class.php</em>: public function executeExplore($request) { // default: the first day of this month - 1 year</p> <pre><code> $this-&gt;form = new ExploreForm(array( 'explore_range' =&gt; array ( 'from' =&gt; $a_year_ago, 'to' =&gt; $last_of_last_month ) )); if ($request-&gt;hasParameter('explore_range') ) { $this-&gt;form-&gt;bind( array('explore_range' =&gt; $request-&gt;getParameter('explore_range')) ); $this-&gt;logMessage("bound", "debug"); if ($this-&gt;form-&gt;isValid()) { $this-&gt;form_values = $this-&gt;form-&gt;getValues(); # cleaned $this-&gt;logMessage("validation WIN", "debug"); } else { $this-&gt;logMessage("validation FAIL", "debug"); $this-&gt;form_values = $this-&gt;form-&gt;getDefaults(); } } else { $this-&gt;logMessage("no explore_range param", "debug"); $this-&gt;form_values = $this-&gt;form-&gt;getDefaults(); } $this-&gt;from = $this-&gt;form_values['explore_range']['from']; $this-&gt;to = $this-&gt;form_values['explore_range']['to']; /* complaints per month */ $this-&gt;complaints_by_month = ComplaintTable::getMonthCounts($this-&gt;from, $this-&gt;to); // ... } </code></pre> <p>And the actual query is in the model, <em>lib/model/doctrine/ComplaintTable.class.php</em>:</p> <pre><code>public static function getMonthCounts($from, $to) { $connection = Doctrine_Manager::connection(); $query = &lt;&lt;&lt;ENDSQL SELECT year(`date`) as y, month(`date`) as m, count(*) as c FROM `complaints`.`complaint` WHERE `date` BETWEEN ? AND ? GROUP BY year(`date`), month(`date`) ENDSQL; $statement = $connection-&gt;execute($query, array($from, $to)); $result = array(); while ($row = $statement-&gt;fetch()) { $result[ sprintf("%04d-%02d",$row[0], $row[1]) ] = $row[2]; } return self::addZeroRows($result, $from, $to); } public static function addZeroRows($set, $from, $to) { /* insert zero counts for months with no count */ $from_fields = date_parse($from); $to_fields = date_parse($to); $start_y = $from_fields['year']; $end_y = $to_fields['year']; $start_m = $from_fields['month']; $end_m = $to_fields['month']; $i = 0; for ( $y = $start_y; $y &lt;= $end_y; $y++ ) { for ( $m = ($y == $start_y ? $start_m : 1) ; ($y == $end_y &amp;&amp; $m &lt;= $end_m) || ($y &lt; $end_y &amp;&amp; $m &lt;= 12); $m++ ) { $y_m = sprintf("%04d-%02d",$y,$m); if ( !isset( $set[$y_m] ) ) { $set[$y_m] = 0; } if ($i++ &gt; 100) { // don't infinitely loop... you did it wrong return $set; } } } ksort($set); return $set; } </code></pre> <p>Now, I'm using Doctrine so you'll have to do a bit of translation into Propelese in the model part, and you may not be doing the "statistics broken down by month" thing I'm doing here, but it should help you get going. Good luck! </p>
    singulars
    1. This table or related slice is empty.
    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.
    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