Hey guys,
I just started with BKS Active Record which could be awesome software, but was really hard to get going. After having figured out the basics, I would like to share it with the community. Hopefully, it saves you guys some time and increases the Active Record usage in Xojo.
This is all the documentation I could find:
- Getting started video from BKS (6 min, a good start but 5 years old)
- official Xojo screencast more focused on general benefits than usage itself. Actual demo starts at 32:50
- Quick and dirty description from 2014
After working through above sources, this is my summary to get ActiveRecord working in your project and use it:
I) Including ActiveRecord in your project:
-
Download BKS Active Record
-
Copy the BKS folder from the downloaded project into the project
-
Set the constants in BKS_ActiveRecordConfig accordingly (In my case: kIncludeMySQLCommunityServer = true)
-
Add a database in the open event of the app (adding it from the IDE did not work for me):
[code]Dim db As New MySQLCommunityServer
db.host=“my_host”
db.port=3306
db.databaseName=“my_db”
db.userName=“my_user”
db.Password=“my_password”
If not db.Connect Then
MsgBox(“Can’t connect!”)
return
End If[/code]
- Register the database with Active Record: (right after the above code in the open event)
BKS_ActiveRecord.Connect(db)
-
Add Classes to the Project (In my case: Person with properties id(Integer), Name(String) and Age(Integer).
Important 1: For the ActiveRecord magic to work, the super class for every class which should be saved to the database needs to be BKS_ActiveRecord.Base
Important 2: To work with ActiveRecord, every class needs to have an id(integer) property (needs to be auto increment in the database, more about the database itself later) -
Add a table for each class that you registered with ActiveRecord with the exact same name as your class (case sensitive!). Apparently, you could also add “tbl” as a prefix to the table name. I did not do that. (Sequel Pro is a really good free open source tool for modifying MySQL databases on Mac OS. )
-
Add each class property as a column in the database. (Strings will be VARCHAR, Integer will be INT). Don’t forget to add an “id” column with auto increment turned on.
That’s it. Now, you can Create, Read, Update, Delete. All you classes now have the following instance methods as they all inherit from BKS_ActiveRecord.Base:
- load
- save
- delete
II) Basic usage of CRUD functions:
A) How to create a new record in the database:
[code] // Create a new instance of your class
Dim newPerson As New Person
newPerson.Name = “Jane”
newPerson.Age = 18
//calling the .save method to save it to the database
newPerson.Save[/code]
B) How to load and update and delete a record from the database:
dim p As New Person
//load Person from database
If p.Load(1) Then // 1 is the id of the requested Person
MsgBox ("Name should be Jane and is ===> " + p.Name) // Output will be "Jane" since it was the first record generated
End If
//Modify the instance
p.Name = "Otto"
//write the updated instance to database
p.save
//Reload record set again to check if the name changed
If p.Load(1) Then // 1 is the id of the requested Person
MsgBox ("Name should be Otto and is ===> " + p.Name) // Output should now be "Otto"
End If
// Delete the instance from database
p.delete
III) Querying the database with Active Record:
A) Get all instances
To make it easier, I used the shared method “List” from the example file provided by BKS and added it to my Person class (I don’t really understand why it’s not a shared method in BKS_ActiveRecord.Base):
[code]Public Shared Function List(sCriteria as string = “”, sOrder as string = “”) as Person()
dim aro() as Person
dim oRecord as Person
dim ty as Introspection.TypeInfo = GetTypeInfo( Person )
for each oRecord in BKS_ActiveRecord.Query(ty, sCriteria, sOrder)
aro.Append(oRecord)
next
return aro
End Function[/code]
Now I can easily call the method to get an Array of dictionaries of all records in the database and put them in a listbox:
for each oPerson as Person in Person.List
Window1.Listbox1.AddRow( oPerson.Name, Str(oPerson.Age) )
Window1.Listbox1.RowTag( Window1.Listbox1.LastIndex ) = oPerson
next
B) Querying the database to get specific records:
The above “List” method conveniently takes two arguments, the search term “sCriteria” and the order “sOrder”. In order to get only people by the name of John, you would add these arguments, e.g.
Dim myPersons() As Person
myPersons = Person.List("Name=""Otto""")
The argument sCriteria takes an SQL expression which would be
Name = "Otto"
In order to escape the quotes (") in Xojo you use an additional quote (") as escape:
"Name=""Otto"""
After having written that, it’s really not hard or complicated, but it took many hours figuring that out. However, there is so much more to Active Record like the validations AfterCreate(), AfterDelete(), AfterSave(), AfterUpdate() which I don’t have a clue how to access.
Maybe someone who uses ActiveRecord himself or maybe even the makers @BKS could elaborate where to go from there.
Thanks!
Ben