Creating new users relation isn't saved, what's wrong? - symfony1

I'm working on a module that uses sfDoctrineGuard plugin as base (means uses sfDoctrineGuard) so in my module I developed this code:
class UsuariosForm extends sfGuardUserForm {
protected $current_user;
public function configure() {
unset(
$this['is_super_admin'], $this['updated_at'], $this['groups_list'], $this['permissions_list'], $this['last_login'], $this['created_at'], $this['salt'], $this['algorithm']
);
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
$this->setDefault('idempresa', $id_empresa);
$this->current_user = sfContext::getInstance()->getUser()->getGuardUser();
$this->validatorSchema['idempresa'] = new sfValidatorPass();
$this->widgetSchema['first_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['last_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['username'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['email_address'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['password'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));
$this->widgetSchema['password_confirmation'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));
$this->validatorSchema['password']->setOption('required', true);
$this->validatorSchema['password_confirmation'] = clone $this->validatorSchema['password'];
$this->widgetSchema->moveField('password_confirmation', 'after', 'password');
$this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_confirmation', array(), array('invalid' => 'The two passwords must be the same.')));
}
public function save($con = null) {
if (sfContext::getInstance()->getActionName() == "create" || sfContext::getInstance()->getActionName() == "new") {
$new_user = parent::save($con); /* #var $user sfGuardUser */
$new_user->addGroupByName('Monitor');
}
return $new_user;
}
}
The first function allow me to have my own form instead of sfDoctrineGuard plugin form and the second one is a override of save() method for add a default group to the new users I'm creating. I want also add a default idempresa as you may notice (in config() function) but it's not working, maybe I'm doing something wrong or don't know. idempresa is a field stored in a sfGuardUserProfile table and have of course the relations configured and so on. My question here is: what should be the right way to setup the default idempresa in order to set the profile when users are created?

You have to save the $new_user object again: $new_user->save($con)
Also you don't have to check for action_name in the save() method, you can check it the object is new or not. The Objectform has a method for that.
<?php
...
public function save($con = null)
{
$new_user = parent::save($con);
if($this->isNew())
{
$new_user->addGroupByName('Monitor');
$new_user->save($con); //this saves the group
}
return $new_user;
}
...

Related

ZF2 on post update record (not working)

I am trying to update records in my database. I am following a book but something isnt working.
This is the edit action. On post form action leads to process action.
public function editAction()
{
$userTable = $this->getServiceLocator()->get('UserTable');
$user = $userTable->getUser($this->params()->fromRoute('id'));
$form = $this->getServiceLocator()->get('UserEditForm');
$form->bind($user);
$viewModel = new ViewModel(array(
'form' => $form,
'user_id' => $this->params()->fromRoute('id')
));
return $viewModel;
}
Process action
public function processAction()
{
// Get User ID from POST
$post = $this->request->getPost();
$userTable = $this->getServiceLocator()->get('UserTable');
// Load User entity
$user = $userTable->getUser($post->id);
// Bind User entity to Form
$form = $this->getServiceLocator()->get('UserEditForm');
$form->bind($user);
$form->setData($post);
// Save user
$this->getServiceLocator()->get('UserTable')->saveUser($user);
}
And this is the class UserTable with function save user:
public function saveUser(User $user)
{
$data = array(
'email' => $user->email,
'name' => $user->name,
'password' => $user->password,
);
$id = (int)$user->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getUser($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('User ID does not exist');
}
}
}
There is no error showing. It passes $this->tableGateway->update and just nothing !
EDIT: I can delete users, add users.
u miss this
if ($form->isValid()) {
$this->getServiceLocator()->get('UserTable')->saveUser($form->getData());
}
After validation you can now retrieve validate form data with $form->getData().
Also note that because of binding entity to form via $form->bind($user) $form->getData() is an instance of User
Hope it helps ;)
I dont know why but i must check if form is valid.
if($form->isValid()){
// do the save
}

How to change sessionid value Zend Framework 2

i want to change PHPSESSID name and value. I can could name but i couldn't change value.
i have following stracture. How i change sessionid value.
My module.config.php is
return array(
'session' => array(
'config' => array(
'class' => 'Zend\Session\Config\SessionConfig',
'options' => array(
'name' => 'portal1'
),
),
'storage' => 'Zend\Session\Storage\SessionArrayStorage',
'validators' => array(
array(
'Zend\Session\Validator\RemoteAddr',
'Zend\Session\Validator\HttpUserAgent',
),
),
),
);
My Module.php
public function onBootstrap($e) {
$this->bootstrapSession($e);
}
public function bootstrapSession($e) {
$session = $e->getApplication()
->getServiceManager()
->get('Zend\Session\SessionManager');
$session->start();
$container = new Container('initialized');
if (!isset($container->init)) {
$session->regenerateId(true);
$container->init = 1;
}
}
public function getServiceConfig() {
return array(
'factories' => array(
'Zend\Session\SessionManager' => function ($sm) {
$config = $sm->get('config');
if (isset($config['session'])) {
$session = $config['session'];
$sessionConfig = null;
if (isset($session['config'])) {
$class = isset($session['config']['class']) ? $session['config']['class']
: 'Zend\Session\Config\SessionConfig';
$options =
isset($session['config']['options']) ? $session['config']['options'] : array();
$sessionConfig = new $class();
$sessionConfig->setOptions($options);
}
$sessionStorage = null;
if (isset($session['storage'])) {
$class = $session['storage'];
$sessionStorage = new $class();
}
$sessionSaveHandler = null;
if (isset($session['save_handler'])) {
$sessionSaveHandler = $sm->get($session['save_handler']);
}
$sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);
if (isset($session['validator'])) {
$chain = $sessionManager->getValidatorChain();
foreach ($session['validator'] as $validator) {
$validator = new $validator();
$chain->attach('session.validate', array($validator, 'isValid'));
}
}
} else {
$sessionManager = new SessionManager();
}
Container::setDefaultManager($sessionManager);
return $sessionManager;
},
),
);
}
MyController.php is; i want to change PHPSESSID key and value here.
public function loginAction() {
$container = new Container(); /*
i want to change PHPSESSID key and value
eg: portal1: fafsafg43kgfdsgfds //my sessionid value
*/
}
Well, I want to clarify something before answer the question -
What dose it means "i want to change PHPSESSID key and value"
As far I understand you want to change the "PHPSESSID" text. that means you want to call it in different name like "MySessionId" isn't it?
Actually I'm not authorized to write comment where I can ask!
Anyway, if you want to call it with different name than its not the issue of ZF its your server's PHP session settings. So, consult the session part of the server. You may get help from http://php.net/manual/en/session.configuration.php#ini.session.name OR http://php.net/manual/en/function.session-name.php
And ZF also let you change that name by Zend\Session\Config\SessionConfig class or Zend\Session\SessionManager class you have to set the preferred name by calling setName method.
Now if you want to change the value of session id (its automatically generated and save it into PHPSESSID) you may do it in ZF. Here is the code of ZF2 to set the value explicitly
$container->getManager()->setId($id); # For current session manager of your container
$container->getDefaultManager()->setId($id); # For default session manager of your entire app
In above code $id is the value you want to set.

ZF2 - get was unable to fetch or create an instance for getAlbumTable

I used the getAlbumTable in the title since the problem I'm facing is based on the Zend Album Tutorial, might be easier for others having a similar problem to find. All I've done is rename Album to "Champrallies".
My error:
Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getChampralliesTable
What I'm trying to do is execute a function from a second Model I've created. I can execute from the 'original' model no problem AlbumTable or in my case ChampionshipTable. I'm trying to get data from a second table, but within the same module. The second table is called "champ_rallies" and the Model files are Champrallies.php and ChampralliesTable.php.
Changing this part in my controller
'champRallies' => $this->getChampralliesTable()->fetchAll(),
to
'champRallies' => $this->getChampionshipTable()->fetchAll(),
means the error message disappears. So my first thought is that there is something wrong with namespaces, or module.php. (Forgive me, I'm fairly new at all this)
Module.php
<?php
namespace Championship;
use Championship\Model\Championship;
use Championship\Model\ChampionshipTable;
use Championship\Model\Champrallies;
use Championship\Model\ChampralliesTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(
'Championship\Model\ChampionshipTable' => function($sm) {
$tableGateway = $sm->get('ChampionshipTableGateway');
$table = new ChampionshipTable($tableGateway);
return $table;
},
'ChampionshipTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Championship());
return new TableGateway('championships', $dbAdapter, null, $resultSetPrototype);
},
'Championship\Model\ChampralliesTable' => function($sm) {
$tableGateway = $sm->get('ChampralliesTableGateway');
$table = new ChampralliesTable($tableGateway);
return $table;
},
'ChampralliesTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Champrallies());
return new TableGateway('champ_rallies', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
My second thought is that perhaps I'm not declaring the right namespace, or something similar.
ChampralliesTable.php
<?php
namespace Championship\Model;
use Zend\Db\TableGateway\TableGateway;
class ChampralliesTable
Champrallies.php
<?php
namespace Championship\Model;
class Champrallies
I fear I'm overlooking something failure but I haven't found any similar posts through google or on here, any help is appreciated!
edit: I had forgotten to add getChampralliesTable function to the controller itself,
public function getChampralliesTable()
{
This is Line 50 -> if (!$this->champralliesTable) {
$sm = $this->getServiceLocator();
$this->champralliesTable = $sm->get('Championship\Model\ChampralliesTable');
}
return $this->champralliesTable;
}
But now I'm getting this,
Notice: Undefined property: Championship\Controller\ChampionshipController::$champralliesTable in /usr/home/phil/git/rallystats/module/Championship/src/Championship/Controller/ChampionshipController.php on line 50
I had forgotten to add getChampralliesTable function to the controller itself,
public function getChampralliesTable()
{
This is Line 50 -> if (!$this->champralliesTable) {
$sm = $this->getServiceLocator();
$this->champralliesTable = $sm->get('Championship\Model\ChampralliesTable');
}
return $this->champralliesTable;
}
But now I'm getting this,
Notice: Undefined property: Championship\Controller\ChampionshipController::$champralliesTable in /usr/home/phil/git/rallystats/module/Championship/src/Championship/Controller/ChampionshipController.php on line 50
Solving this was just a matter of adding
protected $champralliesTable;
to the top of my ChampionshipController.
edit: Doing the above solves my problem.

ZF2 Fieldsets and Form Binding

I'm trying to create one page with a Form with two fieldsets that should each populate a different table.
I can easily create One form as in the Album tutorial, and bind the data like this:
$pageForm = new PageForm();
$pageForm->bind($page);
with my PageForm class as follows:
class PageForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('page');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
} /// and a bunch of other elements
but if I put these elements into fieldsets the bind no longer works, besides I would need to bind each fieldset to a separate table, and they need to save into the separate tables once the form is submited.
How would I go about this, I think I can do it using two forms but that is probably not the right way to go about it (If I understand the concept of fieldsets correctly)?
you have to use setObject in each Fieldset and provide a hydrator to it. eg:
<?php
// file My/Form/Product.php
namespace My\Form;
use Zend\Form\Fieldset;
use My\Entity\Product as ProductEntity;
use Zend\Stdlib\Hydrator\ClassMethods();
class Product extends Fieldset
{
public function __construct($name = 'product')
{
parent::__construct($name);
$this->setObject(new ProductEntity())
->setHydrator(new ClassMethods());
$this->add(array(
'name' => 'name',
'options' => array('label' => 'Product name'),
));
// Brand fieldset
$brand = new Brand();
$this->add($brand);
}
}
// File My/Form/Brand.php
namespace My\Form;
use Zend\Form\Fieldset;
use My\Entity\Brand as BrandEntity;
use Zend\Stdlib\Hydrator\ClassMethods();
class Brand extends Fieldset
{
public function __construct($name = 'brand')
{
parent::__construct($name = 'brand');
$this->setObject(new BrandEntity())
->setHydrator(new ClassMethods());
$this->add(array(
'name' => 'name',
'options' => array('label' => 'Brand name'),
));
}
}
// File My/Form/ProductForm.php
namespace My\Form;
use Zend\Form\Form;
use My\Entity\Product as ProductEntity;
use Zend\Stdlib\Hydrator\ClassMethods();
class ProductForm extends Form
{
public function __construct($name = 'product')
{
parent::__construct($name);
$this->setObject(new ProductEntity())
->setHydrator(new ClassMethods());
// Product Fieldset
// Here, we define Product fieldset as base fieldset
$product = new Product();
$product->setUseAsBaseFieldset(true);
$this->add($product);
}
}
// In Module.php
// ...
public function getServiceConfig()
{
return array(
'invokables' => array(
'My\Form\Product' => 'My\Form\Product',
),
);
}
// ...
// In Controller
// You don't need to use $form->bind($productEntity), except if you're editing a product.
// The form already has an Object, do you remenber??? "$this->setObject(new ProductEntity())" on form???
// ...
$form = $this->getServiceLocator()->get('My\Form\Product');
// ...

Zend FrameWork 2 Get ServiceLocator In Form and populate a drop down list

I need to get the adapter from the form, but still could not.
In my controller I can recover the adapter using the following:
// module/Users/src/Users/Controller/UsersController.php
public function getUsersTable ()
{
if (! $this->usersTable) {
$sm = $this->getServiceLocator();
$this->usersTable = $sm->get('Users\Model\UsersTable');
}
return $this->usersTable;
}
In my module I did so:
// module/Users/Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'Users\Model\UsersTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$uTable = new UsersTable($dbAdapter);
return $uTable;
},
//I need to get this to the list of groups
'Users\Model\GroupsTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$gTable = new GroupsTable($dbAdapter);
return $gTable;
},
),
);
}
Could someone give me an example how to get the adapter to the table from the group form?
I have followed this example to my form users:
http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html
EDITED from here...
Maybe I expressed myself wrong to ask the question.
What I really need to do is populate a select (Drop Down) with information from my table groups.
So I need to get the services inside my userForm class by ServiceLocatorAwareInterface (see this link) implemented because By default, the Zend Framework MVC registers an initializer That will inject into the ServiceManager instance ServiceLocatorAwareInterface Implementing any class.
After retrieving the values ​​from the table groups and populate the select.
The problem is that of all the ways that I've tried, the getServiceLocator() returns this:
Call to a member function get() on a non-object in
D:\WEBSERVER\htdocs\Zend2Control\module\Users\src\Users\Form\UsersForm.php
on line 46
I just wanted to do this in my UserForm...
namespace Users\Form;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Form\Element;
use Zend\Form\Form;
class UsersForm extends Form implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function getServiceLocator ()
{
return $this->serviceLocator;
}
public function setServiceLocator (ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
public function __construct ($name = null)
{
parent::__construct('users');
$this->setAttribute('method', 'post');
$sm = $this->getServiceLocator();
$groups = $sm->get('Users\Model\GroupsTable')->fetchAll(); // line 46
$select = new Element\Select('groups');
$options = array();
foreach ($groups as $group) {
$options[$group->id] = $group->name;
}
$select->setValueOptions($options);
$this->add($select);
// and more elements here...
The other various answers here generally correct, for ZF < 2.1.
Once 2.1 is out, the framework has a pretty nice solution. This more or less formalizes DrBeza's solution, ie: using an initializer, and then moving any form-bootstrapping into an init() method that is called after all dependencies have been initialized.
I've been playing with the development branch, it it works quite well.
This is the method I used to get around that issue.
firstly, you want to make your form implement ServiceLocatorInterface as you have done.
You will then still need to manually inject the service locator, and as the whole form is generated inside the contrstuctor you will need to inject via the contructor too (no ideal to build it all in the constructor though)
Module.php
/**
* Get the service Config
*
* #return array
*/
public function getServiceConfig()
{
return array(
'factories' => array(
/**
* Inject ServiceLocator into our Form
*/
'MyModule\Form\MyForm' => function($sm) {
$form = new \MyModule\Form\MyFormForm('formname', $sm);
//$form->setServiceLocator($sm);
// Alternativly you can inject the adapter/gateway directly
// just add a setter on your form object...
//$form->setAdapter($sm->get('Users\Model\GroupsTable'));
return $form;
},
),
);
}
Now inside your controller you get your form like this:
// Service locator now injected
$form = $this->getServiceLocator()->get('MyModule\Form\MyForm');
Now you will have access to the full service locator inside the form, to get hold of any other services etc such as:
$groups = $this->getServiceLocator()->get('Users\Model\GroupsTable')->fetchAll();
In module.php I create two services. See how I feed the adapter to the form.
public function getServiceConfig()
{
return array(
'factories' => array(
'db_adapter' => function($sm) {
$config = $sm->get('Configuration');
$dbAdapter = new \Zend\Db\Adapter\Adapter($config['db']);
return $dbAdapter;
},
'my_amazing_form' => function ($sm) {
return new \dir\Form\SomeForm($sm->get('db_adapter'));
},
),
);
}
In the form code I use that feed to whatever:
namespace ....\Form;
use Zend\Form\Factory as FormFactory;
use Zend\Form\Form;
class SomeForm extends Form
{
public function __construct($adapter, $name = null)
{
parent::__construct($name);
$factory = new FormFactory();
if (null === $name) {
$this->setName('whatever');
}
}
}
We handle this in the model, by adding a method that accepts a form
public function buildFormSelectOptions($form, $context = null)
{
/**
* Do this this for each form element that needs options added
*/
$model = $this->getServiceManager()->get('modelProject');
if (empty($context)){
$optionRecords = $model->findAll();
} else {
/**
* other logic for $optionRecords
*/
}
$options = array('value'=>'', 'label'=>'Choose a Project');
foreach ($optionRecords as $option) {
$options[] = array('value'=>$option->getId(), 'label'=>$option->getName());
}
$form->get('project')->setAttribute('options', $options);
}
As the form is passed by reference, we can do something like this in the controller where the form is built:
$builder = new AnnotationBuilder();
$form = $builder->createForm($myEntity);
$myModel->buildFormSelectOptions($form, $myEntity);
$form->add(array(
'name' => 'submitbutton',
'attributes' => array(
'type' => 'submit',
'value' => 'Submit',
'id' => 'submitbutton',
),
));
$form->add(array(
'name' => 'cancel',
'attributes' => array(
'type' => 'submit',
'value' => 'Cancel',
'id' => 'cancel',
),
));
Note: The example assumes the base form is build via annotations, but it doesn't matter how you create the initial form.
An alternative method to the other answers would be to create a ServiceManager Initializer.
An example of an existing Initializer is how the ServiceManager is injected if your instance implements ServiceLocatorAwareInterface.
The idea would be to create an interface that you check for in your Initialiser, this interface may look like:
interface FormServiceAwareInterface
{
public function init();
public function setServiceManager(ServiceManager $serviceManager);
}
An example of what your Initializer may look like:
class FormInitializer implements InitializerInterface
{
public function initialize($instance, ServiceLocatorInterface $serviceLocator)
{
if (!$instance instanceof FormServiceAwareInterface)
{
return;
}
$instance->setServiceManager($serviceLocator);
$instance->init();
}
}
Anything that happens in init() would have access to the ServiceManager. Of course you would need to add your initializer to your SM configuration.
It is not perfect but it works fine for my needs and can also be applied to any Fieldsets pulled from the ServiceManager.
This is the way I used get around that issue.
firstly, In Module.php, create the service (just as you have done):
// module/Users/Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'Users\Model\UsersTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$uTable = new UsersTable($dbAdapter);
return $uTable;
},
//I need to get this to the list of groups
'Users\Model\GroupsTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$gTable = new GroupsTable($dbAdapter);
return $gTable;
},
),
);
}
Then in the controller, I got a reference to the Service:
$users = $this->getServiceLocator()->get('Test\Model\TestGroupTable')->fetchAll();
$options = array();
foreach ($users as $user)
$options[$user->id] = $user->name;
//get the form element
$form->get('user_id')->setValueOptions($options);
And viola, that work.

Resources