In the symfony2 and doctrine 2. I have this
/**
* Find a user by its username.
*
* #param string $username
* #return UserInterface or null if user does not exist
*/
function findUserByUsername($username);
Now i want to know that does it solve nay purpose or does it do anything in database with those #param and #return annotation or they just for documentation
#param is explained in the phpDocumentor docs. It has nothing to do with Symfony2.
The link is outdated.
Here is a link to a good address at this present moment.
https://www.phpdoc.org/docs/latest/references/phpdoc/tags/param.html
Below, the first lines of the explanation in any case...
The #param tag is used to document a single argument of a function or method.
With the #param tag it is possible to document the type and function of a single argument of a function or method. When provided it MUST contain a Type to indicate what is expected; the description on the other hand is OPTIONAL yet RECOMMENDED in case of complicated structures, such as associative arrays.
Related
Using API Platform 1.2.
I've simplified my setup for the purpose of this question. Please excuse lack of following standards.
I have 2 entities: Book and Category. Book properties:
/**
* #Groups({book:read})
*/
$name;
/**
* #Groups({book:read})
*/
$summary;
/**
* #Groups({book:read})
*/
$category;
The $category property is mapped to a Category entity. Category has a $categoryName property. This is also attached to the book:read group.
In the GET API call this the output contains all the Book properties plus the $categoryName property. This is great 👍
What I want to know is: Using API Platform, how would I go about getting all properties for a serialization group such as above?
I have found that I could tap into the \ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface to get the property info but this requires knowing what classes and properties to check. Looping through all entities with a metadata lookup, then looping through all properties, performing another metadata lookup seems wasteful and slow.
Is there a better way to acheieve this? I basically want the same output as what the API produces by feeding am entity name and group name.
Here is the simple symfony Route:
/**
* #Route("/test/{param}", requirements={"param": "(one|two)"})
*/
But how can I set dynamic requirements from the array or entity feature like:
/**
* #Route("/test/{param}", requirements={"param": "array or entity"})
*/
p.s. the problem appears about the same Routes like /products/{vendors} and /products/{models}. I want to catch valid route by the requirements of the route.
thanks in advance)
You cannot do that with requirements as they are just regular expressions, so they cannot detect things like the type of the parameter. In fact I believe there is no way to achieve what you want to do: how would you "send" an entity or an array in a URL? If you are using parameter converters, then the requirements should refer to the original, not converted, URL
I beleive next annotation will work:
/**
* #Route("/test/{param}", requirements={"param" = "one|two"})
*/
I have a an entity with an ID as such:
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
I'm migrating data into this entity, and want to preserve existing keys. I looked at "Explicitly set Id with Doctrine when using "AUTO" strategy" and found that I should be able to do the following:
$newData = ... // array containing data to bring in
$newEntity = new MyEntity();
$newEntity->setId($newData['id']);
$newEntity->... // set other data fields
$em->persist($newEntity);
$metadata = $em->getClassMetadata('\CS\AcmeBundle\Entity\MyEntity');
$metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
$em->flush();
However, Doctrine is not using the provided ID. It's ignoring it when inserting. I've also tried this approach instead, since some people seemed to have had luck with it (even tried both):
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
But that doesn't change anything. ID's are still inserted automatically by the database. In the query log, I see that Doctrine isn't even attempting to insert the ID.
If I remove #ORM\GeneratedValue(strategy="AUTO") from MyEntity annotations, then the migration will respect the provided ID I give it. But I want to override it just during the migration.
I'm using Doctrine 2.4.2.
For this technique to work, you must use the second of these:
$metadata = $em->getClassMetadata('\CS\AcmeBundle\Entity\MyEntity');
$metadata = $em->getClassMetadata('CS\AcmeBundle\Entity\MyEntity');
The problem is that Doctrine will return the same class meta data values for both.
They will both correctly identify the class file, read its annotations, etc. Obviously they are equivalent, except that one is an absolute namespace and the other is not.
But these strings will return different instances from getClassMetadata. Changes to one won't reflect in the other. If you want your intended technique to work, you must use the second form, because that is what UnitOfWork uses. It uses this normalization:
// \Doctrine\ORM\UnitOfWork->getCommitOrder()
...
$className = $this->em->getClassMetadata(get_class($entity))->name;
$class = $this->em->getClassMetadata($className);
...
Note that in the linked-to question, the solution uses get_class($entity). That is probably sufficient to get the correct behavior.
Even more detail: after a lot of stepping through code, I noticed that \Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory was memoizing both versions of the class name string in its private property $loadedMetadata. The version that was being used to actually flush the entities was the one without the leading slash, and I was editing the one with the leading slash.
Because both strings return the same data, I think this represents a bug in the implementation.
The differences between GeneratedValue strategies
Inside your entity
Replace
#ORM\GeneratedValue(strategy="AUTO")
with
#ORM\GeneratedValue(strategy="NONE")
I am not sure whether you are using annotations or xml, or yml files. So better to change the xml or yml doctrine entity files inside your bundle config as well.
i may have little be too general questions and if there are i am sorry for duplicates. (did not found the answer i was looking for).
First of all i am using symfony2 framework for developing and i am thinking about purchasing phpStorm. Its not a cheap program and i dont want to waste money. But i like it to now so i would like to ask if its a good step to future?
And my second and the main question
Sometimes happens i dont see auto-completed suggestions of methods of some objects...
Is there any way or some good practises i can do to help phpStorm understand with what object i am dealing with?
I have on my mind things like i saw in the code of someone else:
public function updateSomething(User $user
Or...
/**
* Draws an arc on a starting at a given x, y coordinates under a given
* start and end angles
*
* #param Imagine\Image\PointInterface $center
* #param Imagine\Image\BoxInterface $size
* #param integer $start
* #param integer $end
* #param Imagine\Image\Color $color
* #param integer $thickness
*
* #throws Imagine\Exception\RuntimeException
*
* #return DrawerInterface
*/
Do these things help and I should write them or there are just for better "user-read ability"?
Your first question is of a type that normally belongs outside of StackOverflow as its very subjective, there isn't really an answer, only people's opinions. For what it's worth, in my opinion PHPStorm is worth every penny compared to either a) using no IDE / a text editor, b) using the free Aptana IDE (eclipse based) which are the alternatives I tried before PHPStorm. For me there there are numerous productivity savings but one invaluable feature is the debugger which just works (once you have XDebug configured with PHP).
To answer your second question about the auto-completion of methods of some objects - Yes, both the examples you gave help and those along with additional use of PHPDoc comments should enable the PHPStorm method auto-completion to work in almost all cases. This should also have the side-effect of clearing out most of the spurious PHPStorm yellow highlights of undefined method calls so that genuine errors are more visible.
The least you can do to maximise method auto-completion:
1. Always provide type hints for method parameters where possible / appropriate - see PHP Type Hinting for more details e.g.
public function createUser (UserDetails $userDetails, array $roles)
2. Always give methods that have a return value PHPDoc comments (beginning /**) which at least define the #return type for the method (if not mixed) and the content type of any array parameters (if not mixed). e.g.
/**
* #param UserDetails $userDetails
* #param Role[] $roles
* #return User
*/
3. Use PHPDoc comments with #var to define the type of class member variables and local variables e.g.
class UserManager
{
/**
* #var EntityManager
*/
private $entityManager
...
public function doSomething()
{
...
/** #var User $user */
foreach($users as $user)
{
...
}
...
It is good practice generally to use PHP type hinting and to make appropriate use of PHPDoc.
I have a Duo entity containing two persons. During the creation, the creator submits the email address partner and decides whether he is the captain or not of the new Duo. If he decides that he isn't (he choose the be the assistant) then, automatically, the second person is assigned captain. My Duo entity is as follows:
/**
* #ORM\Table()
* #ORM\Entity()
*/
class Duo{
/**
* #Assert\NotNull()
* #ORM\OneToOne(targetEntity="User")
* #ORM\JoinColumn(name="captain_id", referencedColumnName="id")
*/
private $captain;
/**
* #Assert\NotNull()
* #ORM\OneToOne(targetEntity="User")
* #ORM\JoinColumn(name="assistant_id", referencedColumnName="id")
*/
private $assistant;
I wonder how to organize my form so that there is only an email address that is submitted designating the partner. When I call $form->isValid(), I want my Duo entity to be valid. I do not want to use a form without entity because I do not want to have to assign data manually and handle errors outside of the form. Also, I would like to verify if one of the two users aren't involved in another Duo. If yes, how to pass the error message to the form in a clean way.
What should I do? Is there a way to do it using the Form Callbacks or Event or something like that?
Data transfers can do everything you want:
http://symfony.com/doc/current/cookbook/form/data_transformers.html
I suggest reading this very carefully, because it got very confusing the first time I did. There is a perfect example here that explains how to build issue id to Issue, basically same is your case.
As for the errors, I think your best option is to use Callback in combination with form constraints attribute:
http://knpuniversity.com/screencast/question-answer-day/custom-validation-property-path#creating-a-proper-custom-validation-constraint
You should probably read the whole article as there is not only "proper" way but "quick and dirty" as well.
Hope this helps.