Using Doctrine Annotations for creating custom annotations fails
up vote
0
down vote
favorite
I'm trying to define and use custom annotations but I can't figure out the problem. The basis is doctrine/annotations and these are my own classes:
<?php
namespace MyCompanyAnnotationsAnnotation;
use DoctrineCommonAnnotationsAnnotation;
/**
* @Annotation
* @Target("PROPERTY")
*/
final class Type
{
/**
* @Required
*
* @var string
*/
public $name;
}
<?php
namespace MyCompanyAnnotations;
use MyCompanyAnnotationsAnnotation as MYC;
class Person
{
/**
* @MYCType(name = "string")
*/
private $firstName;
/**
* @MYCType(name = "string")
*/
private $lastName;
public function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
}
Now I want to read the annotations of all properties:
<?php
require __DIR__ . '/../vendor/autoload.php';
use DoctrineCommonAnnotationsAnnotationReader;
use DoctrineCommonAnnotationsCachedReader;
use DoctrineCommonCacheArrayCache;
use MyCompanyAnnotationsPerson;
$refClass = new ReflectionClass(Person::class);
$props = $refClass->getProperties();
foreach ($props as $prop) {
$reader = new AnnotationReader();
$annotationReader = new CachedReader(
$reader, new ArrayCache()
);
$annotations = $annotationReader->getPropertyAnnotations(
$prop
);
print_r($annotations);
}
If I run my test script, it fails with the following error:
Uncaught exception 'DoctrineCommonAnnotationsAnnotationException' with message '[Semantical Error] The annotation "@MyCompanyAnnotationsAnnotationType" in property MyCompanyAnnotationsPerson::$firstName does not exist, or could not be auto-loaded.'
What confuses me is the fact that the class name in the error message starts with a '@' character.
php annotations
add a comment |
up vote
0
down vote
favorite
I'm trying to define and use custom annotations but I can't figure out the problem. The basis is doctrine/annotations and these are my own classes:
<?php
namespace MyCompanyAnnotationsAnnotation;
use DoctrineCommonAnnotationsAnnotation;
/**
* @Annotation
* @Target("PROPERTY")
*/
final class Type
{
/**
* @Required
*
* @var string
*/
public $name;
}
<?php
namespace MyCompanyAnnotations;
use MyCompanyAnnotationsAnnotation as MYC;
class Person
{
/**
* @MYCType(name = "string")
*/
private $firstName;
/**
* @MYCType(name = "string")
*/
private $lastName;
public function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
}
Now I want to read the annotations of all properties:
<?php
require __DIR__ . '/../vendor/autoload.php';
use DoctrineCommonAnnotationsAnnotationReader;
use DoctrineCommonAnnotationsCachedReader;
use DoctrineCommonCacheArrayCache;
use MyCompanyAnnotationsPerson;
$refClass = new ReflectionClass(Person::class);
$props = $refClass->getProperties();
foreach ($props as $prop) {
$reader = new AnnotationReader();
$annotationReader = new CachedReader(
$reader, new ArrayCache()
);
$annotations = $annotationReader->getPropertyAnnotations(
$prop
);
print_r($annotations);
}
If I run my test script, it fails with the following error:
Uncaught exception 'DoctrineCommonAnnotationsAnnotationException' with message '[Semantical Error] The annotation "@MyCompanyAnnotationsAnnotationType" in property MyCompanyAnnotationsPerson::$firstName does not exist, or could not be auto-loaded.'
What confuses me is the fact that the class name in the error message starts with a '@' character.
php annotations
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to define and use custom annotations but I can't figure out the problem. The basis is doctrine/annotations and these are my own classes:
<?php
namespace MyCompanyAnnotationsAnnotation;
use DoctrineCommonAnnotationsAnnotation;
/**
* @Annotation
* @Target("PROPERTY")
*/
final class Type
{
/**
* @Required
*
* @var string
*/
public $name;
}
<?php
namespace MyCompanyAnnotations;
use MyCompanyAnnotationsAnnotation as MYC;
class Person
{
/**
* @MYCType(name = "string")
*/
private $firstName;
/**
* @MYCType(name = "string")
*/
private $lastName;
public function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
}
Now I want to read the annotations of all properties:
<?php
require __DIR__ . '/../vendor/autoload.php';
use DoctrineCommonAnnotationsAnnotationReader;
use DoctrineCommonAnnotationsCachedReader;
use DoctrineCommonCacheArrayCache;
use MyCompanyAnnotationsPerson;
$refClass = new ReflectionClass(Person::class);
$props = $refClass->getProperties();
foreach ($props as $prop) {
$reader = new AnnotationReader();
$annotationReader = new CachedReader(
$reader, new ArrayCache()
);
$annotations = $annotationReader->getPropertyAnnotations(
$prop
);
print_r($annotations);
}
If I run my test script, it fails with the following error:
Uncaught exception 'DoctrineCommonAnnotationsAnnotationException' with message '[Semantical Error] The annotation "@MyCompanyAnnotationsAnnotationType" in property MyCompanyAnnotationsPerson::$firstName does not exist, or could not be auto-loaded.'
What confuses me is the fact that the class name in the error message starts with a '@' character.
php annotations
I'm trying to define and use custom annotations but I can't figure out the problem. The basis is doctrine/annotations and these are my own classes:
<?php
namespace MyCompanyAnnotationsAnnotation;
use DoctrineCommonAnnotationsAnnotation;
/**
* @Annotation
* @Target("PROPERTY")
*/
final class Type
{
/**
* @Required
*
* @var string
*/
public $name;
}
<?php
namespace MyCompanyAnnotations;
use MyCompanyAnnotationsAnnotation as MYC;
class Person
{
/**
* @MYCType(name = "string")
*/
private $firstName;
/**
* @MYCType(name = "string")
*/
private $lastName;
public function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
}
Now I want to read the annotations of all properties:
<?php
require __DIR__ . '/../vendor/autoload.php';
use DoctrineCommonAnnotationsAnnotationReader;
use DoctrineCommonAnnotationsCachedReader;
use DoctrineCommonCacheArrayCache;
use MyCompanyAnnotationsPerson;
$refClass = new ReflectionClass(Person::class);
$props = $refClass->getProperties();
foreach ($props as $prop) {
$reader = new AnnotationReader();
$annotationReader = new CachedReader(
$reader, new ArrayCache()
);
$annotations = $annotationReader->getPropertyAnnotations(
$prop
);
print_r($annotations);
}
If I run my test script, it fails with the following error:
Uncaught exception 'DoctrineCommonAnnotationsAnnotationException' with message '[Semantical Error] The annotation "@MyCompanyAnnotationsAnnotationType" in property MyCompanyAnnotationsPerson::$firstName does not exist, or could not be auto-loaded.'
What confuses me is the fact that the class name in the error message starts with a '@' character.
php annotations
php annotations
asked Nov 8 at 12:54
altralaser
6581630
6581630
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You're missing a call to register the annotations (both yours and any internal ones you're using). The easiest way to do this is to pass the Composer autoloader directly to Doctrine's registerLoader method:
$loader = require_once 'vendor/autoload.php';
DoctrineCommonAnnotationsAnnotationRegistry::registerLoader([$loader, "loadClass"]);
at the top of your test script.
This assumes that you're doing all of your autoloading through Composer but if not, there are methods available to register individual files or namespaces described here in the manual
Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
– altralaser
Nov 8 at 13:33
@altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
– iainn
Nov 8 at 13:44
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You're missing a call to register the annotations (both yours and any internal ones you're using). The easiest way to do this is to pass the Composer autoloader directly to Doctrine's registerLoader method:
$loader = require_once 'vendor/autoload.php';
DoctrineCommonAnnotationsAnnotationRegistry::registerLoader([$loader, "loadClass"]);
at the top of your test script.
This assumes that you're doing all of your autoloading through Composer but if not, there are methods available to register individual files or namespaces described here in the manual
Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
– altralaser
Nov 8 at 13:33
@altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
– iainn
Nov 8 at 13:44
add a comment |
up vote
0
down vote
accepted
You're missing a call to register the annotations (both yours and any internal ones you're using). The easiest way to do this is to pass the Composer autoloader directly to Doctrine's registerLoader method:
$loader = require_once 'vendor/autoload.php';
DoctrineCommonAnnotationsAnnotationRegistry::registerLoader([$loader, "loadClass"]);
at the top of your test script.
This assumes that you're doing all of your autoloading through Composer but if not, there are methods available to register individual files or namespaces described here in the manual
Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
– altralaser
Nov 8 at 13:33
@altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
– iainn
Nov 8 at 13:44
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You're missing a call to register the annotations (both yours and any internal ones you're using). The easiest way to do this is to pass the Composer autoloader directly to Doctrine's registerLoader method:
$loader = require_once 'vendor/autoload.php';
DoctrineCommonAnnotationsAnnotationRegistry::registerLoader([$loader, "loadClass"]);
at the top of your test script.
This assumes that you're doing all of your autoloading through Composer but if not, there are methods available to register individual files or namespaces described here in the manual
You're missing a call to register the annotations (both yours and any internal ones you're using). The easiest way to do this is to pass the Composer autoloader directly to Doctrine's registerLoader method:
$loader = require_once 'vendor/autoload.php';
DoctrineCommonAnnotationsAnnotationRegistry::registerLoader([$loader, "loadClass"]);
at the top of your test script.
This assumes that you're doing all of your autoloading through Composer but if not, there are methods available to register individual files or namespaces described here in the manual
edited Nov 8 at 13:31
answered Nov 8 at 13:16
iainn
11.8k81929
11.8k81929
Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
– altralaser
Nov 8 at 13:33
@altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
– iainn
Nov 8 at 13:44
add a comment |
Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
– altralaser
Nov 8 at 13:33
@altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
– iainn
Nov 8 at 13:44
Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
– altralaser
Nov 8 at 13:33
Perfect! That was the trick. Is there also a possibility to edit the composer.json to load the classes on "composer update" (maybe with a phing task) or is it only possible on top of an init application script?
– altralaser
Nov 8 at 13:33
@altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
– iainn
Nov 8 at 13:44
@altralaser Nothing I know about, sorry. I think this has to be done at runtime. Any project I've worked with that's used annotations has just included the extra line after including the Composer autoloader.
– iainn
Nov 8 at 13:44
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208170%2fusing-doctrine-annotations-for-creating-custom-annotations-fails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown