1

I have an entity model with an embedded credentials document with a username and password property.

  • Entities
    • ...
    • Credentials
      • Username
      • Password

I currently have validation set on the entities model to ensure that there is no existing username in the database.

My question is: should I put the validation check on a Credentials model or just leave it on the Entities model? An example implementation of the model relationships are shown in this Stack Overflow post: lithium fill multiple models from view

Community
  • 1
  • 1
ton.yeung
  • 4,793
  • 6
  • 41
  • 72

3 Answers3

0

As long as MongoDB relationships are not in place, you'd have to do the mangling between those on your own.

The recommended approach is to place it at the Entities level and use the dot-syntax in your validations (Lithium will recognize these at "subdocuments" accordingly).

daschl
  • 1,124
  • 6
  • 11
  • Just to clarify, you are saying to do the validation (public $validates) on the Entities model and make the key "credential.username"? That's what I have, but I never explicitly instantiate a Credentials model (Credentials::Create), so it works pretty well. It feels kinda wrong though, since the validation and password hashing is on the Entities model instead of Credentials. What's the point of having a Credentials model then? – ton.yeung Jan 04 '12 at 00:32
0

As Daschl pinted out: you have to do this manually.

You could write a Credentials model with no related db connection and embed the credentials create and validates trigger in Entites save & create filters. And you have to take care about uniqueness of the username somewhere.

If you query Entities you will get a DocumentSet with SubDocuments. But they are all bound to Entities. like (pseudo Code)

DocumentSet {
  '_model' => Entites,
  'data' => array (
    'entityvar' => foo,
    'Credentials' => DocumentSet {
      '_model' => Entities,
      'data' => ...
    }
  )
}

Take a look into lithium sphere. They use those SubModels without source connection

dgAlien
  • 428
  • 1
  • 4
  • 9
0

Take a look to Lithium Sphere code at http://github.com/pointlessjon/sphere for a real world example on embedding comments on a blog post

Mehdi Lahmam B.
  • 2,240
  • 16
  • 22