I need to override my form like this:
protected function doSave($con = null)
{
$filename= Helper::generateFilename($this->getValue('name')).'jpg';
imagejpeg(Helper::createImage($this->getValues()), sfConfig::get('sf_upload_dir').'/poster/'.$filename );
/* Here I need to set value of *fielname* field of the form equal to
$filename */
parent::doSave($con);
}
Helper.class.php is located in /lib/so it acts like a helper class.
generateFilename actually gives a randomized string. since there is no function like setValue in a form, I'm stuck.
I tried macgyver's approach but didn't work for me. Not sure if I did anything wrong.
Syfmony version is 1.4 with Doctrine.
I had to override the updateObject() function inside the form class.
In my case its inside TimesheetForm.class.php. I had to change the status of the timesheet based on user action.
Following is the piece of code that that I tried. It did update the object before saving. Which was seen effected in the db.
public function updateObject($values = null)
{
$object = parent::updateObject($values);
$object->setStatus("Submitted");
return $object;
}
I didn't impelement/override the doSave() function. I'm using symfony 1.4.
Hope the above helps.
You have to use, say you have used filename as field in your schema to store the image filename, $this->object->setFilename($filename); before parent::doSave($con);
Related
I am stuck with it and i couldn't find any appropriate solution for this. what i want to achieve is in my admin panel based upon check box value i want to change active status of specific control and update database value using ajax. I want to make a common ajax function in controller to avoid repeatedly writing same ajax function for other controls like menu manager content manager, documents manager etc.So i want to send Model name to the ajax controller so that same function can be used. Ajax call is working perfectly but couldn't make appropriate models. for example:
$m = new App\Model.$request->model OR $m = 'App\Model\'.$request->model (adding last \ gives an error) or answer provided in Dynamically use model in laravel is not helping either. Is there any better ways if yes please suggest. I can do this below but it is hardcoded so i want to make model dynamic models
if($request->model ==='Menu')
$model = new \App\Http\Models\Menu;
else if($request->model === 'News')
$this->model = new \App\Http\Models\News;
else if($request->model === 'Document')
$this->model = new \App\Http\Models\Document;
Thankyou !!!
You can just use:
$modelName = '\\App\\Http\\Models\\'.$request->model;
$this->model = new $modelName;
However you should add validation to make sure only some allowed models would be used or at least do something like this:
if (! in_array($request->model, ['Menu', 'News', 'Document']))
{
throw new \Exception('Invalid model');
}
$modelName = '\\App\\Http\\Models\\'.$request->model;
$this->model = new $modelName;
This is because you don't want to expose probably all the models data for security reasons.
Try the below code
if($request->model){
$m = '\App'. '\' .$request->model;
//other code
}
I would like to access a xml field in a controller. After submitting a form containing the xml-field (name="parameter", type="text"), I tried to access the value of the field inside the controller with the following approach:
class NameControllerconfirmation extends NameController
{
public function test(){
$input = JFactory::getApplication()->input;
$parametername=$input->get('parameter');
$this-setRedirect(JRoute::_('index.php?
option=com_name&view=confirmation&name='.parametername,false));
}
}
The problem is, that $parameter is always 'null'. I've studied the joomla.docs as well as existing components intensely, but meanwhile I'm getting more and more confused.
But his Approach works great, when defining the field inside html, but I can't access the field, when it is declared as xml. Any help is appreciated.
You might be confusing URL parameters (which are set appending ?paramname=value to the url) with component parameters (those you set in the administrator). The latter can be retrieved like this:
$componentParams = &JComponentHelper::getParams('com_example');
$param = $componentParams->get('paramName', 'defaultValue');
You can find a near-complete guide to extension parameters here:
http://www.themepartner.com/blog/25/retrieving-plugin-module-component-and-template-parameters/
I'm using phpactiverecord. I have an Order object that has an id and code. On database, the id is the primary key int and auto increment. code is a varchar and unique and is composed by the id and other chars.
The problem is that I need to set the code value before to save and for this, I need to get the id.
Currently I did:
class Order extends ActiveRecord\Model{
function save() {
if (parent::save()): // I save all data
$this->code = "{$this->id}w"; // Once it was saved I get the id
parent::save(); // I save the code
return true;
endif;
return false;
}
}
Is there a more elegant way to do it?
You're technically not supposed to override save() in that way, as even in this case you're changing the method signature, (it's public function save($validate=true)). And there are a bunch of possible callbacks for your case. The better way for your case is:
public static $after_create = array('after_create');
public function after_create()
{
$this->code = $this->id.'w';
$this->save();
}
Also it's soo awkward to use templating if else inside of class code :P.
This code may possibly fail if you don't have the latest version from github, as there was a bug earlier on where after_create didn't know that the object was already saved.
I am using Symfony with propel to generate a form called BaseMeetingMeetingsForm.
In MeetingMeetingsForm.class.php I have the following configure method:
public function configure() {
$this->useFields(array('name', 'group_id', 'location', 'start', 'length'));
$this->widgetSchema['invited'] = new myWidgetFormTokenAutocompleter(array("url"=>"/user/json"));
}
In MeetingMeetings.php my save method is simply:
public function save(PropelPDO $con = null) {
$this->setOwnerId(Meeting::getUserId());
return parent::save($con);
}
However propel doesn't know about my custom field and as such doesn't do anything with it. Where and how to I put in a special section that can deal with this form field, please be aware it is not just a simple save to database, I need to deal with the input specially before it is input.
Thanks for your time and advice,
You have to define a validator (and/or create your own). The validator clean() method returns the value that needs to be persisted.
In Doctrine (I don't know Propel) the form then calls the doUpdateObject() on the form, which in turns calls the fromArray($arr) function on the model.
So if it's already a property on your model you'll only need to create the validator. If it's a more complex widget, you'll need to add some logic to the form.
I would like to validate an embedded form field before it gets saved in the database. Currently it will save an empty value into the database if the form field is empty. I'm allowing empty fields, but I want nothing inserted if the form field is empty.
Also quick question, how to alter field values before validating/saving an embedded form field?
$this->form->getObject works in the action, but $this->embeddedForm->getObject says object not found
I found a really easy solution. The solution is to override your Form's model class save() method manually.
class SomeUserClass extends myUser {
public function save(Doctrine_Connection $conn = null)
{
$this->setFirstName(trim($this->getFirstName()));
if($this->getFirstName())
{
return parent::save();
}else
{
return null;
}
}
}
In this example, I'm checking if the firstname field is blank. If its not, then save the form. If its empty, then we don't call save on the form.
I haven't been able to get the validators to work properly, and this is an equally clean solution.
Symfony gives you a bunch of hooks into the form/object saving process.
You can overwrite the blank values with null pre/post validation using by overriding the the doSave() or processValues() functions of the form.
You can read more about it here: http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms#chapter_06_saving_object_forms