Failed to open page after cloning Symfony PHP project - php

I just cloned a Symfony PHP project from Github but whenever I open a page containing the following code:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$modules = $em->getRepository('AppBundle:Module')->findAll();
return $this->render('module/index.html.twig', array(
'modules' => $modules,
));
}
The page will return the following error:
ConnectionException: An exception occured in driver: SQLSTATE[HY000] [1049] Unknown database ''.
I am not really experienced with PHP Symfony projects yet and someone asked me to clone this and try to solve the error I will be returned with. Where do I need to look in order to fix the error? I suppose some sort of configuration file which points to database configuration?

This error mean you didn't got access to the database.
Run php app/console doctrine:database:create and after, php app/console doctrine:schema:update --force.
It will create the database on your own local server. (make sure to provide correct connection details on your app/parameter.yml file)
EDIT: Also, don't forget to update your project using composer install and composer update.
Since this is a github project, all dependencies are not pulled

You need to create database:
For Symfony2:
php app/console doctrine:database:create
For Symfony3:
php bin/console doctrine:database:create
You can see sql dump:
For Symfony2:
php app/console doctrine:schema:update --dump-sql
For Symfony3:
php bin/console doctrine:schema:update --dump-sql
Than create tables:
For Symfony2:
php app/console doctrine:schema:create
For Symfony3:
php bin/console doctrine:schema:create

Related

Update an entity in Symfony 4?

My question is simple, but I can't find a fine answer to it, I had an entity created by a command line :
php bin/console make:entity
this entity is User that has few attribute ( name - email - password )
After inserting the fields, I migrated, so my table has been created in the database using those commandlines :
php bin/console make:migration
php bin/console doctrine:migration:migrate
But now I want to just change the name to a username but I don't know how to do it.
I did not find anything in the documentation, so any help would be much appreciated.
Be careful: After any changes in your entities you must generate new migration file. In fact this file contains all of the changes which must be done on your database to be update.
To generate this file (new migration version) you can follow these commands:
$ bin/console doctrine:cache:clear-metadata
$ bin/console doctrine:migrations:diff
After above commands you generated your new version file successfully, now if you run following command, you can see you have a new and not executed version file:
$ bin/console doctrine:migrations:status
Finally, to execute the new version file and update your database, you must run the following command:
$ bin/console doctrine:migrations:migrate --all-or-nothing
now your database is update and in the table migration_versions you can see that new version has been added.
Be Successful.
You can follow a similar pattern:
Change the field (annotation) in your User-entity from name to username
Run bin/console doctrine:migrations:diff
Run bin/console doctrine:migrations:migrate
The doctrine:migrations:diff command should detect that the field changed and create a corresponding SQL query for you, which is then stored in a DoctrineMigrations-file right next to the original one.
The doctrine:migrations:migrate detects this new migration and that it was not previously executed, by checking against the doctrine_versions table, where all migration versions that were executed are stored.
See also: https://www.doctrine-project.org/projects/doctrine-migrations/en/2.1/reference/generating-migrations.html#diffing-using-the-orm

Could not open file app/console Symfony 2.8

I am using Symfony 2.8 using Symfony 3 directory structure. After recent composer install it is complaining that
An error occurred when executing the "'cache:clear --no-warmup'" command: Could not open input file: app/console
I looked at the change log and could not find anything regarding this change is there anyway to fix this issue.
Do you have a console folder inside the app one ?
btw try to do php bin/console cache:clear --no-warmup
bin/console is the new directory in Symfony 3. It's maybe the same issue.
Create var directory.
Explanation:
vendor/sensio/distribution-bundle/Composer/ScriptHandler.php:462
protected static function useNewDirectoryStructure(array $options)
{
return isset($options['symfony-var-dir']) && is_dir($options['symfony-var-dir']);
}
So you need both to have symfony-var-dir in composer.json's extra and have this directory existing.

Creating multiple tables with Symfony & Doctrine

Starting my first Symfony/doctrine project I created 2 entities (user & property) using:
$php app/console doctrine:generate:entity
This works fine and gives me the two needed php files with the classes and annotions.
After this I want to create the corresponding tables in my database using:
$php app/console doctrine:schema:update --force
Only the first table (user) is created. What's going wrong? What do i have to do to have both tables created. BTW: no error message received...
SOLVED! There was a syntax error in the property class. But doctrine:schema:update did not give any error... :-(
When trying doctrine:schema:create I got the error message. Corrected it and afterwards doctrine:schema:update worked well!
always backing up your entities before you update schema ..
for backing up entities write
>php app/console doctrine:generate:entities projectname
If there is Error In entity It will show you.
Try to do
php app/console doctrine:generate:entities
before
php app/console doctrine:schema:update --force

Symfony + Doctrine functional database testing

I found only a few lines about database functional testing in the documentation: http://symfony.com/doc/current/cookbook/testing/database.html
The problem is:
I need to install database schema from dev database to test database before testing.
I need to reinstall fixtures before each test.
It's not described in the documentation. How can I do it using symfony?
I use this command to do same job :
In console line :
doctrine:database:create --env=test
doctrine:schema:create --env=test
doctrine:fixture:load --env=test

Error using ORM to import a new table

I have an existing php web app written using symfony2 and doctrine2. I added a table to the database. Now, I want the ORM to write the php classes for me. I tried doing this:
php app/console doctrine:mapping:convert xml ./src/Acme/BlogBundle/Resources/config/doctrine/metadata/orm --from-database --force
I also tried this:
php app/console doctrine:mapping:import SomeBundle annotation
Both of them throw this error:
[ErrorException]
Warning: class_parents(): Class SomeClass does not exist
and could not be loaded in C:\wamp\www\vac\vendor\
gedmo-doctrine-extensions\lib\Gedmo\Mapping\
ExtensionMetadataFactory.php line 80
Any ideas on what I need to do?
delete all your entities along with your Entity Folder.
delete all orm files with your orm folder.
then try to create orm and entity files from your db having new table.
a.$ php app/console doctrine:mapping:convert xml ./src/YourDir/YourBundle/Resources/config/doctrine/metadata/orm --from-database --force
b.$ php app/console doctrine:mapping:import YourDirYourBundle annotation
c.$ php app/console doctrine:generate:entities YourDirYourBundle
Try this.

Categories