Persisting entities in SQLite from Symfony test case - sqlite

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;

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;

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

CRUD generation with composite key on Doctrine 2.2

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;

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;

Symfony 2 bundle dependency issue

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.

Resources