How can I declare many of classes in one line on my Controller? - laravel-5.7

I would declare them with the star * on MyController
use App\Client;
use App\Cabinet;
use App\Giac;
...

You cannot use * wildcard with PHP namespace imports. But if you really need an one-liner, you could group them like this:
use App\{Client, Cabinet, Giac};

Related

How to rename Entity and attribute in symfony

I just want to rename "EntityName" to something else the most easily possible, with the database entry. Also how can I rename an Attribute?
So, I have an Entity called "EntityName" (for exemple) in /appname/src/Entity/EntityName.php like this:
<?php
namespace App\Entity;
use App\Repository\EntityNameRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=EntityNameRepository::class)
*/
class EntityName
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
[...]
When I try to rename the name of the class and try php bin/console doctrine:schema:update --dump-sql or --force I get this error:
In EntityName.php line 11:
Compile Error: Cannot declare class App\Entity\NewName, because the name is already in use
(I think I cannot rename beaucause it check itself if it already exist.)
I'd advise caution, specially if it's a production environment.
doctrine:schema:update --force (or doctrine:migrations:diff or make:migration, for that matter) doesn't detect changes so it will drop your existing table (along with all your data) and create a new one.
The safest way to preserve data would be to hand-create a migration with the ALTER TABLE ... RENAME. You can also use the #Table(name="old_table_name") annotation to preserve the original name, although this might be a bit confusing when looking at the database alone.
Depending on your IDE you might need more or less elbow grease. Chances are that the following will be handled correctly:
Both the entity and repository class name and file name, making them match (even in the caseName).
The entity class name in the repository constructor.
The repository class name in the repositoryClass annotation of the entity.
targetEntity annotations in relations.
There will be the property names themselves in the related classes (and JoinTableand mappedBy / inversedBy if applicable) left to change, if you wish to keep things consistent. If so, you would have to add the needed statements to the migration as well, and you might need to disable the constraints in the process. --dump-sql might be helpful on hinting what tables need renaming, but you won't be able to use the sql straight up.
The easiest way, if you don't care about your data, would be doctrine:schema:update --force indeed.
If you want to rename the entity means you have to change both Entity and Repository Class name and file name too. Then have to change Both classes import statements properly. After that just run this comment bin/console c:c.
If you want to rename an entity, you only need to change its class name AND file name to make it match. Also make sure the namespace match file path.
If all these things are right, then make sure you don't have another file which would contain the same namespace and class name you are trying to use for your "NewName" class
If you still get the error message, then you might want to clear you cache by running php bin/console c:c
Lastly, if it still fails, you might want to dump the autoload by running composer dump-autoload (or if you are using Phpstorm: tools > composer > dump autoloader)

How to get missing translations/messages in Symfony 4, in a controller?

My wish : extract missing translations/messages in Symfony 4, in a controller.
I know it's possible, using command lines.
Extract missing translations with command lines
All my translations come from my templates.
I tried to load the object TranslatorInterface
Exemple :
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
public function index(TranslatorInterface $translator)
{
dump($translator);
}
If you have an idea, a bundle to purpose ?
Thanks
you are looking for https://github.com/php-translation/symfony-bundle I guess?
This brings many additional translation functions like an dedicated editor,
many commands AND the needed extractors.
One culprit: It only supports xliff format, but existing yaml can be converted easily.

Doctrine: Custom repository method with inheritable entity

I have a base entity class called User, which has 3 possible different child-classes Employee, Customer and Student. This is implemented with a single table inheritance.
The base class User has two fields called nameand prename, which all the child classes inherit.
Now my problem: I have a custom repository called UserRepository with the method findByFullTextSearch(string $searchterm): User[] in which I implemented a somewhat Full-Text-Search on those two fields to fit my needs (unfortunately, I do not have the possibility to use DoctrineExtensions for this project to implement the MySQL function FIND_IN_SET or an actual Full-Text-Search).
Is there any possibility for me to only write this method once in UserRepository, so when I use $doctrine->getRepository(Student::class)->findByFullTextSearch('john doe'); I only get results fitting to the Repository i called it from?
My workarounds so far:
Implement all repository classes, pass get_called_class() or $this->_entityName to parent class.
Call the base repository class and pass the child entity class as parameter to findByFullTextSearch
Here I found a related question, but not quite the same.
Is there a better solution to this? Thanks in advance.
I just found the solution by simply adding the base class as repository for each child entity:
<?php
namespace UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* UserBundle\Entity\Customer.
*
* #ORM\Entity(repositoryClass="UserBundle\Repository\UserRepository")
*/
class Customer extends User
{
// ...
This way I wont need empty child repositories just for doctrine to find a possible entry point.

Extbase: inject repository into domain model

I am currently working with Typo3 6.2.10 and Extbase.
I am trying to inject a repository into my domain model like this:
class MyModel extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
/**
* #inject
* #var \Vendor\Package\Domain\Repository\SomeRepository
*/
protected $someRepository;
}
However, $this->someRepository is always null. Injecting repositories into controllers always works though.
Thank you in advance!
Keep in mind, that you must omit the first \ in the class name when useing the $this->objectManager->get(xxx); or this line will throw an exception in typo3 7.x+.
$this->objectManager->get('Vendor\Package\Domain\Model\MyModel');
Also the Backslash is an escape character, so it is safer to escape the backslash or just use the static constant of the class
escaped:
$this->objectManager->get('Vendor\\Package\\Domain\\Model\\MyModel');
Using the static class name:
$this->objectManager->get(\Vendor\Package\Domain\Model\MyModel::class);
I prefer to use the last method, since you can see if you did a typo (depends on the IDE)
Sorry, I found the solution myself.
I tried to instantiate the model using new keyword which (ofc) doesn't work.
I had to use $this->objectManager->get('\Vendor\Package\Domain\Model\MyModel');
instead.

add your own library in a symfony project

I asked a similar questions before but had no right answers. Maybe I did not explain good.
I have 2 classes of my own that I would like to add in symfony project.
seculibs/collections/A.php and seculibs/collections/B.php (it is not on github, it is local).
I would like to add my seculibs folder in the vendor folder and ask symfony to load the classes.
Obviously everything happens in app/autoload.php
I thought that doing this would be okay but not :
$loader->add('seculibs/collections',__DIR__ . '/../vendor/seculibs/collections/');
it still cannot find the classes.
Can somebody tell me exactly what to do? I am with Symfony 2.3.6
Please do not refer to composer.json neither to the UniversalClassLoader and its registerNamespaces method as this is not the class I am dealing with in this autoload.php
So I found the right syntax... It will help some others
$loader->add('seculibs\\collections',__DIR__ . '/../vendor');
Do you HAVE to autoload the classes? I have used my own classes by simply giving them a namespace in my project, and then including them with a use statement in the file that I would like to use them in. For example, I have a class that I put in a custom location within my project, src/My/AppBundle/Utils/MyClass.php, and within the MyClass.php file, I gave the namespace My/AppBundle/Utils
Now in a controller, I include that class with a normal use statement.
use My/AppBundle/Utils/MyClass.php
// .....
class MyController extends Controller {
// ....
}
This way I can use my class anywhere in my application by referencing the namespace. I know this approach is different from what you may have expected, but it works great.
UPDATE:
It doesn't matter where you put the files. If you want them in the vendor folder, put them there, and give them a namespace that makes sense.
So maybe you have something like this:
// in vendors/MyUtils/Classes/MyClass.php
namespace MyUtils/Classes
class MyClass {
// ....
}
And then you can still include the class with a use statement like I mentioned.
Look at vendor/composer/ClassLoader.php
You can see that the add method expects a prefix as the first argument. You can leave it blank if your classes have no namespace.
Assuming your classes are not namespaced then use:
$loader->add(null,__DIR__ . '/../vendor/seculibs/collections/');
$a = new \A();
If they do have a namespace then just pass the prefix of your namespace to add.
$loader->add('Cerad',__DIR__ . '/../vendor/seculibs/collections/');
namespace Cerad\Collections;
class A {}
$a = new Cerad\Collections\A();
Of course you also have to have the Cerad\Collections directory structure under your vendor directory.
==============================================
Update
If you have in /vendor/seculibs/collectionsA.php:
namespace seculibs\collections;
class A {
Then use:
$loader->add('seculibs',__DIR__ . '/../vendor/');
And
use seculibs\collections\A;
$a = new A();

Resources