How to set file usage on drupal 8? - drupal

How do I set the file usage on Drupal 8? How can I use the following methods?
file_usage_add()
file_usage_delete()
file_usage_list()

You can use these methods in Drupal 8:
File usage add: \Drupal::service('file.usage')->add
File usage delete: \Drupal::service('file.usage')->delete
File usage list: \Drupal::service('file.usage')->listUsage

Related

Disable file output of hydra

I'm using hydra to log hyperparameters of experiments.
#hydra.main(config_name="config", config_path="../conf")
def evaluate_experiment(cfg: DictConfig) -> None:
print(OmegaConf.to_yaml(cfg))
...
Sometimes I want to do a dry run to check something. For this I don't need any saved parameters, so I'm wondering how I can disable the savings to the filesystem completely in this case?
The answer from Omry Yadan works well if you want to solve this using the CLI. However, you can also add these flags to your config file such that you don't have to type them every time you run your script. If you want to go this route, make sure you add the following items in your root config file:
defaults:
- _self_
- override hydra/hydra_logging: disabled
- override hydra/job_logging: disabled
hydra:
output_subdir: null
run:
dir: .
There is an enhancement request aimed at Hydra 1.1 to support disabling working directory management.
Working directory management is doing many things:
Creating a working directory for the run
Changing the working directory to the created dir.
There are other related features:
Saving log files
Saving files like config.yaml and hydra.yaml into .hydra in the working directory.
Different features has different ways to disable them:
To prevent the creation of a working directory, you can override hydra.run.dir to ..
To prevent saving the files into .hydra, override hydra.output_subdir to null.
To prevent the creation of logging files, you can disable logging output of hydra/hydra_logging and hydra/job_logging, see this.
A complete example might look like:
$ python foo.py hydra.run.dir=. hydra.output_subdir=null hydra/job_logging=disabled hydra/hydra_logging=disabled
Note that as always you can also override those config values through your config file.

how to add actAs when using symfony doctrine:build-schema

Background:
(1) I use mysql workbench to design my database.
(2) Then I use "Synchronize Model" tool in mysql workbench to apply my modification to mysql database.
(3) Finally I use symfony doctrine:build-schema command to generate schema.yml according to database(For some legacy reason, I have to use Symfony 1.4).
Here comes the problem: I wanna add actAs: { Timestampable: ~ } to schema.yml, but symfony doctrine:build-schema command cannot do that.
The solution I can imagine is to write a shell to edit schema.yml. Clearly this is not an elegant solution.
Any suggestion is greatly appreciated.
1) What errors is given out by command symfony doctrine:build-schema
2) Try such code in schema.yml
Something:
actAs:
Timestampable:
columns:
...
I use MySQL Workbench Schema Exporter (an open source PHP library project) to save time and complete all database model definition tasks in modeling process. Review this Installation Tutorial.
With MySQL Workbench Schema Exporter you can use Table Comments to define table behavior. To add a timestampable behavior to table definition put this in your comments:
{MwbExporter:actAs}actAs: [Timestampable]{/MwbExporter:actAs}
Then export your model (*.mwb file) to YAML (*.yml file) using MySQL Workbench Schema Exporter and place it in ./config/schema/ folder in order to create an sql script and then load it to a database already configured with symfony. It's easy! just like this:
Export model to YAML
php bin/mysql-workbench-schema-export your_model.mwb
Copy schema.yml resulting file to ./config/schema folder location
Create an sql script
php symfony doctrine:build-sql
Now build your model physically with:
php symfony doctrine:build-model

Postgresql: ERROR: type "citext" does not exist

I have read other posts, when searching, an answer to this question.
I am using PostgreSQL 9.1, and created extension 'citext' using CREATE EXTENSION citext, but when I try to create any columns of type 'citext', it throws this error
ERROR: type "citext" does not exist
I researched but did not find any concrete answers? Any idea why?
Ok figured it out. I have several databases and CREATE EXTENSION citext has to be run for each db to install the extension in that DB. You must do on psql prompt:
psql =# \c db_1
CREATE EXTENSION citext;
psql =# \c db_2
CREATE EXTENSION citext;
#NullException is correct that the extension needs to be created in each database. If you want to automatically have an extension created, you can create it in the template1 database which (by default, at least) is the database used as a model for "create database", so with appropriate permissions, in psql:
\c template1
create extension citext;
Then new databases will include citext by default.
To use citext, use the CITextExtension operation to setup the citext extension in PostgreSQL before the first CreateModel migration operation.
https://docs.djangoproject.com/en/2.2/ref/contrib/postgres/fields/#citext-fields
from django.contrib.postgres.operations import CITextExtension
class Migration(migrations.Migration):
...
operations = [
CITextExtension(),
...
]
similarly to HStoreField as
https://docs.djangoproject.com/en/2.2/ref/contrib/postgres/operations/#create-postgresql-extensions
If you use Docker, and want to add this extension to your database,
I have done the following,
# Dockerfile
FROM postgres:11.3
# Adds the CIText Extension to our database
COPY ./compose/production/postgres/initdb_citext.sh /docker-entrypoint-initdb.d/citext.sh
And my initdb_citext.sh:
#!/bin/sh
# Adds the citext extension to database and test database
"${psql[#]}" <<- 'EOSQL'
CREATE EXTENSION IF NOT EXISTS citext;
\c template1
CREATE EXTENSION IF NOT EXISTS citext;
EOSQL
This applies the extension to test databases that django generates too.

Symfony 2 pdo exception

I'm using Symfony 2 and have this row in my parameters.ini:
database_driver = pdo_pgsql
When I was creating database structure with Doctrine everything was good. But if I want to add some doctrine object to my darabase (insert row), I catch an exception:
What I have to do with this?
Are you sure you're using pdo_pgsql? Are you running on localhost? It might be very certain that you are using pdo_mysql driver instead.
However you have to check the following:
php.ini
extension=pdo.so
extension=pdo_mysql.so
or in your case
extension=pdo.so
extension=pdo_pgsql.so
You can check the phpinfo(); to find out the configured database driver.
In your symfony project you have to check the parameters.ini file in config folder. E.g.
[parameters]
database_driver="pdo_mysql"
database_host="localhost"
Besides try to avoid this error
'stty' is not recognized as an internal or external command,
operable program or batch file.
https://github.com/symfony/symfony/issues/4974
First of all, verify your php.ini file: the extensions php_pdo_pgsql and php_pdo must be enabled. Make sure you apply this changes on php.ini file that your symfony project is using, check this on localhost/path_to_your_project/web/config.php. You know if this extensions are enabled executing the function phpinfo().
This command is also helpfull: php -m. It lists on console all the php modules that are loaded.
Tip: check out you Apache error log, there could be something wrong with the load of your extensions. This file is located according to your server configuration.

Drupal 7 - image module and contrib module

I wrote a module with "image" in dependecies. But I can't use the functions in image.gd.php
Apache log:
Call to undefined function image_gd_save()
Can you help me?
If you're doing this outside of the normal operation of the image/system modules you'll need to make sure the relevant file is included first:
module_load_include('inc', 'system', 'image.gd');

Resources