PhpStorm doesn't show autocomplete even after Ctrl+Space - symfony

I am learning to code with Symfony in symfonycasts.com and am using PhpStorm.
Doctrine\ORM\EntityManagerInterface service doesn't load getRepository()-> suggestions.
This is what I should see typing getRepository():
This is what I see:
Other autocomplete libraries seems to be working well.

Found one way to fix it : it's possible to hint IDE which class is that object can by using #var annotation above the variable, e.g.:
/** #var \App\Repository\QuestionRepository $repository */
$repository = $entityManager->getRepository(Question::class);
$repository-> // now you should have autocomplete here :)

Phpstorm seems to have issue with the EntityManagerInterface, when I use the specific type EntityManager instead of interface, the suggestions start working again.

Related

How Rollerworks/strength-validator setup

Hi I'm really new with symfony and the idea of this is kinda new to me specially how the framework behaves. I'm using Symfony 2.8 (due to project demand). Now I need to implement a password strength validator using RollerWorks/strength-password-validator now the instruction here is not clear and I don't know where to start. I even don't know what the anotation mean. There are some sample that make use of this validation but is using an yml file just like THIS
It's just not clear on how do I start with this. I added
new Rollerworks\Bundle\PasswordStrengthBundle\RollerworksPasswordStrengthBundle(),
in the AppKernel under the registerBundles function. Then on the UserBundle/Entity under User.php file I added this
use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
/**
* #RollerworksPassword\PasswordStrength(minLength=7, minStrength=3)
*/
protected $password;
But upon registration nothing happens. It still accept even if I use less than 7 password strength even the simplest password does not work. Any idea on this would be very helpful.
Thanks

Doctrine lifecycleCallbacks strange behaviour

I have defined lifecycleCallbacks in yaml as follows:
lifecycleCallbacks:
prePersist: [setCreatedAtValue]
preUpdate: [setUpdatedAtValue]
The above has generated entities with the respective functions as follows:
/**
* #ORM\PrePersist
*/
public function setCreatedAtValue()
{
if($this->created_at == null)
{
$this->created_at = new \DateTime();
}
}
Which looks all fine, right? However, when I try to open the sonata admin page, I get the following error
[Semantical Error] The annotation "#ORM\PrePersist" in method AppBundle\Entity\Article::setCreatedAtValue() was never imported. Did you maybe forget to add a "use" statement for this annotation?
I have never encountered this before and a bit confused about what to do. I am using symfony 2.7.6, Doctrine ORM version 2.5.1, Sonata Admin 2.3.7
Any help will be greatly appreciated
Since you defined your callbacks using yaml, you don´t need to define them again using annotations. Just remove the comments with the #ORM\PrePersist block before the function and everything will be fine.
If you wanted to use annotations to define your doctrine properties, you would need to import them before you can use them. To do so you would need to add this line at the beginning of your file:
use Doctrine\ORM\Mapping as ORM;
Same issue came with me.
In my case everything worked well until I did not Serialize my object in JsonResponse.
So problem was that previously I was not using that entity class (which was giving error) for sending JsonResponse, as soon as I tried to prepare JsonResponse containing that class, JsonResponse failed to serialize my class as it hadn't implemented Serializable interface.
This will happen if you will fetch objects with methods like findAll or findBy which returns Entity objects instead of php array.
So I just skipped those native methods and write doctrine query for fetching data.
You can also implement Serializable interface.

What is the #var annotation for in a Doctrine entity in Symfony?

Perhaps a silly question but it bugs me that I don't know ...
When I create an entity with app/console doctrine:generate:entity it adds an #var annotation to each property. What is #var used for?
It obviously indicates the data type but I don't see it mentioned in any documentation and things seems to work whether it's there or not so I just wonder what uses it.
This is a standard phpdoc comment. It has nothing to do with Doctrine annotations, and they are ignored by Doctrine. They are used when generating documentation and also as metadata for IDE's that support code completion.

How to auto complete methods from Symfony 2 DI in netbeans

I am starting to develop with symfony 2 and it uses a lot dependency injection. I would like to know if is there any way that makes netbeans detect the type of object based on the string and auto complete with their methods?
For example, $this->container->get('doctrine') returns a Doctrine\Bundle\DoctrineBundle\Registry instance. In the container, the key doctrine corresponds to Doctrine\Bundle\DoctrineBundle\Registry.
Something like it, could be useful for zendframework 2 also.
I don't want to create new methods in the controller and nor use /* #var $var Symfony...*/, I would automatic detection.
As far as I know, there's no way for an IDE to detect the type of the object your container returns. My solution is to wrap those calls to the container into private getter functions. IMHO this improves code readability as well – especially, if you do this call more than once per class.
/**
* #return \Doctrine\Bundle\DoctrineBundle\Registry
*/
private function getDoctrine()
{
return $this->container->get('doctrine');
}
The IDE "PhpStorm" permits to suggest "use" declarations.
And this IDE propose specific features for Symfony2 and Drupal !
edited by JetBrains : http://www.jetbrains.com/phpstorm/
Not free but power full enough to reduce time developpement time (and time is money...)
Enjoy : )
phpStorm:
$foobar= $this->get('foobar'); //returns mixed
/* #var \MyNamespace\FooBar $foobar*/
or
$foobar= $this->get('foobar'); //returns mixed
/* #var FooBar $foobar*/
You can do this with eclipse PDT:
$foobar= $this->get('foobar'); //returns mixed
/* #var $foobar \MyNamespace\FooBar*/
( Walk around ) When comes to Symfony services:
Instead of
$doctrine = $this->container->get('doctrine');
use
$doctrine = $this->getDoctrine();
As you can see, Symfony allows you to access most of it services directly from $this variable. NetBeans will know what auto completion to use.
Lets have a look why this works (inside Controller class)
It is possible because Controller class imports Registry class with USE statement,
use Doctrine\Bundle\DoctrineBundle\Registry;
and then in method comment annotation it declares the returning object type with
/*
* #return Registry
*/
If you call $this->container->get('doctrine'); directly then auto completion will be get omitted and you will have to use whats below.
( Answer ) No magic auto completion works so far. Use Php Storm (it does what you request). For those who pick to stick with NetBeans you need to use manual annotation like in example below:
We can point NetBeans to a class it should be using for auto completion.
1) In terminal from project directory search for service you want to import:
php bin/console debug:container
If you know what you looking for use this instead:
php bin/console d:container | grep doctrine
...
doctrine --------------------------------------------------------
Doctrine\Bundle\DoctrineBundle\Registry
...
2) If this is not a service use get_class() PHP build in function to get class name of the object it particular variable. Or use reflection class. It's up to you.
3) Once you know the class name declare USE statement for better readability
use Doctrine\Bundle\DoctrineBundle\Registry;
4) Now wen we know what is the class name of the object instance in particular variable we are ready to inform NetBeans about what we know by using comment annotations so that it can enable auto completion.
/**
* #var $doctrine Registry
*/
$doctrine = $this->container->get('doctrine');
Now auto completion is enabled. Type
$doctrine->|
then press Ctrl+Space. See the image below:

Doctrine entity field not recognized at all

I am working with FOSUserBundle to authenticate. The authentication worked, so far so good. I have a couple of extra fields which populated nicely.
I then added a Roles-table (with the according entity), where the id-field is referenced by a roleid field in the database.
However, this field is not recognized by doctrine at all. Even
php app/console doctrine:schema:update --dump-sql
doesn't show it! Even when I rename my current table and then try to generate a create table-query, the field doesn't show up. I also tried renaming the field, but to no luck.
My field (in the class, of course):
/*
* #var int $roleid
*
* #ManyToOne(targetEntity="Roles")
* #JoinColumn(name="roleid", referencedColumnName="id")
*/
protected $roleid;
If anyone could point me in the right direction of what is going on, I would greatly appreciate it.
Use
/**
instead of
/*
otherwise the annotations will not be recognized
You have to follow the exact syntax
Use /** and not /*

Resources