I created a bundle to manage user-group-permissions. I want it to make project independent by moving it into the vendors directory.
To make this bundle immutable I moved the users data into a usermeta bundle.
The main bundle contains username and email only about the user, and usermeta contains everything else (name, birthdate etc. whatever a project require).
The problem is the main user bundle intended to belong to a core bundle group, from which every project using the same.
The user-usermeta relation now created a dependency. So every project will need it.
My question is
- How can I standardize its format, to enforce in every project create it properly.
- How can I make this dependency optional (preferred)
I suggest you only handle a UserInterface instead of a User entity in your bundle.
In case of Symfony UserInterface doesn't implement everything you need (username but no email), create your own UserInterface in your bundle :
namespace YourDomain\YourBundle\Interface;
use Symfony\Component\Security\Core\User\UserInterface as BaseInterface;
/**
* UserInterface is the interface that user classes must implement.
*/
interface UserInterface extends BaseInterface
{
/**
* Returns the email address of the user.
*
* #return string The user email address
*/
function getEmail();
}
And then, in the projects using your bundle, your User entity must implements your specific interface instead of Symfony UserInterface.
Related
we need to send the data to 2 topics which belongs to the two different servers. One is confluent offerings in Azure another is kafka cluster on azure VM.
Now we are using KafkaRoutingTemplate to send message to the 2 different Kafka Offerings.
But we create 2 producers/factory via config class ( coding) - Is there any way to configure 2 producers via application.yml file instead of coding
You can override the bootstrap servers in one of the templates; they can use the same producer factory, but you have to define the templates as beans.
/**
* Create an instance using the supplied producer factory and properties, with
* autoFlush false. If the configOverrides is not null or empty, a new
* {#link DefaultKafkaProducerFactory} will be created with merged producer properties
* with the overrides being applied after the supplied factory's properties.
* #param producerFactory the producer factory.
* #param configOverrides producer configuration properties to override.
* #since 2.5
*/
public KafkaTemplate(ProducerFactory<K, V> producerFactory, #Nullable Map<String, Object> configOverrides) {
I have setup two entity managers in doctrine.yml
I have to inject repository into service but problem is repository always taken an default entity manager.
How should I give specific entity manager to repository.
In symfony 4 we can treat repository as service using ServiceEntityRepository
You can try to inject Doctrine\Common\Persistence\ManagerRegistry in your construct. And then use $managerRegistry->getManager('your_connection_name');
For example:
//use Doctrine\Common\Persistence\ManagerRegistry;
private $connection;
function __construct(ManagerRegistry $em)
{
$this->connection = $em->getManager('your_connection_name');
}
Rather than lazy/auto loading them, you'd need to setup each as a named service and explicitly configure their loading in your services.yaml file.
I'm using the Symfony web test case to test my endpoints usually. In this case I just wanted to test a single repository call to persist a new entity so I wanted to avoid using the web test client.
I tested using the web test client first and POSTing to the endpoint will create the entity in the SQLite database using the repository. However if I get the repository class in my test and directly call the creation method it fails on persist() with the exception:
Entity of type MyEntity is missing an assigned ID for field 'id'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.
I've updated the schema using
app/console doctrine:schema:update --force --env=test
But app/console doctrine:schema:validate --env=test still tells me the database is not up to date. This would suggest a problem with the mapping, but if there was a problem with the mapping why would it work in production (which uses MySql) or indeed why would it work if I call it using the web test client?
The doctrine mapping for the ID is:
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*
* #var int
*/
private $id;
I want to limit my user account to only view and modify entities it has a relationship with.
I've got a basic FOS User setup, and I have a entity with a one to many relationship with my user entity.
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="blog")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user_id;
The schema validates properly, and doctrine seems happy with my relationship.
I've used the app/console generate:doctrine:crud command to generate a basic crud system.
How should I limit doctrine from returning entities that don't have a relationship with the logged in user?
This is the code I'm currently using to get all entities.
$entities = $em->getRepository('ExampleBundle:Blog')->findAll();
Is there a pre-built command for fetching by user id, or do I need to write some DQL?
You can use the findBy method with the current logged in user.
$user = $this->get('security.context')->getToken()->getUser();
$entities = $em->getRepository('ExampleBundle:Blog')->findBy(array('user' => $user));
And in your Blog entity, it's not a user_id but a user, you want to get the user not the id. $blog->getUser();
Hope it's helpful.
Best regard.
I've just started to learn the symfony2 framework. Now I'm building my first bundle, a chat bundle. Everything works just fine but there is one thing I can't get a hang of, the foreign key to the user table. (I'm using the FOS User Bundle and Doctrine).
What should the annotation look like to the FOS User table (one to one)? And when fetching data from the chat table, will I get the user object aswell or do I need to fetch the user object after reading each row?
Solved.
Make sure to include the namespace of the user entity:
use Acme\UserBundle\Entity;
Add the annotation:
/**
* #ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;