execute yii class methods in yii shell in the context of the yii app - yii

Is it possible to execute yii class or instance methods in yii shell.
Say for example, i want to print all the records of a table, the command for this is Post::model()->findAll. But can I execute this in the yii shell, in the context of the yii web app.
Also, is it possible to access the components of an yii application (like db) in the yii shell.
In short i could like to execute some yii methods in the context of the yii application (much like executing small javascript statements in the firebug console).
Edit 1
Found one solution as mentioned below:
php C:\xampp\htdocs\trackstar\protected\yiic shell C:\xampp\htdocs\trackstar\protected\config\main.php
and then execute the yii commands.
Edit 2
echo Project::model()->findByPk(3);
is giving error object of class project cant be converted to string. Is there any way to print or pretty print the yii objects in the console.

This has nothing to do with Yii. It's a php questions. Anyways, you can try this -
var_dump(Project::model->findByPk(3));
// or
print_r(Project::model->findByPk(3));
or better still use CVarDumper from yii -
CVarDumper::dump(Project::model->findByPk(3);
Documentation on CVarDumper - http://www.yiiframework.com/doc/api/1.1/CVarDumper

Related

Grails compile .gsp to .html from gradle/command line

Is there any way that I can compile a gsp view into an html given a provided model from a groovy script without having to run all the Grails application?
The use case is that due to client demands we have to use Javascript/jQuery to create the front-end of the application. We've already had the architecture definition, but we're having issues creating integration front-end tests since our front-end composes of .gsp, javascript and css, all componentized.
For instance: Button may have a .gsp a .js and a .css associated to it.
Ideal solution to create front-end component integration tests: Have the .gsp compiled into html before the tests run so we can run the assertions in the *.test.js files. Since we don't need database, services or other instances to compile the .gsp, no need for the application to be running, avoiding the time to boot up the app.
Thanks in advance!
Next code should help you.
File gspFile = new File(BuildSettings.BASE_DIR, "grails-app/view/path/to/view/${viewName}.gsp")
This code will find gsp, you can find it by absoluteUrl
if(gspFile.exists()) {
def model = model((Class)scaffoldValue)
def viewGenerator = new GStringTemplateEngine()
Template t = viewGenerator.createTemplate(gspFile)
def contents = new FastStringWriter()
t.make(model.asMap()).writeTo(groovy.lang.Writer)
}
Writer you should find by yourself, think that it can be a stream to the file.
Didn't test it, but it should work :)

Generating CRUD in symfony 4

After releasing Symfony 4.0, there is no support for SensioGeneratorBundle. hence the command php app/console generate:doctrine:crud is not available.
They suggest to use MakerBundle, but I could not find appropriate replacement for CRUD generation.
Could anyone help?
You can use the make command in Symfony4+ (and it's quite an improvement!), from the MakerBundle:
php bin/console make:crud
It'll prompt you for which entity you want the crud for. It generates a controller with index, new, update, view and delete methods in /src/controller, with matching templates in /templates.
Useful to know: If you run make:entity, and later run that command again and enter an existing entity, it responds with:
Your entity already exists! So let's add some new fields!
At the moment MakerBundle supports just a few core commands. You can see the list here. Unfortunately there's no CRUD generator. But there some discussion about it in the issues so you can follow what will be done.
If what you need is just a generator for boilerplate code and not a more structured solution like EasyAdminBundle you should consider creating your own maker.
Symfony4 (thanks to #jelle)
composer require symfony/maker-bundle --dev
composer require symfony/form symfony/validator symfony/twig-bundle symfony/orm-pack symfony/security-csrf
php bin\console make:crud
The class name of the entity to create CRUD (e.g. BravePuppy):
>
first install pre-req packages
composer require twig-bundle security-csrf
and then you can run
php bin/console make:crud
after that just enter your entity name which you want to curd
The class name of the entity to create CRUD (e.g. BlogPosts):
>

How to set cron job in yii

I am quite newbie to yii. I am working on a project.
I have written a function to send automatic reminder to clients
say this function is at url :
http://somedomain.com/index.php/somecontroller/someaction
I want to set the cron for this url.
one method is that I should write GET cron_job_url.
But I dont want to use the url for my cron.I only want to use physical path of the controller and action. Is this possible with yii ?
If you want to use a cron job, I'd suggest to write a yiic command instead of calling a URL. It's very simple and you don't have to deal with URL access permissions.
Create a new class that extends from CConsoleCommand and implement either a run() method or some actions as you would in a controller. You can find more information on console commands here. You have to save the command to the protected/commands directory and the class name must end in Command.
If your command is called DemoCommand then you can call it from a cron job as /path/to/your/webroot/protected/yiic demo.

Application modules with Pyramid

I'm creating a workflow app with pyramid and i'm searching how to make the application modulable : meaning create a core app with sqlalchemy models, base forms with wtforms, and some base templates with mako.
The basic structure of the "Core" app is:
App_Core/core.ini
/setup.py
/...
/App_Core/
/__init__.py
/models.py
/forms.py
/utils.py
/templates/
/templates/base.mako...
/static/
/static/staticfiles...
My goal is to create 1 application per workflow which will be included in the Core app : it seems possible to do that via the includeme function provided with pyramid.
I want to include each workflow via the core.ini file, for example:
pyramid.includes =
workflow_app1
workflow_app2
workflow_app3
...
I defined an new app called workflow_app1 with the following structure:
worflow_app1/
/setup.py
/...
/workflow_app1/
/__init__.py
/models.py
/forms.py
/views.py
/templates/
/templates/workflow_app1.mako
/...
And the _init_.py file will contain the includeme function and will define new routes:
def includeme(config):
config.add_route('route1', '/route1/')
config.add_route('route2', '/route2/')
config.scan()
When i'm writing a view for the worflow_app1, i'm rendering to a template included with that app, but when i'm calling it from the core app, it can't render the template and gives the following error:
TopLevelLookupException: Cant locate template for uri 'workflow-app1.mako'
This error quite logical cause the mako.directories directive is given with the path App_Core_PATH/templates so my template should be in the same folder.
Question1:
Is it possible to make mako searching in each folder of modules the wanted templates?
Question2:
Is it possible to make the workflow-app1.mako inheriting of the base.mako from the core app?
Thanks by advance for your answer.
The solution that I would recommend is switching to asset specs for your templates. They are explicit, allow overriding, and provide better control over your template hierarchy. This means that you would stop using mako.directories and instead use 'workflow_app1:templates/workflow_app1.mako' in your inherits or include or renderer arguments. Given this, it's obvious that you can inherit from your base.mako in your core app, whereas managing the mako.directories option is more difficult.
If you're deadset on mako.directories then you can add a line to it every time you add a package to pyramid.includes.
mako.directores =
App_Core:templates
workflow_app1:templates
workflow_app2:templates
Another option is to switch to jinja2, as its plugin has the ability to add search paths after the fact. Thus your included modules can config.add_jinja2_search_path(...) throwing themselves into the lookup order. Pyramid's mako integration does not offer this option right now.

Rails 3: Choose and run a Mechanize script from inside Rails action.

My application scrapes information from various sites using Mechanize. Naturally, each site requires custom Mechanize code. Each site is stored in my database, including the url to scrape and the string name of an .rb file containing that site's Mechanize code. For this case, let's assume the scripts are available in the assets folder.
I would like to call http://example.com/site/:id, then have the show action dynamically choose which Mechanize script to run (say, #site.name + ".rb" ). The script will massage the data into a common model, so all sites can use the same show template.
I can't find a way to dynamically load a .rb script within an action and obtain the result. It may be easier to have the scripts return a JSON string, which I can parse before passing on to the template, but I can't see a solution for that either. Ideally, the script will run in the action's scope. The ugly solution is an enormous if-else chain (testing the site name to determine which code block to run), but there must be a better way.
Any suggestions would be greatly appreciated, as would any general solutions to running different code dependent upon the properties of database objects.
If you have all the code in your app already why are you eval'ing Ruby code?
Create classes like:
class GoogleSpider < Spider; end
class NewYorkTimesSpider < Spider; end
class SomeOtherSpider < Spider; end
And the site class will hold the class name that will be used, so you would be able to easily do something like this in your controller action:
def show
#site = Site.find(params[:id])
# name contains SomeOtherSpider
#process_output = #site.name.constantize.new.process
# do something with the output here
end
And then you don't need to mess around evaluating Ruby code, just call the class needed. You can even make them all singletons or keep them all in a hash for faster access.