Skip to content

Mongodb support for non-existent field #471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

foxbit19
Copy link

Mongodb has no fixed structure for collections. So a field may be non-existent in mongodb.
In this case, the property that represents the foreignKey cannot be retrieved and an ErrorException will be thrown.
I've added a try-catch construct to avoid the case in which the field doesn't exists.

Mongodb has no fixed structure for collections. So a field may be non-existent in mongodb.
In this case, the property that represents the foreignKey cannot be retrieved and an ErrorException will be thrown.
I've added a try-catch construct to avoid the case in which the field doesn't exists.
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 96.49% when pulling f3e5c7f on foxbit19:patch-1 into 8304a46 on jenssegers:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 96.49% when pulling f3e5c7f on foxbit19:patch-1 into 8304a46 on jenssegers:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 96.49% when pulling f3e5c7f on foxbit19:patch-1 into 8304a46 on jenssegers:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 96.49% when pulling f3e5c7f on foxbit19:patch-1 into 8304a46 on jenssegers:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 96.49% when pulling f3e5c7f on foxbit19:patch-1 into 8304a46 on jenssegers:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 96.49% when pulling f3e5c7f on foxbit19:patch-1 into 8304a46 on jenssegers:master.

@jenssegers
Copy link
Contributor

In what case would you get this error?

@foxbit19
Copy link
Author

I've no foreign keys fields for some documents in a collection. The foreign keys fields are inserted progessively by a laravel application. I think this problem comes up because I've first inserted the documents inside some collections, then I've defined the relations between them.

@jenssegers
Copy link
Contributor

Could you show me a code example when this would happen? I will be able to add it to the tests then and fix it more easily :)

@foxbit19
Copy link
Author

Just define your model without relations:

/*
* Keyboard's key :)
*/
class Key extends Moloquent 
{
    protected $collection = 'keys';
    protected $connection = 'mongodb';
}

Then use the model in your code to populate the collection.

You want to add another collection for the keyboard. But your key collection is populated and you don't want to delete all the documents inside it. You just want to add the relation between the collection without touching any data.

So you add the Keyboard model with the correct relation.

class Keyboard extends Moloquent 
{
    protected $collection = 'keyboards';
    protected $connection = 'mongodb';

    public function getKeys() 
    {
        return $this->hasMany('App\Models\Key','keys');
    }
}

Add now add an inverse relation to the model Key:

class Key extends Moloquent 
{
    protected $collection = 'keys';
    protected $connection = 'mongodb';

    /*
    * Since this is a one to many relation, I want a 'keyboard' field
    * inside the collection using the inverse relation
    */
    public function getKeyboard() 
    {
        return $this->belongsTo('App\Models\Keyboard', 'keyboard');
    }
}

You expected to work with the field keyboard of the collection key but when you use this code

$keyboard = $key->getKeyboard;

you get an ErrorException.

The above code fails because belongsTo search for a field named keyboard inside the collection key. This field cannot exist at this moment.

Consider the case in which I want to add some one-to-many relations to my collections and my data cannot be touched because they are used daily by my clients (through my production systems).

This is the context in which this error happen and I think the same behaviour can be extended to other inverse relations.

@foxbit19
Copy link
Author

Any news of this?

jenssegers added a commit that referenced this pull request May 3, 2015
hackel pushed a commit to arcstone/laravel-mongodb that referenced this pull request May 4, 2015
@jenssegers jenssegers closed this Jul 22, 2018
mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this pull request Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants