Monday, April 26, 2010

Re: How to write to_char() function in cakephp

thanks sir

On Mon, Apr 26, 2010 at 10:00 PM, cricket <zijn.digital@gmail.com> wrote:
On Apr 26, 9:44 am, Narendra Padala <crazycake...@gmail.com> wrote:
> Hi all,
> I am new to cakephp i have problem like this please help me out sir
>
> $currentday = date("w");
>
> $rsStoreTime = pg_exec($conn, "SELECT
> intdaynumber,to_char(dtmopeningtime,'HH24:mi') AS
> dtmopeningtime,to_char(dtmclosingtime,'HH24:mi') AS dtmclosingtime
> FROM tblstorehours where intdaynumber = '$currentday'");
>
> above query  how can i convert into cakephp
>
> and
>
> $intCnt = pg_numrows($rsStoreTime);
>
> this query also how can i convert into cakephp .....

It's a bit tricky to provide an example as your names do not follow
Cake's convention, so I'll have to assume a few things. First, that
the model name is StoreHour (maybe you should post the model name and
its associations).

So, from StoreHoursController:

$rsStoreTime = $this->StoreHour->find(
       'first',
       array(
               'fields' => array(
                       'StoreHour.intdaynumber',
                       'to_char(dtmopeningtime,"HH24:mi") AS dtmopeningtime',
                       'to_char(dtmclosingtime,"HH24:mi") AS dtmclosingtime'
               ),
               'conditions' => array(
                       'StoreHour.intdaynumber' => date('w')
               )
       )
);


But there's one thing you have to know about Cake. When using
calculated fields like this, Cake puts the result in a spearate array
than normal. See this article for a clear explanation as well as a
quick fix:

http://teknoid.wordpress.com/2008/09/29/dealing-with-calculated-fields-in-cakephps-find/

Also, if you're using version 1.3 you should have a look at "virtual
fields". See here:

http://book.cakephp.org/view/1608/Virtual-fields


But you really should re-name things to follow Cake's naming
conventions, though. It'll save you a lot of trouble in the end.

table: store_hours

fields: day (not reserved in Postgres), opening_time, closing_time

And, while i'm a big fan of using a database's built-in functions (and
a big fan of PG) I wonder if it's worth doing all this in the query.
You could simply make the conversion in the view using PHP.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
 
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: