Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Operations on variables

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

Page: 1 2
Go to End
Author Topic: 1464 Views
  • BlueO
    Avatar
    Community Member
    52 Posts

    Operations on variables Link to this post

    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;
       }
    }

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Operations on variables Link to this post

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

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: Operations on variables Link to this post

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

  • BlueO
    Avatar
    Community Member
    52 Posts

    Re: Operations on variables Link to this post

    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

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: Operations on variables Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Operations on variables Link to this post

    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

  • BlueO
    Avatar
    Community Member
    52 Posts

    Re: Operations on variables Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Operations on variables Link to this post

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

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

    1464 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.