2

I come from a .net background so the empty classes (models) that I'm seeing in Lithium is unsettling.
In .net, I don't have a property unless I do something like:

public class MyClass()
   public property myProp as string
end class

and then I set or get the property like so:

dim aClass as myClass
aClass.myProp = "some string"
dim myString as String = aClass.myProp

What I'm seeing in Lithium are dynamic objects a la javascript.
I can declare an arbitrary object and add properties as I go. Now, I'm not saying this is a bad thing, I just want to know:

  1. If this is normal for PHP or normal for Lithium, and
  2. If I add properties (so I can get code completion in eclipse), will it hurt the ORM features of Lithium?
hakre
  • 193,403
  • 52
  • 435
  • 836
ton.yeung
  • 4,793
  • 6
  • 41
  • 72

1 Answers1

3

Lithium is fairly advanced and takes advantage of some PHP features that many frameworks do not. You can add properties directly to objects in PHP, but, when using a framework, you want to look at what the best practices are and how it will impact your application.

In Lithium, MyModel::create() or MyModel::find() return entity objects or collections of entity objects that represent database records or documents.

Entity properties are stored in the protected $_updated and $_data arrays and accessed via __get and __set methods.

So, when you reference $myModel->title, you're getting/setting $myModel->_updated['title'].

Then, when you call $myModel->save(), the data in $_updated is saved to the database record or document.

See https://github.com/UnionOfRAD/lithium/blob/master/data/Entity.php for more details.

Justin Jenkins
  • 26,590
  • 6
  • 68
  • 1,285
psparrow
  • 9,808
  • 1
  • 17
  • 11
  • so that means its a lithium thing, and coding in actual properties is a bad thing? (again, only harping on it for code completion. If its a not a good idea to code in properties, I'm good with not doing it) – ton.yeung Dec 19 '11 at 16:28
  • Right. You'll want to stay away from it when working with entity data. Enjoy working with Lithium! – psparrow Dec 19 '11 at 16:33
  • You don't say this explicitly, but I *think* adding properties will short-circuit the mapping that Lithium does; your changes will not go into the `$_updated` array and won't get saved to the database. So adding the properties manually will actually cause problems. – benzado Dec 19 '11 at 19:51
  • If you're obtaining the entity through Model::create() or Model::find(), you're not actually extending the Entity object. Simply setting $myModel->customField = 1 will set that value in the $_updated array. I haven't found a need to extend the Entity object yet as the entity returned by the model handles the field mapping perfectly. – psparrow Dec 19 '11 at 20:36