Model Class does not exist on relationship

emergingdzns - 6 months ago

I've been struggling all day with something that frankly should just work. I'm building a module. One of the fields in a particular stream (called owner) is a user relationship field. In the interface on the front end of the site, the user is supposed to be able to enable the "owner" record, which in my controller is supposed to reference the model, assign values to a few fields, and then save that record. See the code below for example:

<?php namespace My\CustomModule\Http\Controller;
use Auth;
use Input;
use Response;
use My\CustomModule\Owner\Contract\OwnerRepositoryInterface;
use My\CustomModule\Owner\OwnerModel;

class BaseController extends PublicController
{
    public function addOwnerRecord($role) {
        $newRecord = new OwnerModel;
        $newRecord->one_example_field = strtoupper(str_random(6));
        $newRecord->user = Auth::user()->id;
        //also tried this: $newRecord->user_id = Auth::user()->id;
        //also tried this: $newRecord->user = Auth::user();
        $newRecord->save();
        return Response::json(['result' => 'success']);
    }
}
?>

Here's how I'm defining the user field:

        'user' => [
            'type'   => 'anomaly.field_type.relationship',
            'config' => [
                'mode'     => 'lookup',
                'relation' => UserModel::class,
            ],
        ],

No matter what I've tried, I get this:

local.ERROR: ReflectionException: Class  does not exist in /path/to/my/pyro/site/vendor/laravel/framework/src/Illuminate/Container/Container.php:749
Stack trace:
#0 /path/to/my/pyro/site/vendor/laravel/framework/src/Illuminate/Container/Container.php(749): ReflectionClass->__construct('')
#1 /path/to/my/pyro/site/vendor/laravel/framework/src/Illuminate/Container/Container.php(644): Illuminate\Container\Container->build(NULL, Array)
#2 /path/to/my/pyro/site/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(709): Illuminate\Container\Container->make(NULL, Array)
#3 /path/to/my/pyro/site/core/anomaly/relationship-field_type/src/RelationshipFieldType.php(185): Illuminate\Foundation\Application->make(NULL)
#4 /path/to/my/pyro/site/core/anomaly/relationship-field_type/src/RelationshipFieldType.php(173): Anomaly\RelationshipFieldType\RelationshipFieldType->getRelatedModel()
#5 /path/to/my/pyro/site/storage/streams/default/models/Horses/HorsesOwnersEntryModel.php(273): Anomaly\RelationshipFieldType\RelationshipFieldType->getRelation()
#6 /path/to/my/pyro/site/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2706): Anomaly\Streams\Platform\Model\Horses\HorsesOwnersEntryModel->user()
#7 /path/to/my/pyro/site/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2679): Illuminate\Database\Eloquent\Model->getRelationshipFromMethod('user')
... (many many more but this is where it gets interesting)

So it always seems to have something to do with the user relationship but even if I comment out the line where I assign the user to the model in the controller, (in the example above) I still get the same stack trace where it's looking at the user relationship. This tells me it MUST be seeing the owner model/class. But the error doesn't say WHAT class is missing.

Any thoughts?

Answer

emergingdzns - 6 months ago

BOOM! That was it! It doesn't like us using the term user as a field name! I changed it to member in my migrations, models and such and reinstalled. No more error! You may want to note that somewhere. :-)

piterden - 6 months ago

<?php namespace My\CustomModule\Http\Controller;

use Anomaly\Streams\Platform\Http\Controller\PublicController;
use Illuminate\Support\Facades\Auth;
use My\CustomModule\Owner\Contract\OwnerRepositoryInterface;

class BaseController extends PublicController
{
    public function addOwnerRecord(OwnerRepositoryInterface $owners)
    {
        $newRecord = $owners->create([
            'one_example_field' => strtoupper(str_random(6)),
            'user'              => Auth::user(),
        ]);

        return response()->json([
            'result' => 'success',
            'created' => $newRecord,
        ], 200);
    }
}

piterden - 6 months ago

$newRecord = new OwnerModel; shouldn't work!

$newRecord = new OwnerModel(); right construction of model, but much better to use repository for it

emergingdzns - 6 months ago

I actually tried exactly that route a few minutes ago. Same error. It simply doesn't like the user relationship. I can't figure out why. Interestingly, I went to the admin panel and using the owner form builder in my module with only the user field and one text field I get the exact same error. If I take out the 'user' field definition in the $fields array of the form builder it works fine. What did I do wrong in the relationship configuration?

ryanthompson - 6 months ago

3 in there seems interesting...

3 /path/to/my/pyro/site/core/anomaly/relationship-field_type/src/RelationshipFieldType.php(185): Illuminate\Foundation\Application->make(NULL)

Are you overriding config in any way?

Does setAttribute('user_id', Auth::id()); work?

emergingdzns - 6 months ago

No config overriding at all. Just vanilla. Where would I use the setAttribute line? I've never used that. Would it be on the new model class object or on the $owners repository interface object?

emergingdzns - 6 months ago

I tried adding it to the model. I also tried just Auth::id() on both model and interface repository methods. I get the same error no matter what I try. If I remove the user relationship from the stream, it doesn't fail. I tried doing streams:compile after changing the user field definition in my migration to this:

        'user' => [
            'type'   => 'anomaly.field_type.relationship',
            'config' => [
                'mode'    => 'lookup',
                'related' => 'Anomaly\UsersModule\User\UserModel',
            ],
        ],

Still no luck. Checking the "compiled" Entry Model created for the module, I see the following:

'slug' => 'user',
'type' => 'anomaly.field_type.relationship',
'config' => 'a:2:{s:4:"mode";s:6:"lookup";s:8:"relation";s:34:"Anomaly\UsersModule\User\UserModel";}',
'locked' => '1',

and

public function user()
{
return $this->getFieldType('user')->getRelation();
}

It seems to fail on this last part where it tries to get the relationship.

emergingdzns - 6 months ago

I just compared my compiled entry model with the one from the posts module. The ONLY difference in the relationships is that in the posts module you use the word 'author' (which makes sense) but in my module I use the word 'user'. I wonder if this is a conflict in the system somehow.

emergingdzns - 6 months ago

BOOM! That was it! It doesn't like us using the term user as a field name! I changed it to member in my migrations, models and such and reinstalled. No more error! You may want to note that somewhere. :-)

ryanthompson - 6 months ago

That's weird.. so the exact same code you tried worked with member? I'll have to look into it.

emergingdzns - 6 months ago

Yep. Everything is exactly the same. Just replaced user with member.

fryiee - 6 months ago

Just to rebut, I use user for the large majority of my relationships and it works fine.