{"record":{"id":"810db022fd4349cd","repo":"getgrav/grav","slug":"object-has-to-be-type-s-s-given","errorCode":null,"errorMessage":"Object has to be type %s, %s given","messagePattern":"Object has to be type (.+?), (.+?) given","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"system/src/Grav/Framework/Flex/FlexIdentifier.php","lineNumber":66,"sourceCode":"    {\n        if (!isset($this->object)) {\n            /** @var Flex $flex */\n            $flex = Grav::instance()['flex'];\n\n            $this->object = $flex->getObject($this->getId(), $this->getType(), $this->keyField);\n        }\n\n        return $this->object;\n    }\n\n    /**\n     * @param T $object\n     */\n    public function setObject(FlexObjectInterface $object): void\n    {\n        $type = $this->getType();\n        if ($type !== $object->getFlexType()) {\n            throw new RuntimeException(sprintf('Object has to be type %s, %s given', $type, $object->getFlexType()));\n        }\n\n        $this->object = $object;\n    }\n}\n","sourceCodeStart":48,"sourceCodeEnd":72,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Framework/Flex/FlexIdentifier.php#L48-L72","documentation":"FlexIdentifier is a lightweight typed reference to a Flex object (type + key). setObject() enforces that the supplied object's flex type equals the identifier's getType(); otherwise it throws this RuntimeException. The guard keeps identifiers homogeneous so they can be resolved back through the same directory.","triggerScenarios":"Creating a FlexIdentifier from a 'pages' object then calling setObject() with a 'user' or 'shop-products' object; generic code that caches identifiers per type and reuses the wrong one; copying an identifier and swapping in an object from another directory.","commonSituations":"Generic CRUD tooling that handles multiple flex types and mixes identifier pools; refactoring that changes the type a controller works with while reusing stored identifiers.","solutions":["Check the type before assigning: if ($identifier->getType() === $object->getFlexType()) { $identifier->setObject($object); }.","Create a fresh identifier from the object instead of reusing one: FlexIdentifier::fromObject($object).","If two types must be referenced, keep two identifiers, one per type."],"exampleFix":"// before\n$identifier->setObject($userObject); // identifier typed 'pages'\n\n// after\nif ($identifier->getType() === $object->getFlexType()) {\n    $identifier->setObject($object);\n} else {\n    $identifier = FlexIdentifier::fromObject($object);\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"use Grav\\Framework\\Flex\\Interfaces\\FlexObjectInterface;\n\n/** True when $object can be attached to an identifier typed $type. */\nfunction matchesIdentifierType(string $type, FlexObjectInterface $object): bool\n{\n    return $type === $object->getFlexType();\n}","tryCatchPattern":null,"preventionTips":["Compare getType() with getFlexType() before setObject().","Prefer FlexIdentifier::fromObject($object) over mutating existing identifiers.","Keep one identifier pool per flex type to avoid cross-type reuse."],"tags":["grav","flex","type-mismatch","identifier"],"backgroundTag":"type-mismatch","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}