Results 1 to 2 of 2

Thread: calculated fields easy

  1. #1
    avas325 is offline Junior Member
    Join Date
    Jul 2011
    Posts
    5

    Default calculated fields easy

    Hi all,
    After searching this forum was unable to fined easy way for managing calculated fileds.
    So ended up with writing triggers in MySQL. Very easy, simple and hassel free.
    Example

    DB cat01_stock_cstm
    2 fields stock and stock_comment are in the DB

    CREATE TRIGGER TRIG
    FOR EACH ROW ON UPDATE cat01_stock_cstm
    BEGIN
    IF NEW.stock_c<1 THEN
    SET NEW.stock_comment_c='few';
    ELSEIF NEW.stock_c>=5 THEN
    SET NEW.stock_comment_c='plenty';
    ELSE SET NEW.stock_comment_c='not in stock';
    END IF;
    END

    Works superfast and updates only the updated record
    Am I missing something? or the whole point with adding php code is just to keep less data in the DB?
    seems easy and upgrade stable

  2. #2
    rafael.q.g@hotmail.com's Avatar
    rafael.q.g@hotmail.com is offline Sugar Community Member
    Join Date
    Jun 2011
    Location
    Florianópolis - Brazil
    Posts
    782

    Default Re: calculated fields easy

    It is a very good approch for computed fields.
    But my personal opnion is whether some information is really computed it don't have to be store it on database, it has to be calculated on runtime. Because sometimes the field contents is based on external infos that could be changed after trigger execution, and then when you read this information back it could be obsolete.

    Sugar provides a simple way to do that using fields managed by a PHP function, and this function receives automatically an object named $bean which already has all attributes filled supporting an easy computation of new fields.
    Rafael Queiroz Gonçalves
    Advanced OMG UML Certified Professional
    Sun Certified Enterprise Architect for the Java Platform
    Sun Certified Programmer for the Java 2 Platform
    IBM Certified Advanced Application Developer - Lotus Notes and Domino
    IBM Certified Application Developer - IBM WebSphere Portlet Factory
    Computer Science Mastering / UFSC - PPGCC

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. v6.1 calculated fields
    By Meyer in forum Help
    Replies: 2
    Last Post: 2011-04-08, 05:35 AM
  2. calculated fields
    By Meyer in forum Help
    Replies: 0
    Last Post: 2010-12-07, 01:53 PM
  3. Calculated Fields
    By cbeagle in forum Developer Help
    Replies: 7
    Last Post: 2009-10-15, 08:33 AM
  4. Creating Fields - calculated fields and 'related' field
    By gipps in forum General Discussion
    Replies: 0
    Last Post: 2007-07-21, 10:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •