CRUD generation with composite key on Doctrine 2.2 - symfony

I got a problem with the CRUD generation on Doctrine. I saw that the composite keys have been implemented since version 2.1. I got a few tables that are identified with such keys but when I try to generate the code with the command php app/console generate:doctrine:crud it send me back the following error: "The CRUD generator does not support entity classes with multiple primary keys."
Should I code my CRUD from scratch or is there a workaround to generate them?

Doctrine doesnt support composite keys, you could just use a new primary key:
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue
*/
private $id;

Related

How can I update an Entity in Symfony 4 and API Platform

I'm using Symfony 4 with API Platform and I altered my existing Entity by adding some fields to it by running:
php bin/console Make:Entity
again.
After that I ran:
php bin/console doctrine:schema:update --force
and:
php bin/console cache:clear
Then I copied the Entity and the Repository to the Server and cleaned the cache there also.
After that I altered the SQL Table directly in the database reflecting the same as on my local side (where I ran migration).
When I now open my Swagger Documentation by travelling to /symfonysite/api/ on the server,
I can see the model altered correctly but the route examples (if you click on a route) does not reflect that.
Also when I do a GET Request with Postman, it's only returning the old Entity reflecting the old Model without the new added fields.
Can you point me out what is missing ?
What is your way to alter an Entity on production site?
Thanks in advice!
Ok, i finally got it, it was very simple.
To reflect the new model in the result, i had to add a group to my entity fields. Since its a get request i had to add the group "get" in the annotations
#Groups({"get"})
When i asked the question, my entity looked like this:
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $firstname;
Finally i fixed it by changing it to this
/**
* #ORM\Column(type="string", length=255, nullable=true)
* #Groups({"get"})
*/
private $firstname;

Persisting entities in SQLite from Symfony test case

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;

Symfony2 - Adding a new field to an existing entity

First off, I am completely new to Symfony2.
I created an entity -> created a table based on that entity -> created a form using the entity.
I have now realised I need to add a field to the form. So I did the following:
Added the new property -> Added the ORM annotations -> Generated the setters and getters -> ran "php app/console doctrine:schema:update"
This resulted in the following exception: "The table with name 'XXX' already exists"
So nothing was updated. Any idea what I did wrong? Below is the property I added to the entity:
/**
* #var text
*
* #ORM\Column(name="description", type="text")
*
* #Assert\NotBlank(message="Please insert a description")
* #Assert\Length(max=100)
*
*/
private $description;
Try using Doctrine Migrations Bundle. What you are trying to do - make changes to a database you've already deployed - is called a "database migration." I've found this bundle to be very helpful.
Instead of running "app/console doctrine:schema:update", you'll run "app/console doctrine:migrations:diff" which will compare your database schema against your updated entity and generate the sql code to bring them back in sync.
I know this is an old question. But I write because it can be usefull for others.
Probably you are not using annotations in your project. Probably your project is confiured for use xml or yml.
you must to check your configuation at $proyect_home/app/config/config.yml and write in the orm zone:
orm:
....
mappings:
AppBundle:
type: annotation

Symfony2 allow a FOS user to only update entities they have a relation to

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.

Symfony2 and FOS User Bundle - annotations

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;

Resources