Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Operations on variables


Go to End
Reply

12 Posts   1492 Views

Avatar
BlueO

29 October 2010 at 10:05am Community Member, 52 Posts

Hi All,

This seems like a really simple thing that I just can't get my head around. I have an object with a currency variable on it. I want to be able to divide that variable by 4 and create a new variable. The code I've tried is below. Help much appreciated.
The $Total variable comes from a many_many relationship managed by the dataobjectmanager.

class Flatmate_Controller extends Page_Controller {
function eaPayment($Total) {
   $num = 4;
   $ea = $Total/$num;
   return $ea;
   }
}

Avatar
Hamish

29 October 2010 at 12:31pm Community Member, 712 Posts

How does this get called and what error do you get?

Avatar
Invader_Zim

29 October 2010 at 12:36pm (Last edited: 29 October 2010 12:37pm), Community Member, 141 Posts

Hi Blu0.

Are there any error messages?
Are you really,really sure that the variable $Total is of a type that can be divided? (z.B. an Integer...)

Cheers,
Christian

edit: yay, Hamish was faster...

Avatar
BlueO

29 October 2010 at 9:43pm Community Member, 52 Posts

Hey thanks guys,

I'm trying to 'call' (not sure if i'm using the term correctly) it on the template and have tried $ea, putting it under a <% control eaPayment %> but no luck. I don't get any error message - just no value appears on the template.

The variable is created here: static $db = array ('Total' => 'Currency',); Which stores numbers to two decimal places (eg 123.45) which I assume can be divided - I tried changing it to an 'Int' variable with no luck

Bernard

Avatar
Invader_Zim

30 October 2010 at 12:41am Community Member, 141 Posts

Well, try this one:

function eaPayment() {
$num = 4;
$total = $this->Total;
$ea = $total/$num;
return $ea;
}

And in your template:

<% if eaPayment %>
<p>$eaPayment</p>
<% end_if %>

This might hopefully work.
Cheers,
Christian

Avatar
Hamish

30 October 2010 at 1:52pm Community Member, 712 Posts

By the way - the method should exist on the model (Flatmate), not the controller (Flatmate_Controller). Also, if you add the following to the model, you'll get automagic casting too:

static $casting = array(
   'eaPayment' => 'Currency',
);

That is, you can do any of the following, depending on the type of output you want:

$eaPayment
$eaPayment.Nice
$eaPayment.Int

Avatar
BlueO

2 November 2010 at 7:39pm Community Member, 52 Posts

Ah, you guys are great! Thanks! That worked with a couple of tweaks. The eaPayment function needed to go on my Bill object and be called through <% control Bills %>.

As a learning exercise would someone be able tell me what this part

$total = $this->Total;

actually does. My understanding is that it creates a variable for the function from the 'Total' variable on the current object (being Flatmate in this instance). What i'm not really sure is what the '->' is actually doing.

Also the automagic casting doesn't seem to be going that well. $eaPayment.Nice just returns a blank. It would be awesome to get this going as i'm getting a few decimal places...

b

Avatar
Hamish

2 November 2010 at 7:45pm Community Member, 712 Posts

Your understanding is correct, you might want to read up on OO basics:

[url]http://www.php.net/manual/en/language.oop5.basic.php[/url]

Go to Top