I am writing a php script to import some data into my custom module, basically i will run a cron job periodically to copy some data from another table to populate my custom module fields.
Thanks.
Use PearDatabase.php class to get database access and functions related to the DB operations.
Example:
<?php
include_once 'include/database/PearDatabase.php';
$db = PearDatabase::getInstance();
$result = $db->pquery("SHOW TABLES LIKE 'leads'", array());
echo $db->num_rows($result);
print_r($result);
echo "<br>Test Done <br>";
//Using $db object you can access other DB related methods defined in that class.
//Include the file in your module in any file where you want DB operation.
// But I would recommend, use inside your main module class or model controller class.
?>
Put this PHP script into your root directory and run directly this file using your browser. So you will get some idea and you can able to use inside your module because all module loads from index.php file (root directory).
Related
I have a Symfony 4 app which is complete and working. Any uploaded files are put inside an upload folder outside the doc root and accessed via a separate cdn url.
Inside the upload folder I have a htaccess file that redirects any request, let’s say for example an image, to a php file which then serves the image.
What I want to be able to do is send an auth key along with the image request so that the standalone index.php file can check the user is valid from the database.
This means I need some way of accessing the symfony entity manager from outside of symfony, by including it. The other option would be a curl request to a symfony controller, but I’d rather not add additional network requests if possible. The third option is a separate pdo direct to the database and make sql queries... but again I’d prefer to use the symfony entity manager by including symfony somehow so I can use other features if needed.
I actually tried including the symfony bootstrap in the index.php but that then drags in the need for routing to work which isn’t needed in this case.
You can access EntityManager like that:
require __DIR__.'/../vendor/autoload.php';
(new Dotenv())->load(__DIR__.'/../.env');
$kernel = new Kernel('dev', true);
$kernel->boot();
$user = $kernel->getContainer()->get('doctrine.orm.entity_manager')->getRepository(Repo::class)->find(id);
dd($user->getRoles());
I'm extending the WP REST API by writing a Controller class.
I'm trying to read the config for this class from a file, e.g:
{
"base-namespace": "myapi",
"version": "v1",
"resource": "things"
}
This would allow me to keep server and client in sync as they would both use the same config file.
However, I do not want WP to stay reading this file for every request it serves... Currently, if I read this file from anywhere in the plugin file (or any of its required files - including the Controller definition), and if I also echo out where I'm reading, I see it's always passing through that bit of code (including the reading) for every request.
I imagine I need to read this file somewhere outside the plugin itself - make it a global, and then access it when instantiating the Controller.
I'm new to WP - this is the first time dabbling with it. Where should this global variable definition go such that it's only executed once?
Note:
I have tried using require_once in my plugin to require a config file which does the file reading. I had put an echo statement there and it shows that that file gets executed for every request (despite the require_once).
I have also tried wrapping the file reading in an if(!isset($my_global_var) statement. But adding an echo statement inside the if statement shows that this global variable is always unset with every request served... clearly this needs to go in some kind of WP startup file which only gets executed once.
Thank you.
Store your config data as a PHP array in a .php file and then include it using the PHP include statement. Advanced PHP engines parse the PHP source once and cache a compiled representation of the script so that it does not have to re-parse the PHP sources everytime. So if your data is inside a PHP source file it would be saved in the PHP's engine compiled script cache.
Of course if your client is non-PHP it would need to have code to parse a PHP array.
In my project i have lot of different types of document of user to be uploaded. I am using local drive to save media and ony save the file. What it want is to define the uploaded directory in only configuration file (category wise) access it in controller and twig files. so that if i changed the media path later, i have to change it only once and should work everywhere?
I know lot bundle available for this, but i am not using any bundle I used doctrine to upload this media. More, later i also move this media to Amazon S3.
You can do it directly in parameters.yml:
parameters:
/.../
media_directory: '%kernel.root_dir%/../web/uploads'
And you can have access in controller like all other parameters:
public function someAction()
{
$directory = $this->getParameter('media_directory');
}
EDIT
For twig, try like that:
<p>Media Directory: {{ media_directory }}</p>
Before, I think you have to declare twig global var in app/config/config.yml:
# Twig Configuration
twig:
globals:
media_directory: %media_directory%
As question is self explanatory. And very common but how can i do with the help of symfony2 components (Filesystem).
I do not see read function in filesystem.
After lot of googles, I could not fine. Please help!
There is no function in Symfony2's Filesystem component that appends content to a file. Just use php's built in function:
file_put_contents($file, $content, FILE_APPEND);
PHP Documentation on file_put_contents
Symfony2 API for FileSystem Component
EDIT:
Since Symfony 3.3, it is possible to append content in file:
$fs = new Filesystem();
$fs->appendToFile('logs.txt', 'Email sent to user#example.com');
Symfony Filesystem Component
Since Symfony 3.3, it is possible to append content in file:
$fs = new Filesystem();
$fs->appendToFile('logs.txt', 'Email sent to user#example.com');
Symfony Filesystem Component
I've got to read and serve a lot of resources (images and files) and I want to hide real path where resources are stored. What is the best way to do it with symfony 2.x?
If you want to abstract from the filesystem you could use KnpGaufretteBundle. Gaufrette is a PHP library that abstracts the filesystem. That is, you can access resources no matter where they are stored (e.g., the local filesystem, a FTP server, Amazon S3, Dropbox, etc).
However, Gaufrette does not abstract the path (you set up a kind of base directory for the filesystem) and you would use a path relative to this base directory. Consider the following code that abstracts the local filesystem:
<?php
use Gaufrette\Filesystem;
use Gaufrette\Adapter\Local as LocalAdapter;
$adapter = new LocalAdapter('/var/media');
$filesystem = new Filesystem($adapter):
$content = $filesystem->read('myFile.txt');
$content = 'Hello I am the new content';
$filesystem->write('myFile.txt', $content);
In this example you would read and write the file /var/media/myFile.txt.
If you want to further abstract the filesystem, you could create a service that has a map of files and its aliases. For example, you could read a list of these file/alias pairs from a YAML configuration file. You can then get the real filename by using some kind of getter with the alias as parameter.