Sonata AdminBundle and UserBundle: how can I find this parameter? - symfony

After "finish" studying Symfony2, I start to take a look to Sonata Admin and User bundles since, those bundles, could help me in some future works.
On the firsts, all seems to me terribly complicated and difficult to understd. But with few hours of architecture study, applied to real files,classes and so on, I suppose that I've understood perfectly what's the essence of that bundle and how this works.
My only "black hole" into whole thing understand is, where can I find the parameter used into sonata.user.admin.user, called sonata.user.admin.user.entity? As far I know, that parameter simply point to user class that is responsable for read/write operation from and to database.
I ask this because, after dozens of minutes spent looking, the only place where I found those information are
./app/cache/prod/appProdProjectContainer.php
./app/cache/dev/appDevDebugProjectContainer.xml
./app/cache/dev/appDevDebugProjectContainer.php
Is possible that this variable is defined only there?
If yes, because is placed into cache folder? Is loading time faster?

You should also look through Extensions in DependencyInjection folders - sometimes arguments are defined in yaml in one way, but after loading with concatenation they are transformed into another:
If in your parameters you have something like:
your_user: 'Somename'
And in DependencyInjection it is parsed as follows:
foreach($config as $key => $value) {
$container->setParameter('your.prepend.'.$key, $value);
}
Then in the end you'll have your parameter compiled with index your.prepend.your_user.
I guess, the same logic is used in mentioned bundles.
UPDATE:
Example of usage of described logic

Related

How to filter in Symfony2?

First of all i am very new to Symfony 2 and started to learn.
Is there any possibility for filter a value? Perhaps filter chains too?
I know this concept from Zend Framework 1 and 2.
E.g.:
i have a string "1A- N "
now i want to filter so that only numeric values pass; result "1"
Do i have to implement this on my own i Symfony?
I would like to do something like:
$text = '1A - N';
$numberFilter = new NumberFilter();
$filteredText = $numberFilter->filter($text);
//now in $text i find '1'
But for now i nowhere found something like this in Symfony what surprises me a lot. I thought it is a full stack framework and such function is so basic.
I found something like validators but they only say if a value e.g. contains only numbers or not. Or is the validation concept of symfony like that it does not only say if it is numeric or not but filter all other smybols out, too?
Depending on what you want precisely:
disallow user input not fulfilling certain rules
use validators in forms
use asserts in entities
chenge user input in case it's wrong
use viewransformers in forms
use event listeners in forms
use event listeners for doctrine
change data that already exists in the database
use filters in twig
create a command to execute from commandline
You can also try http://php.net/manual/ro/filter.filters.sanitize.php
I have built quite big apps with Symfony and never needed such a feature. Filters are mostly used in views anyway. Symfony comes with Twig, which has filters, that can be chained, and you can write your own filters. But if you need filters in the backend to do some background processing, you can accomplish it the way you suggested.
I suggest you write an interface and use a factory pattern, so you set a standard if you make many filters, so it will be easier to make the chaining work;)
After the answers and searching i got to the following conclusion.
There is no concept for this for now in Symphony 2.
You have to write it on your own.

symfony2 class and repos architetture

I'm building an application in symfony2 just to better understand how it works. My problem is I'm undecided on where to put some classes, for example:
I have a category entity and a category repository, and a controller to manage categories where I query the repos (how explained in the cookbook) using
$category = $this->getDoctrine()->getRepository()
now I'm working on a blog controller, I want to display a form (for example in edit mode), so I have to query the blog repos, but I also need to query again the category repos to permit to choose a category.
I don't think it could be a good idea to query 2 different repos in the controller, can anyone suggest me how to organize this classes just to avoid to instantiate all the repository in all the actions I need them?
any other useful suggestion on how to organize the code?
thanks
There is nothing wrong with calling methods of 2 (or more) repositories in a single controller.
But in the scenario you describe the Form Component will take case of querying for categories to choose from. I suggest you dive a litter deeper in that documentation. Hint: the Entity Field Type .
Some advice
You probably know what "spaghetti code" is, but there's also something called "lasagna code" which is the opposite: you get pillars of classes that don't really work together.
You could have a pillar for users (UserEntity, UserRepository, UserService and UserController) and another for blog-posts (BlogPostEntity, BlogPostRepository, BlogPostService and BlogPostController), etc...
This is also something you rather not create, so a good mix between spaghetti and lasagna would be advisable ;)
Try to have services represent your business needs / the business world (or Domain). Controllers are nothing more than connectors between clients (browsers, etc) and your services. And repositories are just technical details (not your points of focus).
PS: Little correction
You should change this:
$category = $this->getDoctrine()->getRepository();
to this:
$categoryRepository = $this->getDoctrine()->getRepository('Category');
It will be less confusing for you in the long run. Example:
$categoryRepository = $this->getDoctrine()->getRepository('Category');
$category = $categoryRepository->find(123);

Access Session from EntityRepository

I'm using Symfony2 with Doctrine2. I want to achieve the following:
$place = $this->getDoctrine()->getRepository('TETestBundle:Place')->find($id);
And on that place will be the info of the place (common data + texts) on the user language (in session). As I am going to do that hundreds of times, I want to pass it behind the scenes, not as a second parameter. So an English user will view the place info in English and a Spanish user in Spanish.
One possibility is to access the locale of the app from an EntityRepository. I know it's done with services and DI but I can't figure it out!
// PlaceRepository
class PlaceRepository extends EntityRepository
{
public function find($id)
{
// get locale somehow
$locale = $this->get('session')->getLocale();
// do a query with the locale in session
return $this->_em->createQuery(...);
}
}
How would you do it? Could you explain with a bit of detail the steps and new classes I have to create & extend? I plan on releasing this Translation Bundle once it's ready :)
Thanks!
I don't believe that Doctrine is a good approach for accessing session data. There's just too much overhead in the ORM to just pull session data.
Check out the Symfony 2 Cookbook for configuration of PDO-backed sessions.
Rather than setting up a service, I'd consider an approach that used a Doctrine event listener. Just before each lookup, the listener would pick out the correct locale from somewhere (session, config, or any other place you like in the future), inject it into the query, and like magic, your model doesn't have to know those details. Keeps your model's scope clean.
You don't want your model or Repository crossing over into the sessions directly. What if you decide in the future that you want a command-line tool with that Repository? With all that session cruft in there, you'll have a mess.
Doctrine event listeners are magically delicious. They take some experimentation, but they wind up being a very configurable, out-of-the-way solution to this kind of query manipulation.
UPDATE: It looks like what you'd benefit from most is the Doctrine Translatable Extension. It has done all the work for you in terms of registering listeners, providing hooks for how to pass in the appropriate locale (from wherever you're keeping it), and so on. I've used the Gedmo extensions myself (though not this particular one), and have found them all to be of high quality.

use generated entities as "base class" in sf2/doctrine2

How can I tell Doctrine2 Entity Manager to use an inherited class by default, instead of the generated one?
In my new sf2 app, I generated all entities from the existing database to /src/Package/Bundle/DataBundle/Entity - of course I'd like to have generated forms as well, so I need for my foreign-key relations __toString() methods, which I don't want to put into those generated files because they get overwritten (yes they also get backuped in an extra file, so my changes aren't lost, but manually merging files is not what I want).
So I added new classes to /src/Package/Bundle/DataBundle/Model inheriting all from the entities, but with a __toString() method and surely some other tweaks in the future as well. But now, when I call $entity = $em->getRepository('PackageDataBundle:Customer')->find($id); - I get an instance of /src/Package/Bundle/DataBundle/Entity/Customer instead of /src/Package/Bundle/DataBundle/Model/Customer ..
I'd like to have the behaviour from sf1, where all custom work is done in the inherited classes and the generated ones are the "base" Classes, which can be updated any time on a schema update and aren't touched otherwise..
I hope there is some configuration for this..
Maybe as a bonus, I'd like to have my naming convention turned around: to have Model as the "abstract" generated one and Entity as the actual used one
Thanks.
I'd like to have the behaviour from sf1, where all custom work is done in the inherited classes and the generated ones are the "base" Classes, which can be updated any time on a schema update and aren't touched otherwise..
For purposes you described you should use Propel ORM - it was so in Symfony-1.4, and became even more flexible for Symfony-2.0 with Propel 1.6. It is well documented, easily installed and naturally used within Symfony-2.0
Perhaps I misunderstood your problem, but going from Propel to Doctrine i was also disappointed that there is no way to keep that "dirty" auto-generated code out of my logic.
I do not know how much it is now important to you, but I currently writing a simple bundle that implements such generation style.
$ app/console doctrine:generate:entities СompanySomeBundle --propel-style=true
Generating entities for bundle "СompanySomeBundle"
> backing up User.php to User.php~
> generating Сompany\SomeBundle\Entity\Base\User
> generating Сompany\SomeBundle\Entity\User
https://github.com/madesst/MadesstDoctrineGenerationBundle
PS: Sorry for my english =/

using module_invoke_all to submit a form question

here is more detailed explanation:
i am using the ubercart module with the file download feature module (the uc_file module ).
i have created a product class (which is a new content-type as far as the drupal system) and add a cck file field to it.
what i want to achive is the following behavior:
once a user saves a new node of my product class, i want the uploaded file to be added as a file download feature to the product class automatically.
i know i can hack the function uc_file_feature_form_submit($form, &$form_state), do what it does in my module code, but i rather ivoke it since i'll have easier life with future changes to the uc_file module (since i am calling it's function, i dont care if it will change in the future).
so, to invoke the uc_file_feature_form_submit function i need to build fake $form, &$form_state parameters, i know i can print_r those arrays, and build it from there, the thing is that there are a lot of data in those arrays that is not mandatory, i was wondering what are those mandatory fields that i have to build on my own.
thank you...
Short answer: Look at the submit function you are trying to invoke. The form values that it's using are the ones you need.
Long answer . . . need more info before I can give a better answer.
You can use drupal_execute() to programatically execute a form. I am not sure if it works with files though.

Resources