Has_one relationship

Silverstripe Version: 4.9

Question:

So I have a has_one relationship with the Member class being extended.
I think everything is declared properly but I have an error.

Emergency] Uncaught Exception: No has_one found on class ‘App\Hub\PostObject’, the has_many relation from ‘SilverStripe\Security\Member’ to ‘App\Hub\PostObject’ requires a has_one on ‘App\Hub\PostObject’

GET /admin/security/EditForm/field/Members/item/2/edit

Line 1097 in /var/www/unlabelledgirls/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php

Details of your query go here

<?php

namespace App\Hub; 

use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Member;

class PostObject extends DataObject {
        
    private static $db = [
        'Title' => 'Varchar',
        'EmbedCode' => 'Varchar',
        'IsActive' => 'Boolean(0)'
    ];

    private static $has_one = [
        'Member' => Member::class 
    ]; 

    public function getCMSFields(){
        $fields = parent::getCMSFields();        
        return $fields;
    } 
} 

AND THE OTHER OBJECT THAT EXTENDS MEMBER

<?php

use SilverStripe\Security\Permission;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;

class MemberExtension extends DataExtension 
{
       
    private static $db = [
        'Country' => 'Varchar(64)'
    ]; 

    private static $has_many = [
       'Posts' => App\Hub\PostObject::class
    ];

}







1 Like

I see no issues in the code you posted, so I’m giving you the usual checklist.

  • Did you run /dev/build?flush after adding that relation?
  • Did the above command run without error?
  • Is there a MemberID field in the PostObject database table?

Ok so, troubleshooting:

  • yes I ran /dev/build?flush multiple times
  • the build doesn’t trigger any error, so I assume the database side is ok
  • yes, there’s a MemberID field in the PostObject though it appears as App_Hub_PostObject

I was wondering if the fact that the relation is being done on the DataExtension could be the issue

Yes, that’s correct: your PostObject is inside the App\Hub namespace.

My next guess is your code is correct but that error is triggered by old code, most likely because of a PHP cache somewhere (e.g. OpCache). This is annoying as hell and not specific to SilverStripe: my development station is carefully configured to avoid any caching.

How to clear that cache depends on your current configuration: if you are running php-fpm usually restarting it (IIRC reloading should be enough) fixes the problem. On recent linux systems this can be done with systemctl restart php-fpm or something similar. If that is the case, you should see random problems come up because you are mixing old code with new code.

Thanks for the insight, so I disabled OPcache in php.ini, restarted apache, relaunched dev/build, still no error during built but the issue is not solved. The same error when accessing the admin and trying to create an object.

Seems that it’s not installed, I don’t remember going through an installation of php-fpm either

That’s fine if you are using mod_apache, and this seems to be the case. It was just a wild guess.

Did you restart php-fpm?

Sorry but there is an elephant hidden somewhere here. To be sure, I copy-pasted the code you posted here in a project (with minor adaptations) and all is working as expected:

Ha, wait a sec, PostObject.php is in chmod 604 so not executable, will try with at least 705

A little clarification: every PHP file must be readable by whoever should parse it, typically the web server (www-data or http on linux server) and the user launching sake or php (if using the command line).

1 Like

Another wild guess is you are running /dev/build?flush from the command line but the error is from the web. I think this is (at least theoretically) possible if your PostObject.php file is readable by the CLI user and not by apache, but this would be a really sick environment.

1 Like

So I changed the permission to chmod 777 just to check quick and it worked then set to 755 and it seems to work properly. I’ve create a new post object, it’s directly linked to the member and shows up properly in the member “posts” tab.

Thanks for the wild guess, it solved the issue.

Thanks for all this information, I’m on my first VPS so it’s the first time I deal with all the libraries and permissions from scratch. Your help is precious, you saved the day!