Symfony VichUploaderBundle rename file - symfony

Hi is there a way to rename uploaded file using VichUploaderBundle? If I upload two image with same name it will remove the old one and it's a problem for me.
I have configuration exactly the same like in the doc

For this kind of issues, the bundle provide "file namers". They are simple services used to name files, in particular to avoid collisions on the filesystem.
You probably want to use the vich_uploader.namer_origname file namer as it will rename your uploaded files using a uniqueid as the prefix of the filename and keeping the original name and extension (a file named foo.jpg will be stored as 50eb3db039715_foo.jpg)

You could use the namer in your vich_uploader.yaml
vich_uploader:
db_driver: orm
mappings:
product_image:
namer: vich_uploader.namer_origname

Related

Configure translation file format and folder structure

I use the php-translation bundle.
When I edit a value for a translation key in the debug bar and sync it, then a .xlf file gets automatically created in the app/Resources/translations folder and it provides the translation key with value that I edited in the debug bar.
What I actually want to have is that the value of the translation key gets updated in the .yml file in the src/UserBundle/Resources/translations file that I have already created.
Is there any possibility to configure the bundle that way?
You can specify the output folder. From the php-translation documentation:
translation:
locales: ["en", "fr", "sv"]
configs:
app:
dirs: ["%kernel.root_dir%/Resources/views", "%kernel.root_dir%/../src"]
output_dir: "%kernel.root_dir%/Resources/translations"
excluded_names: ["*TestCase.php", "*Test.php"]
excluded_dirs: [cache, data, logs]

Pyexcel, loading a file to create a book in memory

This is solved; thanks to #vmontco's solution: I was missing MEDIA_URL, now it works perfectly.
----------original question below-----------
I welcome suggestions from every angle; I am fairly new to Django and Python. I'm sure I am missing something simple.
Using a Model Form, with a FileField, I upload and save an Excel file to a folder structure under MEDIA_ROOT. This works.
I want to read that same file later to perform operations using Pyexcel. This is where I am stuck. I am attempting to upload the file using the FileField stored in the DB.
This is where I have problems, and I am not sure if am misunderstanding MEDIA_ROOT, or some other aspect of Django.
When I pass the pk to the 2nd view, I then instantiate an object based on the Model. It has the FileField 'docfile', which I am trying to use to access the file to do some operations using Pyexcel,
here is the FileField declaration from models.py:
docfile = models.FileField(
verbose_name="Choose file to upload:",
upload_to='Excel_CSV_Assets/%Y/%m/%d')
EDIT: If I hard-code the pth to the file like this, everything works, including operations afterwards:
thedocfile='site_static/site/original_assets/Excel_CSV_Assets/2016/04/23/Animals_oglc4DV.xlsx'
book=pyexcel.get_book(file_name=thedocfile)
:END OF EDIT
Here is the code from the 2nd view, where I attempt to read the file into memory, and make a 'book' class object using Pyexcel. I am stuck here:
asset = Excel_CSV_Asset.objects.get(id=assetid)
book=pyexcel.get_book(file_name=asset.docfile)
Here is my error description:
Here is the info right at where my code breaks:
Although it says "Wrong filename", I can see the file is in the folder:
I'm able to open the file by double-clicking; the file is not corrupted.
EDIT:
If I cast the 'asset.docfile' to str, like so:
asset = Excel_CSV_Asset.objects.get(id=assetid)
book=pyexcel.get_book(file_name=str(asset.docfile))
I get a different error:
[Errno 2] No such file or directory: 'Excel_CSV_Assets/2016/04/23/Animals_oglc4DV.xlsx'
...but this is the correct directory, located beneath the MEDIA_ROOT file structure.
Here is settings.py MEDIA_ROOT:
MEDIA_ROOT = 'site_static/site/original_assets/'
Here is urls.py:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^e/', include('excel_to_mongo.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Here is the url.py of that app:
url(r'^efactory/(?P<assetid>\d+)/$', 'display_sheet_column_choices', {}),
I think your problem is that you don't fully understand the media files management with Django.
What are media files?
Media files are all the files that are user-uploaded (at running time).
You must not mistake them with Static files that are assets needed by your project to work and that you add at development time (CSS, background picture and JS files for instance).
You shouldn't mix them because they are managed differently by the server and that it could lead to security problems (cf. the warning here):
Static files management :
You put your static files as a part of the code either in one static subdirectory from the installed django applications, either in one of the locations you added to STATICFILES_DIRS.
Static files have to be gathered before starting the server by calling ./manage.py collectstatic, this command will collect (copy) the static files into the a directory (STATIC_ROOT's value).
You then have to set STATIC_URL to choose with wich url you should serve your static files. An usual choice would be /static/. To access the static file you should then try to reach /static/path/to/static/file/in/static_root/dir.
Media files management :
Your media files are added at running time. They are stored in the MEDIA_ROOT location that has to be an absolute path. Hence the fact I suggested you to join the BASE_DIR value (an absolute path) and the subdir you would choose with something like :
MEDIA_ROOT = os.path.join(BASE_DIR, "/media/subdir")
You then have to set an URL for your media files, by using the MEDIA_URL variable. To access your media files, the urls will start with the value you choose :
MEDIA_URL = '/media/'
Then, add this to your urls.py file :
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
With the current example, your mymediafile.txt will be located at /path/to/your/project/media/subdir/path/in/media/root/mymediafile.txt and served at http://127.0.0.1:8000/media/path/in/media/root/mymediafile.txt.
But this is suitable only for a development use as told here. And this would work only for DEBUG == TRUE
For a production use, you should consider deploying your media files with your http server (apache for instance).
Conclusion :
Take the time to understand this. Because I suspect you don't really understood what you did and this lack of understanding could lead to future bugs and errors.

ORM convert-mapping is creating the wrong file structure

I currently got orm set up on my silex application. Everything seems to work as expected except for when I run the command to generate the entities from my database (reverse-engineering).
../../../vendor/bin/doctrine orm:convert-mapping --namespace="Random\MyApp\Model\Random\Entities" --force --from-database annotation ./../random/entities
This command will create all of my entities perfectly but they will be under the folder structure
./../random/entities/Random/MyApp/Model/Random/Entities/(files here)
which is wrong since I am expecting to have
./../random/entities/(files here)
A namespace is actually the path under your entities are grouped. So it's perfectly logic that doctrine generates files under this namespace.
If you want a file structure like it just do :
../../../vendor/bin/doctrine orm:convert-mapping --namespace="random/entities" --force --from-database annotation ./
But this is definitely not conform with the PSR-0

Nothing to update "Doctrine"

I'm using Symfony2 with Doctrine to try and update a table schema. I was able to create the table. I was also able to populate the table. However after updating the comments in the Entity (I wanted some fields to become nullable), those changes did NOT get picked up.
I did create the entity with the "Annotations" option chosen. But when I added this line "nullable=true" to the Entity on the field imageName nothing happens. ie: when I run "./app/console doctrine:schema:update" I get the following output "Nothing to update - your database is already in sync with the current entity metadata."
Note, I have tried deleted the table via: ./app/console doctrine:database:drop --force and then recreating it via: ./app/console doctrine:database:create and then also ./app/console doctrine:schema:create but it STILL does not add my updated nullable field to imageName.
I was able to figure this out. I first of all created my entity "Foobar" using yml as the Configuration format. I then wanted to use "annotation" as the configuration format so I manually deleted the Entity folder (I only had one table created), however I did NOT delete the configuration yml in the Resources/config/doctrine/Foobar.orm.yml.
Thus when I created the entity again, this time using the annotation as the configuration format, it was still linking to the yml configuration. Removing that solved all the troubles.
I have however decided to stick to yml as I feel it is a little easier to read than the Doctrine Metadata found in the comments.
I had been stuck with this for almost 2 days. Removing all the file in /src/AppBundle/Resources/config/doctrine resolve my issue.
For me, the key was to clear Redis cache.
php app/console redis:flushdb
I has this problem too. Have you right annotation before class declaration?
/**
*
* #ORM\Entity <- this does the trick
*/
class MyEntityName
{
...
Check doctrine.yaml config file for orm mappings:
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
As you see here all entities should have prefix (namespace) App\Entity
You have to check your entities namespace, it should be App\Entity or whatever you want in config

Symfony2 - Change Migration Directory

How can I change the default migration dirctory in the config.yml?
Right now I am using 2 bundles with different db-connections and I would like to create migrations files and store them in different directories to use the doctrine:migrations:migrate --em=whatever function in depency of the bundle.
For example:
doctrine:migrate:diff --em=whatever #creating a version file in the DoctrineMigrationsWhatever directory
php app/console doctrine:migrations:status --em=whatever # shows only the version files, that belong to the bundle
If you'll create separate entity manager for this second connection/bundle you will get another directory in your DoctrineMigrations dir. For example:
app/
DoctrineMigrations/
entityManager1/
entityManager2/
If you want put all migrations to another directory, you can set it inside your config.yml:
doctrine_migrations:
dir_name: '%kernel.root_dir%/../Acme/CommonBundle/DoctrineMigrations'
namespace: 'Acme\CommonBundle\DoctrineMigrations'
If you want some more complex thing like put migrations from em1 to dir1 inside bundle1 and put migrations from em2 to dir2 inside bundle2 you will need an additional two configuration files where you'll specify dirs for particular bundles:
http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/reference/introduction.html#configuration
And then you run your migrations like this:
doctrine:migrations:status --em=em1 --configuration=./path/to/bundle1/Resources/config/migrations.yml
doctrine:migrations:status --em=em2 --configuration=./path/to/bundle2/Resources/config/migrations.yml
By https://github.com/doctrine/DoctrineMigrationsBundle/pull/46
the migrations.yml file should look like:
name: Doctrine Postgres Migrations
migrations_namespace: Application\Migrations
table_name: migration_versions
migrations_directory: PostgreSqlMigrations
For other people who found this page and spent hours trying to implement Cyprian's solution, it doesn't work.
First, --configuration gets clobbered, and second, the doctrine migrations bundle doesn't support multiple entity managers.
See https://github.com/doctrine/DoctrineMigrationsBundle/issues/18 for information about --configuration and see https://github.com/doctrine/DoctrineMigrationsBundle/pull/46 for an open pull request to support multiple entity managers.
If and when PR #46 goes through, this will be a trivial configuration:
doctrine_migrations:
default:
dir_name: ...
namespace: ...
em2:
dir_name: ...
namespace: ...
The only tweaks available right now, are:
doctrine_migrations:
dir_name: '%kernel.root_dir%/../Acme/CommonBundle/DoctrineMigrations'
namespace: 'Acme\CommonBundle\DoctrineMigrations'
But that will update the configuration for all migrations across all entity managers.
For Symfony 4, the recommended approach is to use %kernel.project_dir% instead, and put it in the src/ folder namespaced by App\:
doctrine_migrations:
dir_name: '%kernel.project_dir%/src/DoctrineMigrations'
namespace: 'App\DoctrineMigrations'

Resources