How to access database from configure method of a symfony task - symfony1

You can create a symfony "task" by extending sfBaseTask; see http://www.symfony-project.org/cookbook/1_1/en/tasks for more details.
However, I cannot see how to set up a database connection in the configure section. I want to do this so that I can have the detailedDescription property show some options that are only defined in the database.
What is the best way to do this?

If you check out the way symfony creates a custom Task Skeleton (./symfony generate:task yourTaskName), you would see code that looks like this inside the execute function:
protected function execute([..])
{
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'])->getConnection();
[..]
The problem with this code is that it uses the default configuration, but if you know what connection you want to open (check your database.yml or schema.yml to figure out the name) it should be really easy, with code that looks like this:
protected function configure()
{
$databaseManager = new sfDatabaseManager(sfProjectConfiguration::getApplicationConfiguration('frontend'‌​, 'prod', true));
$connection = $databaseManager->getDatabase("the_name_of_your_connection")->getConnection();
[..]
Then you can access your models the usual way, e.g.:
$myItem = Doctrine_Core::getTable('item')->find(14);
echo $myItem->getId();

If you are using Doctrine, the best way to do that is defining a method into the php model file. The model's files are located into /PROJECT/lib/model/doctrine/MODEL_NAMETable.class.php
Be carefull with the MVC model!
Once you have your own method, the way to call it is: Doctrine_Core::getTable('TABLE_NAME')->miNewMethod($params-if-any);
I hope I'll help you!

Related

How do you call a constructor from using getArtefact in Grails 3

I need a list of my domains, and I am getting them by using:
def domains = grailsApplication.getArtefacts("Domain")*.clazz
In each of my domains is a constructor, I want to do something like this:
def item = new domains[0](object)
item.save()
essentially making a generic save.
I figured out a way to invoke the constructor. I don't know if it is the best way to do it. Example code below:
//provides all Domain classes in your project in an ArrayList
//You can also use getArtefact("Domain", "classname")*.clazz to return a subset
def domains = grailsApplication.getArtefacts("Domain")*.clazz
def domain = domains[0].newInstance(object) //Whatever your constructor wants variable: "item" works as well
def domain.save()
Got this working thanks to http://mrhaki.blogspot.ca/2010/06/groovy-goodness-create-class-instance.html

Laravel 4: how to inject another class in a eloquent model

I'm trying to use the built-in laravel's Ioc container to inject a PageManager class inside a Page model and I'm a little lost.
What I'm trying to achieve is something like that:
class Pages extends Eloquent {
public function __construct(PagesManagerInterface $manager, array $attributes = array())
{
parent::__construct($attributes);
$this->manager = new $manager;
}
public function saveToDisk()
{
$this->manager->writeToFile();
}
But I obtain this error:
ErrorException: Argument 1 passed to Pages::__construct() must be an instance of PagesManagerInterface, none given.
I tried to add this in app/start/global.php:
App::bind('Pages',function(){
return new Pages(new PagesManager);
});
But is seems ignored by the framework, and also i don't know how to insert the $attribute array into this declaration.
I'm a little lost so any help is appreciated!
It's not a good idea to overload a model's constructor because new instances can be spawned behind the scenes through various methods, like Model::find().
When that happens, the dependencies you're asking for in your custom constructor aren't being passed in because the Model class isn't aware of them. So, you get that error message.
See the find() method here: http://laravel.com/api/source-class-Illuminate.Database.Eloquent.Model.html#380-397
See this post by Jason Lewis: http://forums.laravel.io/viewtopic.php?pid=47124#p47124
I think that what you need is:
App::bind('PagesManagerInterface',function(){
return new Pages(new PagesManager);
});
This tells Laravel to inject a new Page object everytime it needs an instance of your PagesManagerInterface wich wasn't passed while creating the model.
In Laravel you can use the IoC Container:
public function saveToDisk(){
$managerObject = app()->make('path\to\class\PagesManagerInterface');
$managerObject->writeToFile();
}

BjyAuthorize settings file connect with database table

I am using BjyAuthorize to control access in my project.
Everything working as expected with hard coded settings in module.bjyauthorize.global.php file. But my requirement is to set users dynamically and assign their user levels dynamically.
So I want to connect this file to DB tables some how and dynamically load settings. Please someone help me to get my thing done
Thank you
You have to create your own Providers or Guards that will load the settings from the database.
See BjyAuthorize\Provider\Role\ZendDb or BjyAuthorize\Provider\Role\ObjectRepositoryProvider.
You can set settings in the Module class.
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $event)
{
$serviceManager = $event->getApplication()->getServiceManager();
$authorizeService = $serviceManager->get('BjyAuthorize\Service\Authorize');
// settings
}
}

Unit Testing Claims in .Net MVC app

Background : We are using MVC4 and using WIF for Claims/Authorization. We are using Moq/MvcContrib for Mockup Objects. I have looked here and created the MockIdentity and MockPrincipal Objects - do I need them?
Goal : I have a controller class that has a class level attribute that only allows users with 'Manager' claim to access the actions. I want to create mock users and test to see if anyone that doesn't have 'Manager' claim can access the actions or not.
I get the mock concept but I have only dealt with the data objects mocking and having a tough time figuring out what plugins/classes/methods/setups I need in place to do what I need to do.
Thanks in advance.
I want to create mock users and test to see if anyone that doesn't have 'Manager' claim can access the actions or not.
No, you don't. You just want to pass users to that attribute you wrote and test that sets the filterContext.Result correctly. That's it. You don't need to test that System.Web.Mvc works. Single unit under test!
Presumably your attribute is an AuthorizeAttribute, correct? So you need to test OnAuthorization(AuthorizationContext).
Disclaimer: I haven't used moq in a while, but your code would presumably look generally like this:
var user = new Mock<IPrincipal>();
user.Setup(/* whatever you need to look at */);
var authContext = new Mock<AuthorizationContext>();
authContext.Setup(ac => ac.HttpContext.User).Returns(user);
var myAttribute = new RequireManagerAttribute();
myAttribute.OnAuthorization(authContext);
authContext.VerifySet(ac => ac.Result = /* whatever you expect */);

Accessing Orchard CMS settings programatically inside a Module

I am writing an Orchard CMS module within a multi-tenant application.
I would like to be able to access the settings declared when the tenant was set up, namely the DB table prefix which i'd like to use as a unique identifier for the current tenant in other areas of my system.
Is there an API/Helper I can query for these settings?
Cheers.
Get the site item from the work context. It has all the settings as parts. For the table prefix specifically it's a little different: you need to inject ShellSettings. But I would question the need to do that first...
I have found this, if it helps:
private readonly ISiteService _siteService;
public MyController(ISiteService siteService)
{
_siteService = siteService;
}
public void MethodExample(){
var myVar = _siteService.GetSiteSettings().BaseUrl;
}

Resources