3

I want to implement php classes that should model the following : (Symfony,DoctrineMongoDBBundle)

Notification Collection { from { id , fname } , to , sentDate }

Member Collection { id , fname , lname , email , phone , regDate , ... }

And i want tell to ODM : "from field is an object that holds only 2 values ,id of the sender and his fname"

What annotation i must use? Should i define another class like from.php ? Or i can create two classes as the following:

/*
 *@Document
 */
class Notification {
/*
 *@Id
 */
protected $id;

/*
 *@EmbedOne(targetDocument="Member")
 */
protected $from;

/*
 *@ReferenceOne(targetDocument="Member")
 */
protected $to;

/*
 *@Date
 */
protected $sentDate;
}


/*
 *@Document
 */
class Member {
/*
 *@Id
 */
protected $id;

/*
 *@String
 */
protected $fname;

/*
 *@String
 */
protected $lname;

/*
 *@String
 */
protected $email;

.
.
.
}

If it's correct,in my controller how can i control "from" field to hold only id and fname from a Member object? suppose this:

$senderUser;
$newNotification = new Notification();
$newNotification->setFrom($senderUser);

Then $newNotification->from set to a Member object that hold all info about a Member.But i want only id and fname of the sender to persist! (because needs of my project) Excuse my English grammatical errors. Thanks for any help...

ABS
  • 2,626
  • 3
  • 28
  • 44

1 Answers1

0

If you insist on nesting the notification's originating user ID and first name in a from field, then you will need an Embed One relationship to a From document, which in turn has the following fields:

  • id: this should be a Reference One relationship to a Member document. I would suggest using the simple option for the reference, so that you only store the Member's ID value, rather than a DBRef object.
  • fname: this should be a string field. ODM has no facility for keeping it up-to-date with the referenced Member document, so you will need to ensure that on your own.

Personally, the Notification document seems small enough that I would simply create fromMember and fromMemberFirstName fields directly on Notification. Then, you could add some logic in the setFromMember() method that also sets the fromMemberFirstName field from the passed Member argument.

If you want to take things a step further, you could explore using events to monitor Member objects for changed fname fields and issue queries to update Notification documents, but that's a separate conversation.

jmikola
  • 6,892
  • 1
  • 31
  • 61