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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Operations on variables


Go to End


12 Posts   3356 Views

Avatar
BlueO

Community Member, 52 Posts

29 October 2010 at 10:05am

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

Community Member, 712 Posts

29 October 2010 at 12:31pm

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

Avatar
Invader_Zim

Community Member, 141 Posts

29 October 2010 at 12:36pm

Edited: 29/10/2010 12:37pm

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

Community Member, 52 Posts

29 October 2010 at 9:43pm

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

Community Member, 141 Posts

30 October 2010 at 12:41am

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

Community Member, 712 Posts

30 October 2010 at 1:52pm

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

Community Member, 52 Posts

2 November 2010 at 7:39pm

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

Community Member, 712 Posts

2 November 2010 at 7:45pm

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

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

Go to Top