FOS UserBundle how to create own User Repository - symfony

i stack to create my own FOS UserBundle UserRepository.
I guess its not correct extended?
app/Resources/FOSUserBundle/Repository/UserRepository.php:
<?php
namespace FOS\UserBundle\Repository;
/**
* UserRepository
*/
class UserRepository extends \Doctrine\ORM\EntityRepository
{
// some function
}
Thanks for help.
Mike

A repository class is optional so FOS does not define a repository. They only have the UserManager that calls the repository for the entity from Doctrine.
You have to change the repository on your entity:
#ORM\Entity(repositoryClass="AppBundle\UserBundle\Entity\UserRepository")
This should do the trick.

Related

Symfony 4 autowire DataTransformerInterface

I want to build a new form type to handle one of my problem.
But, using this code :
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;
/**
* Entity hidden custom type class definition
*/
class EntityHiddenType extends AbstractType
{
/**
* #var DataTransformerInterface $transformer
*/
private $transformer;
/**
* Constructor
*
* #param DataTransformerInterface $transformer
*/
public function __construct(DataTransformerInterface $transformer)
{
$this->transformer = $transformer;
}
I got this error :
Cannot autowire service "App\Form\Type\EntityHiddenType": argument
"$transformer" of method "__construct()" references interface
"Symfony\Component\Form\DataTransformerInterface" but no such service
exists. Did you create a class that implements this interface?
I tried to put autowire to off, but I can't inject an interface right ?
Why I can't autowire this Symfony interface ?
If you have your DataTransformer in a own class (which implements DataTransformerInterface) you have to inject this class (your implementation) - not the interface.
see https://symfony.com/doc/current/form/data_transformers.html#using-the-transformer
you could of course aliasing the interface for your specific implementation - but then you only could have this one DataTransformer when injecting the Interface.
Because DataTransformerInterface is not autowirable.
Try this, to see autowiring classes/interfaces
bin/console debug:autowiring
The only way to make it work is to create a service class which implements DataTransformerInterface
Inject your new service by configuring it in service.yml

Can't extend Doctrine's Repository

I'm trying to make a custom Doctrine's ORM Repository and extend it but I can't find a way to make it work. So far this is what i have:
The original Repository
//AppBundle\Repository\LocaleRepository.php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
use JMS\DiExtraBundle\Annotation as DI;
class LocaleRepository extends EntityRepository
{
protected myCustomFunction(){
}
}
The extended Repository
//OfficeBundle\Repository\OfficeRepository.php
namespace OfficeBundle\Repository;
use AppBundle\Repository\LocaleRepository;
class OfficeRepository extends LocaleRepository
{
//Empty class
}
My entiy:
namespace OfficeBundle\Entity;
// some calls to traits
use Doctrine\ORM\Mapping as ORM;
/**
* Office
*
* #ORM\Table(name="office__office")
* #ORM\Entity(repositoryClass="OfficeBundle\Repository\OfficeRepository")
*/
class Office implements TranslatableInterface{
//...
}
And Finally the call:
$em = $this->getDoctrine()->getManager();
$this->getEntityManager();
$office=$em->getRepository('OfficeBundle:Office')->myCustomeFunction($slug);
This trows the exception:
Undefined method 'myCustomFunction'. The method name must start with either findBy or findOneBy!
If I place myCustomeFunction inside the OfficeRepository it works fine but it brings down the purpose of extendind the repository. Also, the repository loaded by the controller is the correct one, vardumping the class shows: 'OfficeBundle\Repository\OfficeRepository'.
Finally I'm using KNP DoctrineBehaviors(translatable) on the office entity.
You must make your method public if you are going to use it outside the repository class.
class LocaleRepository extends EntityRepository
{
public function myCustomFunction()
{
....
}
}

Error while generating doctrine entities with FOSUser Bundle in Symfony2

I'm using FOSUser bundle and everything went ok until I tried to create the database entities, I get this error:
It has nothing to do with the bug in Symfony 2.5, I'm using version 2.7
Case mismatch between loaded and declared class names: App\UserBundle\Entity\user vs App\UserBundle\Entity\User
This is my config file for fos:
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: App\UserBundle\Entity\User
My classes and namespaces they seem to have the right caps, I don't know why this is happening. I tried to clear the cache, no errors but didn't work.
The user class is App/UserBundle/Entity/User.php, here's the content:
<?php
namespace App\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
/**
* User
*/
class User extends BaseUser
{
public function __construct()
{
parent::__construct();
}
}
What happens if you amend your User entity to appear as below (replacing user_table with intended table name)?
namespace App\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* User
* #ORM\Table(name="user_table")
* #ORM\Entity
*/
class User extends BaseUser
{
public function __construct()
{
parent::__construct();
}
}
I found the problem, I was using yaml for the entity definition and the filename was user.orm.yml instead of User.orm.yml.

How to get the instance of the Entity Manager in FOSUserBundle Controller

I am using FOSUserBundle in Symfony 2.3, I created an additional table for store the user profile, at some point, I need to access to the entity manager to persist and flush the data coming from the profile form to the profile table, but I am having problems trying to get an instance of the entity manager.
Any idea?
Thanks!
To edit profiles of users using the FOSUserBundle, you have to create a new ProfileController which extends the ProfileController of FOSUserBundle.
Here you can now override methods of the FOSUserBundle.
An exemple of start :
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Acme\UserBundle\Controller;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Controller\ProfileController as BaseController;
/**
* Controller managing the user profile
*
* #author Christophe Coevoet <stof#notk.org>
*/
class ProfileController extends BaseController
{
public function editAction()
{
$em = $this->container->get('doctrine')->getManager();
// your code here
}
}
You can access to the Doctrine service this way :
$em = $this->container->get('doctrine')->getManager();
The controllers of the FOSUserBundle extend from Symfony\Component\DependencyInjection\ContainerAware. So you have access to the Symfony\Component\DependencyInjection\ContainerInterface through $this->container. With this container you can access doctrine and subequently the EntityManager:
$doctrine = $this->container->get('doctrine');
$em = $doctrine->getManager();

inform symfony of where repository classes are located

I see in the creating your own entity provider section that you put the UserRepository class in the Entity directory. Can you stray from this code organization? For example if i wanted a structure like this:
MyCompany
MyProject
MyBundle
Controller
DependencyInjection
Entity
Repositories
doctrine
UserRespository.php
Resources
doc
public
translations
views
Can you inform symfony about where to locate the UserRepository?
This would do:
/**
* #ORM\Entity(repositoryClass="MyCompany\MyBundle\Repositories\doctrine\UserRepository")
*/
class User
{
}
In reading your doc :
/**
* Acme\UserBundle\Entity\User
*
* #ORM\Table(name="acme_users")
* #ORM\Entity(repositoryClass="Acme\Entity\UserRepository")
*/
class User implements UserInterface, \Serializable
{
So just replace the path by the path you want :
* #ORM\Entity(repositoryClass="MyCompany\MyBundle\Repositories\doctrine\UserRepository")

Resources