Re: Recreating Timestampable and Geographical Doctrine behaviors

39 views
Skip to first unread message

Knud Potente

unread,
Dec 6, 2012, 8:14:27 PM12/6/12
to redbe...@googlegroups.com
I'm a new RedBean user, too, so other experienced users may have more effective answers.

It seems to me, that you should perhaps use FUSE models to add the logic you need. If I understood the manual correctly, in RedBean, when you perform certain operations on your beans - dispense, load, store, trash - RedBean will automatically check to see if you have a FUSE model with corresponding 'hook' methods. If so, RedBean will run the FUSE method. The way FUSE works, as well as the names of the methods you should use, are described here:


ADDING AUTOMATIC TIMESTAMPS
To ensure that your model will always have 'created_at' and 'updated_at' fields, in your FUSE model use the dispense() method to check whether these properties exist, and if they don't, add them. You can use the R::isoDateTime() function to automatically populate these properties with the current datetime stamp.

To ensure that your model updates the updated_at stamp whenever any information in it is changed, in your FUSE model define an update() method that updates the updated_at property before saving.

ADDING DISTANCE CALCULATION
If you never studied any geometry in your life - of if your geometry days are long forgotten - then you can quickly learn how to calculate the distance between 2 points, here:


TL;DR-people: given 2 points (x1,y1) and (x2,y2) in the cartesian plane, the formula is:

public function distance($x1, $y1, $x2, $y2)  
{  
   return sqrt(pow(($x2-$x1),2) + pow(($y2-$y1),2)); 
}

You can define $x1, $y1 as properties of your bean. You can then define distance as a custom method in your FUSE model, as described here:


I hope the information above helps. If other more experienced users have a better way to implement this, it would be helpful to hear it! :-)


On Saturday, December 1, 2012 10:21:37 AM UTC+11, indiekiduk wrote:
How would you recommend implementing Timestampable and Geographical behaviors that were in Doctrine v1? Since they removed these in Doctrine v2 whilst making it so much more complicated I have been searching for a new ORM and have been amazed by RedBean so far. 

Timestampable added automatic created_at and updated_at datetime fields and automatically updated them when an object was modified. Geographical added latitude and longitude doubles and provided a search that returned rows with a distance field with how far the results were from the search coordinates.

Should I subclass the Redbean record class?

indiekiduk

unread,
May 7, 2013, 7:39:14 PM5/7/13
to redbe...@googlegroups.com
Thanks will look into FUSE.

FYI the distance calculation between 2 lat/lons is not a simple distance calculation because it has to take account the curvature of the earth. Doctrine's calculation is very complex but should be able to be copied.
Reply all
Reply to author
Forward
0 new messages