Run yii commands from shell script - php

I am trying to make a script which automaticly loads a project from a git repository. I run exec() from a php file with name of the shell script as an argument. The script looks something like this:
git pull
php yii migrate
The git command works well, but the yii command is totally ignored. I'm doing this from the root of the directory of the yii site, so it should work, but it doesn't.
How can I fix this?

if you are using Yii version 1.x, you have to run your command from inside protected directory
cd protected
php yiic.php migrate

First of all, if you want to run console application in Yii2, just use
yii <route> [--option1=value1 --option2=value2 ... argument1 argument2 ...]
Second: yii migrate is the defined console command to upgrade a database to its latest structure. So it probably does its work, but not that you want to.
Try to rename your console command.
Reference links to read:
Guide: console commands
Guide: migrations (part with yii migrate).

Related

Run php artisan from one Laravel install to another

We have a Laravel deployment website set up under deploy.mysite.com which handles deployments for a multitude of other websites.
One other website I'm trying to deploy which is also a Laravel site resides under site2.myothersite.com.
Both are on the same server. Deploy calls a script on site2, this deploy script runs various commands after cd'ing to the project directory. We use the following to update the database structure.
php artisan migrate --force
Ordinarily when this is run directly via SSH when in the project root, it runs just fine.
However when this is run via the deployment script (using php exec() to run these commands) the process does work - however, instead of updating the project that we've cd'd into, it updates the database structure of the deployment site!
It seems as if the php artisan migrate command ignores the fact I've cd'd into another project and it takes the database values from the current directory.
How can I go about changing this behaviour?
After playing around with multiple different solutions I eventually came to realise that the problem was the .env file from the project that I was in was setting the environment variables and then weren't being overwritten by anything therefore was then essentially running the code as the wrong site.
What I did to solve this
In my deploy script I manually loaded up the .env file and overwrote the environment variables with overload rather than just load.
Note: This will only work on versions below V5.
$dotenv = Dotenv::create(__DIR__);
$dotenv->overload();
UPDATE for V5
As per #nibnut's comment, since dotenv V5 (now in Laravel 7.0) this loads the environment variables as immutable. In my case I'm happy to allow these to be mutable, in which case I can do the following instead of the above.
$dotenv = Dotenv::createMutable(__DIR__);
$dotenv->load();
https://github.com/vlucas/phpdotenv#immutability-and-repository-customization
This was all I had to do to get my original script working.
NOTE: as this is a laravel install at all ends, the dotenv package was already installed. This could be used for any project by installing it separately.
Rather than cd to the directory, you could change the command to something similar to this:
php /var/www/sitea/artisan migrate --force
That will run the artisan command for the provided directory.
$log = shell_exec("unset DB_HOST &&
unset DB_PORT &&
unset DB_PASSWORD &&
unset DB_USERNAME &&
unset DB_DATABASE &&
composer dump-autoload &&
cd /var/www/yourproject/ && php /var/www/yourproject/artisan migrate:fresh &&
php /var/www/yourproject/artisan db:seed &&
php /var/www/yourproject/artisan seed:translation");
novocaine's answer unfortunately no longer works in Laravel 7.x, since the DotEnv file is loaded immutable.
My current workaround is to create a temporary .env file in my project (i.e. ".env.temp") then use the --env argument when calling my artisan commands:
php artisan migrate --env=temp
This may not work for all use cases, but if you're calling artisan commands in project B from within project A, it should work fine.
novocaine's answer involve reloading .env file. But you can also empty environment variables with env -i.
This method will avoid having some variables not overwritten due to different .env file variable name. For example, your deployment site may have a variable called DEPLOY_KEY in the .env that is non-existent on the .env of site2. Reloading .env file may leave the DEPLOY_KEY variable available on the site2 script. This may leads into security issues.
So, instead of running
exec('php artisan migrate --force');
you'll have just to add env -i:
exec('env -i php artisan migrate --force');
Source: https://unix.stackexchange.com/a/48995/375005

Can't get Symfony 2 to work with PHPstorm

I've installed the symfony plugin to PHPstorm, but when I'm trying to run the project I get the following error
Fatal error: Class 'Doctrine\Tests\Common\Cache\CacheTest' not found in ...
I've checked the path and CacheTest file is in the correct folder. When I run the symfony project from the command line it works just fine.
Is there someway to fix this problem?
What you try is not correct. You execute the php.exe and that is the normal PHP executable. To run your Unit-Tests you need PHPUnit to run.
Go to Preferences -> Languages & Frameworks -> PHP -> PHPUnit and define the phpunit.phar.
When you've done that you create a new configuration to run PHPUnit and you define the configuration which is under app/phpunit.xml.
I figured it out. First of all you need add phpunit.phar as Stony explained in his answer.
After that all I really needed to do was to rename phpunit.xml.dist to phpunit.xml in app/ folder and then just right click it and choose the Run 'phpunit.xml' option. This way the Tests succeed and it doesn't give any errors.
As for running the symfony project you need to add symfony command line tool support to phpstorm. All about that in this link: https://confluence.jetbrains.com/display/PhpStorm/Symfony2+Command+Line+Tool+Integration+-+Symfony+Development+using+PhpStorm
After that you just run it as a command like you would with normal command line. That's also explained in the link.
Try to choose "Use custom autoloader" option in Preferences -> Languages & Frameworks -> PHP -> PHPUnit and specify the path to vendor/autoload.php file.

How to create a migration in YII?

I have just started with YII and I am trying to create a new migration. I am using Xampp and it is located in the d drive of my system.
The command that I am trying to use is:
D:\xampp\php> d:\xampp\htdocs\yii\framework\yiic migrate create create_project_table
When I run the above command nothing happens, neither do I get an error message nor there is any error.
I did manage to successfully create a shell by using
D:\xampp\php> d:\xampp\htdocs\yii\framework\yiic shell d:\xampp\htdocs\trackstar\index.php
I tried to search for any solution for the migration issue but they all seem to be linux based. Please let me know what I am missing here. Thanks for your help.
I managed to create the migration sucessfully. Turns out that I needed to use the yiic located in the protected folder of my application instead of the yiic located in framework folder of yii. The command that I used is
D:\xampp\php>D:\xampp\htdocs\trackstar\protected\yiic migrate create create_project_table
I have found that you need a forward slash in my version.
yii migrate/create <migration_name>
First you need to go to protect folder on your Yii project
Then run the comamnd:
php yiic migrate create create_new_table_name_table

Symfony did not create model

I am going through symfony book and trying to do jobeet project(I use propel).. so here what book says
The ORM also generates PHP classes that map table records to objects:
$ php symfony propel:build --model
The propel:build --model task generates PHP files in the lib/model/ directory that
can be used to interact with the database.
I run that command and when i go to lib/ direcotry , there is no model folder :/
You have to write:
$ php symfony propel:build-model
Have you tried this?
sudo ./symfony project:permissions
./symfony project:cc
./symfony propel:build-all-load // this will write to your db as well
otherwise please consider pasting the output of propel:build-model and your symphony version :)

bash command not found, or other workaround?

I'm working with the book Agile Web Development with Yii. In Chapter 8, it creates a php script to set up a RBAC (role based access control) so that when we access the shell at
/framework/yiic shell
it should allow us to enter a command
rbac
Pursuant to the PHP script that we created, the command creates three roles, Owner, Member and Reader with various permissions.
My problem is that, working with MAMP, when I do the command
yiic shell
it says
-bash: yiic: command not found
Therefore, I can't create these roles, and then I can't continue along with the book, because I don't know any work around
Can anyone help?
UPDATE
pursuant to one of the answers below, I did chmod +x yiic
Now when I run /framework/yiic shell it seems to enter the shell but not the same one described by my book
The book tells me I should get this
% YiiRoot/framework/yiic shell Yii Interactive Tool v1.1 (based on Yii v1.1.2) Please type 'help' for help. Type 'exit' to quit. >>
Now type help to see a list of available commands:
>> help
At the prompt, you may enter a PHP statement or one of the following commands:
- controller
- crud
- form
- help
- model
- module
- rbac
Type 'help <command-name>' for details about a command.
However, I'm getting
USAGE
yiic shell [entry-script | config-file]
DESCRIPTION
This command allows you to interact with a Web application
on the command line. It also provides tools to automatically
generate new controllers, views and data models.
It is recommended that you execute this command under
the directory that contains the entry script file of
the Web application.
PARAMETERS
* entry-script | config-file: optional, the path to
the entry script file or the configuration file for
the Web application. If not given, it is assumed to be
the 'index.php' file under the current directory.
Try the full path:
/framework/yiic shell
You have to add /framework/ to your $PATH-environment variable if you want to use yiic without giving the full path.
You might need to specify the path:
/framework/yiic
or include the directory in your PATH
PATH="$PATH:/framework"
export PATH

Categories