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
];
}
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.
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:
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).
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.
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 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!