How to access the user object in the form configure method - php

I'm currently doing this kind fo thing with symfony forms
$this->myForm = new MyForm();
$this->myForm->customConfigureMethod($this->getUser()->getGuardUser());
because I need to configure a DoctriineChoice widget on the basis of the user.
I would rather do this kind of thing
$this->myForm =new myCustomConfiguredForm($this->getUser()->getGuardUser());
With the customisation being part of the form instantiation.
Anyone know how I could achieve this? I think I might be a bit unclear about the difference between the configure() and setup() functions for the forms so can't think clearly about it.

You shpuld pass the user object as an option. Here is an exapmle:
class ProductForm extends BaseProductForm
{
public function configure()
{
// or use an instance variable if you need the user in an another method too
$user = $this->getOption('user');
if (!$user instanceof sfBasicSecurityUser)
{
throw new InvalidArgumentException('A user object is required as "user" option in ' . __METHOD__);
}
// do something with the user...
}
}
$form = new ProductForm(array(), array('user' => $this->getUser()));

Related

How does DBAL read data that ORM inserts but has not yet “flush”?

For historical reasons, my pattern of running databases using Symfony is mixed. That is, the query uses DBAL and the insert uses ORM. Now you need to write a lot of data to the database. The flush in ORM can help me achieve business at the lowest cost.
All flush operations have been removed from the project. Put it in the __destruct of the controller.
However, doing so will cause DBAL to not find the latest changed data. Of course, these data ORMs can be obtained normally.
This is a very difficult problem. I hope to get guidance.
class BaseController extends Controller
{
public function __destruct()
{
$this->getDoctrine()->getManager()->flush();
}
public function indexAction()
{
$model = new CompanyModel();
$model->install(['company_name' => '1234']);
$model->update(['company_name' => 'abcd'], $model->lastInsertId);
}
}
class CompanyModel extends BaseController
{
public function validate($data, $id = false)
{
$this->entityManager = $this->getDoctrine()->getManager();
if(empty($id)){
$this->company_class = new Company();
}else{
if(!$this->is_exist($id)){
return false;
}
$this->company_class = $this->entityManager->getRepository(Company::class)->find($id);
}
if(array_key_exists('company_name', $data)){
$this->company_class->setCompanyName($data['company_name']);
}
if(self::$error->validate($this->company_class)){
return false;
}
return true;
}
public function insert($data)
{
if(!$this->validate($data)){
return false;
}
$this->company_class->setCreateAt(new \DateTime());
$this->entityManager->persist($this->company_class);
//$this->entityManager->flush();
$this->lastInsertId = $this->company_class->getId();
return true;
}
public function update($data, $id)
{
if(empty($id)){
self::$error->setError('param id is not null');
return false;
}
if(!$this->validate($data, $id)){
return false;
}
$this->company_class->setUpdateAt(new \DateTime());
//$this->entityManager->flush();
return true;
}
public function is_exist($id)
{
return $this->get('database_connection')->fetchColumn('...');
}
}
The final result of executing indexAction company_name is 1234; $ model-> update() was not executed successfully. The reason is that the $this-> is_exist() method that took the DBAL query did not find the ORM insert but did not flush the message.
Unchanging conditions,run
$this->entityManager->getRepository(Company::class)->find($id);
Is successful。
The problem is not the entity manager or dbal, as far as I can tell, but the usage of an anti-pattern, which I would call ... entanglement. What you should strive for is separation of concerns. Essentially: Your "CompanyModel" is an insufficient and bad wrapper for the EntityManager and/or EntityRepository.
No object should know about the entity manager. It should only be concerned with holding the data.
The entity manager should be concerned with persistence and ensuring integrity.
The controller is meant to orchestrate one "action", that can be adding one company, editing one company, batch-importing/updatig many companies.
Services can be implemented, when actions become to business-logic-heavy or when functionality is repeated.
(Note: the following code samples could be made way more elegant with using all the features that symfony provide, like ParamConverters, the Form component, the Validation component, I usually wouldn't write code this way, but I assume everything else would go way over your head - no offence.)
handling actions in the controller
controller actions (or service actions, really) are when you look at your problem from the task perspective. Like "I want to update that object with this data"). That's when you fetch/create that object, then give it the data.
use Doctrine\ORM\EntityManagerInterface;
class BaseController extends Controller {
public function __construct(EntityManagerInterface $em) {
$this->em = $em;
}
public function addAction() {
$company = new Company(['name' => '1234']); // initial setting in constructor
$this->em->persist($company);
// since you have the object, you can do any changes to it.
// just change the object
$company->update(['name' => 'abcd']); // <-- don't need id
// updates will be flushed as well!
$this->em->flush();
}
public function editAction($id, $newData) {
$company = $this->em->find(Company::class, $id);
if(!$company) {
throw $this->createNotFoundException();
}
$company->update($newData);
$this->em->flush();
}
// $companiesData should be an array of arrays, each containing
// a company with an id for update, or without an id for creation
public function batchAction(array $companiesData) {
foreach($companies as $companyData) {
if($companyData['id']) {
// has id -> update existing company
$company = $this->em->find(Company::class, $companyData['id']);
//// optional:
// if(!$company) { // id was given, but company does not exist
// continue; // skip
// // OR
// $company = new Company($companyData); // create
// // OR
// throw new \Exception('company not found: '.$companyData['id']);
// }
$company->update($companyData);
} else {
// no id -> create new company
$company = new Company($companyData);
$this->em->persist($company);
}
}
$this->em->flush(); // one flush.
}
}
the base controller should handle creating objects, and persisting it, so very basic business logic. some would argue, that some of those operations should be done in an adapted Repository for that class, or should be encapsulated in a Service. And they would be right, generally.
the entity handles it's internal state
Now, the Company class handles its own properties and tries to stay consistent. You just have to make some assumptions here. First of all: the object itself shouldn't care if it exists in the database or not. it's not its purpose! it should handle itself. Separation of concerns! The functions inside the Company entity should concern simple business logic, that concerns its INNER state. It doesn't need the database, and it should not have any reference to the database, it only cares about it's fields.
class Company {
/**
* all the database fields as public $fieldname;
*/
// ...
/**
* constructor for the inital state. You should never want
* an inconsistent state!
*/
public function __construct(array $data=[]) {
$this->validate($data); // set values
if(empty($this->createAt)) {
$this->createAt = new \DateTime();
}
}
/**
* update the data
*/
public function update(array $data) {
$this->validate($data); // set new values
$this->updateAt = new \DateTime();
}
public function validate(array $data) {
// this is simplified, but you can also validate
// here and throw exceptions and stuff
foreach($array as $key => $value) {
$this->$key = $value;
}
}
}
some notes
Now, there should be NO use case, where you get an object to persist and at the same time an update - with an id - that refers to the new object ... unless that object was given the id beforehand! HOWEVER. If you persist an object, that has an ID and you call $this->em->find(Company::class, $id) you would get that object back.
if you have many relations, there are always good ways to solve this problem without destroying separation of concerns! you should never inject an entity manager into an entity. the entity should not manage its own persistence! nor should it manage the persistence of linked objects. handling persistence is the purpose of the entity manager or entity repository. you should never need a wrapper around an object just to handle that object. be careful not to mix responsibilities of services, entities (objects) and controllers. In my example code, I have merged services and controllers, because in simple cases, it's good enough.

Populate entity from data array without form/request

Just wondering if it is possible to only use some parts of the symfony form handling. For exampe, when creating CRUD actions via generate:doctrine:crud I get something in my Controller (for handling create User POST requests) that looks like this:
$entity = new User();
$form = $this->createForm(new UserType(), $entity,
array(
'action' => $this->generateUrl('user_create'),
'method' => 'POST',
));
$form->handleRequest($request);
//Here I have a filled Entity
But what I want is to have this in a more reusable solution. Currently I have my business logic in a service called UserModel. Here I also want to have the create method to create, validate and persist a new entity. Tough the UserModel should also be callable from some Command scripts via the console, so I probably won't always have Request nor a Form.
So now from the above code I know that Symfony is already somehow populating data to an Entity according to the UserType definition, but how could I do that manually without having a Form or a Request and instead just some array of data?
So that I don't have to take care of setting the properties myself.
Edit:
The validation is no issue for populating the entity, I'm using the validator later on the populated entity before persisting the data.
And also important for me would be that the passed related entity ids will be handled/loaded automatically.
you may still use the Form component, but instead of using handleRequest, you should use directly submit.
If you are curious, you should look up the code on github for both handleRequest and what it actually does ; you'll see that it just do some verification, takes the data from the Request, and then uses the submit method of the Form.
So, basically, you can use only the submit method with the data you wish to use. It even validates your entity. :)
UPDATE
And for the concern of creating / updating related entities, if your relation have a persist / update cascade, it should roll out from itself without you doing a single thing, except persist + flush on your main entity.
If you do not worry about handling validation like things, you can do something like I have done.
You can define a trait or include the fromArray function in your entity classes.
trait EntityHydrationMethod
{
public function fromArray($data = array())
{
foreach ($data as $property => $value) {
$method = "set{$property}";
$this->$method($value);
}
}
}
If you defined trait, you can use it in your entities like:
class User{
use EntityHydrationMethod;
}
Then from your user model you can define your create function something like:
public function create($data = array())
{
$entity = new User();
$entity->fromArray($data);
return $entity;
}
-Updated-
As you updated your question. you may achieve this by defining a trait or include the createFromArray function in your EntityRepository classes.
trait RepositoryCreateMethod {
public function createFromArray($data)
{
$class = $this->getClassName();
$object = new $class();
$meta = $this->getClassMetadata();
foreach ($data as $property => $value) {
$v = $value;
if(!empty($value) && $meta->hasAssociation($property)) {
$map = $meta->getAssociationMapping($property);
$v = $this->_em->getRepository($map['targetEntity'])->find($value);
if(empty($v)){
throw new \Exception('Associate data not found');
}
}
$method = "set{$property}";
$object->$method($v);
}
return $object;
}
}
If you defined trait, you can use it in your Repository like:
class UserRepository{
use RepositoryCreateMethod;
}
Then you can use this something like call from controller:
$user = $this->getDoctrine()
->getRepository('YourBundle:User')
->createFromArray($data);

How to historize each version of my object with Doctrine ?

public function executeNew(sfWebRequest $request)
{
$this->form = new JobeetJobForm();
}
public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod('post'));
$this->form = new JobeetJobForm();
$this->processForm($request, $this->form);
$this->setTemplate('new');
}
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()));
if ($form->isValid())
{
$jobeet_job = $form->save();
$this->redirect('job/edit?id='.$jobeet_job['id']);
}
}
I generated module with doctrine generator. I would like make: if i edit current Job and click Save then instead of save this edit i would like create new object job with new ID and same data as current edited Job. How can i make it? I would like make this same as wikipedia.
EDIT:
i dont know how to open action edit, edit few fields and click Save and instead save this changes i would like create new object. What i must edit in processForm?
This is exactly the goal of the versionable behavior of Doctrine.
When you need do keep versions with all related objects. You can serialize the object and keep it in database. Look at the JMSerializerBundle at github.

Cannot get DataMapper to work in CodeIgniter

I'm trying to implement an ORM in a CodeIgniter application, but cannot get it to work. To start I'm just trying to instantiate a simple test model:
<?php
class Cart extends DataMapper
{
public function __construct()
{
// model constructor
parent::__construct();
}
var $validation = array(
'username' => array(
'label' => 'UserName',
'rules' => array('required', 'trim', 'unique', 'alpha_dash', 'min_length' => 1, 'max_length' => 50),
)
);
}
?>
And then in the Controller I try this:
public function __construct()
{
parent::__construct();
$this->load->model('cart');
}
public function index()
{
$cart = new Cart();
}
But I don't even get past the constructor. The debugger stops and gives me a message saying "Waiting for an incoming connection with ide key xxxxx" (random number)
BTW the cart model class file name is in lower case, but the class name in upper case. I tried both in the constructor.
I have followed the instructions for installation carefully, copying the two datamapper files to libraries and config folders, as well as autoloading the datamapper library.
But it just doesn't work. Am I missing something? The table I'm trying to map is only a test table that actually only has an id and a username field. I don't actually understand the validation array, but just followed the examples in the docs and modified to my field. The id field doesn't seem like anyone has put in the validation array.
I should also mention that I'm a newbie at CodeIgniter.
Your code seems mostly correct for use with DataMapper ORM and CodeIgniter.
To explain things a bit, DataMapper is just an abstraction layer. It handles a lot of the necessities when working with databases and mapping your objects to your tables. That being said, you don't have to load your models, etc. As long as you are autoloading your database library and datamapper library, you can use DataMapper.
The validation array lets DataMapper know the requirements to your properties. So, if you try to save an object and one of the properties that you've created/changed doesn't meet those requirements, then your save will fail and you'll get an error message:
// For example
if ($myObj->save())
{
// $myObj validation passed and is saved to db
}
else
{
// $myObj validation failed, save did not complete
echo $myObj->error->string;
}
Codeigniter already has a library named Cart, so you wouldn't want to name your model Cart. So you could rename that model to Basket or something else that makes sense.
I know you're still just trying to get things to work, but I feel you need to think about your data structure a bit. You wouldn't save the username in the Cart object, that's why we use relations. So, I would structure it a bit like this:
// baskets table (a table represents many baskets, therefore it is plural)
id
user_id
blah
blah
created
updated
// users table
id
username
email_address
created
updated
// basket model (a model represents 1 basket, therefore it is singular)
class Basket extends DataMapper
{
public function __construct()
{
parent::__construct();
}
var $has_one = array('user'); // each basket belongs to one user
var $validation = array(...);
}
// user model
class User extends DataMapper
{
public function __construct()
{
parent::__construct();
}
var $has_many = array('basket'); // each user can have many baskets
var $validation = array(...);
}
// controller
public function __construct()
{
parent::__construct();
}
public function index()
{
$basket = new Basket();
$basket->blah = 'whatever';
$basket->save();
// at this point, $basket is saved to the database
// now let's add it to the user
$user = new User();
$user->where('id', 1)->get(1);
// now we have a user
// save the relationship to the basket
$user->save($basket);
// now $basket->user_id == 1
// get the username from the basket
$u = $basket->user->get();
$username = $u->username;
// yes, there are faster and shorter ways to write most of this,
// but I think for beginners, this syntax is easier to understand
}
The CodeIgniter documentation about models states that you can load a model by calling
$this->load->model('Model_name');
in the constructor, and that you can access this model in your controller by doing
$this->Model_name->function();
So you should change your Controller code into
public function __construct()
{
parent::__construct();
$this->load->model('Cart');
}
public function index()
{
$this->Cart->functionCall();
}

how to Use zend_auth as a plugin

I'm working on my first user login in Zend Framework, but I'm a little confused with Zend_Auth. All the articles I read about it use it directly in the controller. But to me, it makes more sense, to work as a plugin
What do you guys think?
You can use it as a plugin, the only downside is that if you initialize the plugin in your bootstrap, then the plugin will be executed for every controller and action, since it would have to run before your controller.
You could extend Zend_Auth and add extra methods to set up the auth adapter and manage the storage, and then you can just call Your_Custom_Auth::getInstance() to get the auth instance and then you can check for auth in the preDispatcth() part of your controllers that need auth.
This way you can easily work with zend_auth in multiple places with less code
<?php
class My_User_Authenticator extends Zend_Auth
{
protected function __construct()
{}
protected function __clone()
{}
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
// example using zend_db_adapter_dbtable and mysql
public static function getAdapter($username, $password)
{
$db = Zend_Controller_Front::getInstance()
->getParam('bootstrap')
->getResource('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($db,
'accounts',
'username',
'password');
$authAdapter->setIdentity($username)
->setCredential($password)
->setCredentialTreatment(
'SHA1(?)'
);
return $authAdapter;
}
public static function updateStorage($storageObject)
{
self::$_instance->getStorage()->write($storageObject);
}
}
// in your controllers that should be fully protected, or specific actions
// you could put this in your controller's preDispatch() method
if (My_User_Authenticator::getInstance()->hasIdentity() == false) {
// forward to login action
}
// to log someone in
$auth = My_User_Authenticator::getInstance();
$result = $auth->authenticate(
My_User_Authenticator::getAdapter(
$form->getValue('username'),
$form->getValue('password'))
);
if ($result->isValid()) {
$storage = new My_Session_Object();
$storage->username = $form->getValue('username');
// this object should hold the info about the logged in user, e.g. account details
My_User_Authenticator::getInstance()->updateStorage($storage); // session now has identity of $storage
// forward to page
} else {
// invalid user or pass
}
Hope that helps.
"Plugin" in ZF doesn't only mean "front controller plugin", also Action helpers, view helpers...
ZF guru Matthew Weier O'Phinney wrote an excellent article about creating action helpers, and guess what ?..
He illustrates it with an Auth widget !
http://weierophinney.net/matthew/archives/246-Using-Action-Helpers-To-Implement-Re-Usable-Widgets.html
don't forget to read the articles comments, as a lot of interesting Q&A are handled there

Categories