sfDoctrineGuardPlugin and admin filters - symfony1

I have an admin that uses sfDoctrineGuardPlugin. I also have another table, sf_guard_user_profile, that extends the sf_guard_user table, to include more fields, such as address, age etc
My problem is, I am embedding the profile (sf_guard_user_profile) information into the sf_guard_user record when viewing the records in the admin generated module. This works fine, but I'd like to then be able to use some of the filters from the sf_guard_user_profile table.
I have tried to add these into the generator.yml file, but I this throws an error
generator.yml
...................
filter:
display: [username, email_address, address_1, is_active ]
Widget "address_1" does not exist.
address_1 is a field in sf_guard_user_profile
Is this possible to do?
Thanks

Ok, so I had to do a little bit of work in the sfGuardUserFormFilter class,
$this->widgetSchema['age'] = new sfWidgetFormInputText(array(
'label' => 'Age'
));
//Type of validator for filter
$this->validatorSchema['age'] = new sfValidatorPass(array ('required' => false));
public function getFields()
{
$fields = parent::getFields();
$fields['age'] = 'age';
return $fields;
}
public function addAgeColumnQuery($query, $field, $value)
{
$rootAlias = $query->getRootAlias();
$query->where('p.age LIKE ?', '%'.$value.'%');
//remember to return the $query!
return $query;
}
I could then use the age field in the generator.yml file.
Thanks

Related

How to get entity manager in form type

How could I get entity manager when building forms?
I would like to search results from the database and build the choices for choicetype.
I know I could use entitytype instead but in this situation I want to record string in database than an object.
And also I need to add some more options as well.
Thank you.
In Symfony 3.2 (and possibly others, I'm not sure about 3.1, but it is probably the same), the $this->createForm() method needs a string as the first parameter, and cannot take a form object.
Add a configureOptions method to your form class:
class YourFormType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'entityManager' => null,
]);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Entity Manager is set in: $options['entityManager']
}
}
Then get the form in your controller like so, passing in the Entity Manager:
$form = $this->createForm(
YourFormType::class,
$yourEntity,
[
'entityManager' => $this->getDoctrine()->getManager(),
]
);

Manual SQL Query from Form Type / Abstract Class

First of all, I am moving from Symfony 1.4 to Symfony 3. (Yes, I was kicking and screaming at first)
My Question: I am running a manual query from the following FormType class that is for a chunk of my Signup form. I am moving the Address part of the signup into its own class. I am calling a geographical table to get my states list and I have a Union... hence this is why I am not calling an entity class.
The problem is that I need to connect to the database but cannot because it's an Abstract class. If I run this in the Controller Class, no problem, but can't do it herein this Abstract class. I have a bunch of manual steps to go through before making any inserts, so I can just
How do you make the following work? I have not created any services, but if I put all of this into a controller class, then it works fine.
<?php
namespace LocationBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Doctrine\ORM\EntityManager;
class PartialAddressType extends AbstractType
{
private function getStatesList()
{
$sql = "
SELECT
'0' AS id
,'' AS name
,'' AS abbreviation
,'Select State' AS display
UNION
SELECT
id
,trim(name)
,trim(abbreviation)
,CONCAT(trim(abbreviation), ' - ', trim(name)) AS display
FROM
geo_state
WHERE
type = 'state'
ORDER BY abbreviation ASC; ";
$manager = $this->getDoctrine()->getManager('default');
$conn = $manager->getConnection();
$rs = $conn->query($sql)->fetchAll();
return $rs;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$rs = $this->getStatesList();
// This one is for a Select
$builder->add('locationState', ChoiceType::class, array(
'expanded' => FALSE,
'multiple' => FALSE,
'choices' => $rs(),
'choice_label' => $rs['display'],
'choice_attribute' => $rs['abbreviation'],
'preferred_choices' => array('TX'),
'choices_as_values' => FALSE,
'label' => 'State',
)
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array());
}
public function getName()
{
return 'location_bundle_partial_address_type';
}
}
I suggest you following steps:
1) Move the query in a separate repository class (I hope you map the table as an entity)
2) Use an EntityType Field with the Custom query as described here
3) Avoid the UNION using the placeholder
Last tips, You should implements the getBlockPrefix method instead of getName as described in the migration guide here.
Hope this help

Kohana ORM table join

I have 2 tables
user (id, name, position_id)
position (id, name)
how can i join the to models, so i can do something like this.
ORM::factory('user')->position()->name
See http://kohanaframework.org/3.3/guide-api/ORM#property:_belongs_to
class Model_User extends ORM {
protected $_belongs_to = array(
'position' => array('model' => 'Position')
);
}
Now you can:
ORM::factory('User')->with('postion')->find()->position->name;
Or indeed, with an already loaded user (e.g. $user = ORM::factory('User', 1);
$user->position->name;

symfony - adding a user to a group (sfGuardUserGroup) in a form

I'm trying to save some users in a custom admin form and I'd like to set them in a particular group, in the sfGuardUserGroup.
So If the user I've just created has an id of 25, then I'd expect an entry in the sfGuardUserGroup table with a user_id of 25 and a group_id of 8 (8 is my group id I want o add these users to.)
Could I do this in the form class, or in the processForm action?
I'm using doctrine and SF1.4
Thanks
This should do what you need:
<?php
class AdminUserForm extends sfGuardUserForm
{
public function configure()
{
//customise form...
}
public function save($con = null)
{
//Do the main save to get an ID
$user = parent::save($con);
//Add the user to the relevant group, for permissions and authentication
if (!$user->hasGroup('admin'))
{
$user->addGroupByName('admin');
$user->save();
}
return $user;
}
}
If you require this behaviour for all sfGuardUser's created you should put this logic in the model for sfGuardUser class. [example below]
// sfGuardUser class
public function save(Doctrine_Connection $conn = null) {
if (!$this->hasGroup('group_name'))
$this->addGroupByName('group_name', $conn);
parent::save($conn);
}
If you require this functionality only on this specific form, you should put the logic within the form. Adding logic to the processForm action would be incorrect as you would be placing business logic within the controller.

symfony: trying to add a field from another model in a backend list. Inner join

I have created an inner join criteria to show a list of elements in the backend.
This are my models:
user:
name: { type: varchar(255) }
age: { type: integer }
article:
title: { type: varchar(255) }
content: { type: varchar(255) }
user_id: { type: varchar(255) }
and this is my generator.yml (a part) of the article module:
list:
peer_method: getArticles
display: [title, content, age]
And this is the method getArticles():
public static function getArticles()
{
$con = Propel::getConnection();
$sql = "select * from article LEFT JOIN user ON article.user_id = user.id";
$stmt = $con->prepare($sql);
$result = $stmt->execute();
$articles = self::populateObjects($stmt);
return $articles;
}
As you can see I want to show the column corresponding to the "age" field, so when I try to show the list of articles an error that says something like "The method Articles::getAge() is not defined".
So I think I should create the method Articles::getAge(), but.. what should i write inside? A new criteria that retrieves the user object corresponding to the value of the field user_id of the article? Or maybe am i wrong with any other stuff?
sf 1.4/propel
Regards
Javi
Yes:
class Articles
{
public function getAge()
{
return $this->getUser()->getAge();
}
}
or similar code to retrieve the value based on your related model - I use Doctrine normally, but from my hazy Propel memory I think the above should work :-)

Resources