coen-hyde / Shanty-Mongo

Shanty Mongo is a mongodb library for the Zend Framework. Its intention is to make working with mongodb documents as natural and as simple as possible. In particular allowing embedded documents to also have custom document classes.
Other
200 stars 52 forks source link

Comparisons on 'stuff' #58

Closed tonymillion closed 12 years ago

tonymillion commented 12 years ago

I have a blog system with 'User' objects. Users can either post or comment. A poster can comment on their own post.

In both posts and comments I have

class Comment extends Shanty_Mongo_Document
{
    protected static $_requirements = array(
        'user' => array('Document:User', 'AsReference'),

class Post extends Shanty_Mongo_Document 
{
    protected static $_requirements = array(
        'user' => array('Document:User', 'AsReference'),
        'comments' => 'DocumentSet',
        'comments.$' => 'Document:Comment',

in my code I was coding a system that sends a notification to the writer of a Post and to all the Commenters on a Post. Obviously if a poster has commented on their post I don't want them getting two notifications so I did something like:

foreach($post->comments as $comment)
{
     if($post->user == $comment->user)

however the comparison fails … if I do

 if($post->user->getID() == $comment->user->getID())

it succeeds.

Is this correct operation? Am I doing something wrong?

coen-hyde commented 12 years ago

That is correct operation. Your first attempt fails because you are comparing complex objects that are not the same. The second one works because you are comparing id objects which would be converted to a string during comparison. If you'd like to do the comparison as you did in your first attempt just add a toString method on your user object to return the same thing as a toString on id. eg

public function __toString() { $this->getId()->toString(); }

On Fri, Dec 23, 2011 at 5:16 AM, Tony Million reply@reply.github.com wrote:

I have a blog system with 'User' objects. Users can either post or comment. A poster can comment on their own post.

In both posts and comments I have

class Comment extends Shanty_Mongo_Document
{
       protected static $_requirements = array(
               'user' => array('Document:User', 'AsReference'),

class Post extends Shanty_Mongo_Document
{
       protected static $_requirements = array(
               'user' => array('Document:User', 'AsReference'),
               'comments' => 'DocumentSet',
               'comments.$' => 'Document:Comment',

in my code I was coding a system that sends a notification to the writer of a Post and to all the Commenters on a Post. Obviously if a poster has commented on their post I don't want them getting two notifications so I did something like:

foreach($post->comments as $comment)
{
    if($post->user == $comment->user)

however the comparison fails … if I do

 if($post->user->getID() == $comment->user->getID())

it succeeds.

Is this correct operation? Am I doing something wrong?


Reply to this email directly or view it on GitHub: https://github.com/coen-hyde/Shanty-Mongo/issues/58