I have BlocksController and Block model. In cakephp-1.2 I used edit action to edit my application's blocks and it worked fine.
After migration to 1.3 and 2.x I noticed that the edit action leads to saving an edited block as a new record.
In the cakePHP docs I read that in 1.3 the form's helper does not supply the id any more and so the model regarded the process as add.
To solve this issue, I tried to add a hidden field named id with a value of the id of the block is being edited as follows:
<?php echo $this->Form->create('Block', array('class' => 'nice custom'));?>
//The following line is required in cakephp 1.3+
<?php echo $this->Form->hidden('id', array('value' => $block['Block']['id']));?>
The described solution is working fine. However I need to know another way to do that without changing in the view. Does it possible?
TLDR
There are any number of ways to accomplish what you want. The most ideal solution would be to add the id into the view, similar to how you mentioned. But - there are as many options as you can think of.
In View
The easiest (and I would argue the ideal) solution would just be to add the id in your view. You should be able to just add it like this:
echo $this->Form->input('id');
It makes it hidden automatically since it's "id" and it should populate for you as well if you're passing the data correctly to the view.
In Controller
BUT - since your question was how to do it NOT in the view, another option is to do it in the Controller. I don't know your exact project, so change it as you see fit, but before your save, you can always just append the Block's id to the data.
$this->request->data['Block']['id'] = $YOUR_ID;
$this->Block->save($this->request->data);
In Model
This seems like a strange place, but - you if you're storing the ID in session or something, you COULD theoretically add the id here before saving as well.
Related
Ok, lets say there are two associated models, where the main model has a hasMany relationship with the other model. ex Donor hasMany Donations. I have read most of cake's documentation and I also took note of this (from the doc):
'When working with associated models, it is important to realize that
saving model data should always be done by the corresponding CakePHP
model. If you are saving a new Post and its associated Comments, then
you would use both Post and Comment models during the save operation'(http://book.cakephp.org/2.0/en/models/saving-your-data.html)
And as also pointed out by #burzum on another one of my questions : CakePHP - Controller or No Controller? , I am still struggling on how to decide which works best.
So taking the 'Donor and Donation' example mentioned earlier, let's say we want the user to add donation to a donor. So for the user to add a donation, one would need to view a donor, accessing a url of the sort : 'domain/donors/view/1', meaning the user is currently viewing donor with id of 1. Then within that view, let us assume there is button which allows the user to add a donation. Now from what I have been told and from what I read, the add(donation) action should belong to the donationsController.
Let us also assume that the add(donation) action has its own view.. So if I am at 'domain/donors/view/1' the user is redirected to 'domain/donations/add/donor_id:1' where the id of the donor is then retrieved using
$this->request->params['named']['post_id']
and set to the FK like so :
`$this->request->data['Donation']['donor_id'] = $id;
But I have a gut feeling telling me that this is not the proper way to go and it feels like bad practice jumping from one controller to the other, when having associated models.
Any insight on this ? Is there a general rule of thumb one could apply or follow ? Is it ok to redirect the user from the donorsController's view action to the donationsController's add action ?
Thanks in advance!
As far as I know your approach is not wrong, and linking other controllers from a view is normal.
However i prefer (and that's my rule of thumb, i'm not sure is a "best practice") to give the user the change of add an associated model - donations in your case - directly from the view of the "main" model. I've used this kind of approach in my applications, mostly considering the user experience (don't know if it is your case). So, if you are in the edit action of Donor, in the respective view the user can add the Donation and send it to still the edit action of Donor. Here you can save the associated model if the request data are well formatted:
data['Donor'] = array('id'=> XX);
data['Donation'] = array('id' => YY, 'amount' => blabla,...);
$this->Donor->save(data);
That would respect the MVC (no ugly ClassRegistry::Load ... ) and keep the code simple, avoiding to jump from one controller to another.
Just my opinion :D
Regards.
I'm developing a component for Joomla! 3.x and I came across a strange issue. I followed the official documentation (http://docs.joomla.org/J3.2:Developing_a_MVC_Component/Adding_backend_actions) and I was able to get somewhere. Now the problem is that I wanted to expand the tutorial and create submenus within the components menu in the backend. I succeeded with this too.
The 2 submenu selections link correctly to 2 different views and I am able to fetch data from different tables nicely. The problem is that I cant add new entry to the database using my second view. The first view works fine. On the second view, when I click the green Add button, I get a jquery error: Uncaught TypeError: Cannot read property 'task' of null
The problem is that the addNew method cant find municipality.add or something. However this (almost) same code works for the default view.
What I'm trying to do is to display the data of 2 different tables in the DB and then being able to edit delete or add new.
Any ideas? Thanks in advance
The code
municipality.add
and
municipalitys.delete
refers to two different controllers, named municipality.php and municipalitys.php
You need to ensure the methods are present, in your case municipality.php should contain a
public function add()
which is not there. For a reference on how to implement it look at the other controller (most likely you'll invoke the add() method of the relevant model).
Or possibly you're extending the municipality controller from a different ancestor that doesn't implement the add method
Answering my own question, the problem was in the views/municipalitys/tmpl/default.php.
The form contained in that file was wrong, missing the id="adminForm" and the proper action= value.
I have a situation where I need to create a shortcut for a specific filter in a Joomla component.
The problem is that I cannot unset it, as I do not know if Joomla sets the form fields to its own session handler, request handler or some kind of custom handler. There also does not appear to be any documentation on this specific case.
The full situation is that I have a link that will auto filter in the same view as another link (in the components sidebar). One view will be just a specific filter and the other is standard. So I need it when you click into the filtered view it will reset the current filters to make sure everything displays as it should, and vice-versa so clicking back will again reset the filters.
I have tried a number of approaches for this, and although I can consistently force it to filter but it will not reset the form when I re-enter the last page with any technique I have tried so far and of course I want to avoid bypassing Joomla's default functions.
if(JRequest::getVar('filter_group_id',false)==10){
JRequest::setVar('last_filter',true);
EthicstoolHelper::addSubmenu('supervisors');
}else{
if(JRequest::getVar('last_filter',false)===true){
JRequest::setVar('last_filter',false);
JRequest::setVar('filter_group_id',false)
}
EthicstoolHelper::addSubmenu('users');
}
This is the most recent think I have tried, as you can see I try to reset the value to false in a hope that Joomla will read it as not being set, as JRequest has no built in unset method.
I don't have enough rep to comment yet, so I'm guessing a bit as to what the problem could be. Assuming that you are using a model to set the state of the filters, you can look at overriding the populateState method.
Another options is to fiddle with the context property in the model. For example, you could change the context if you have your special filters enabled if you are using things like $app->getUserStateFromRequest(). If you can post a bit more information about the design of your component (controllers and models), I can help more.
I am working on a symfony 1.4 project and am trying to add a custom field in a backend form. At the moment, my module has a batch history with editing and deleting options for each row and a form that allows the user to filter through it. I would like to add a custom creation form and I am struggling to do that.
Going on the /*model_name*/new/ page does show me a form: this is the form I would like to make changes to.
I would like to display a dropdown list of elements from another table (and model) on this page. How can I do that knowing that the current module's widgetSchema doesn't have that relation?
Should I edit generator.yml? When I try adding a title: to new: , it doesn't seem to affect the /new/ page (even after cache:clear).
It would be easier if you create that relation in your schema.yml. Your generated form will be able to render the widget you want.
Otherwise, what you need to do is edit the *model_name*Form class by adding the widget and validator you want (sfWidgetFormDoctrineChoice and sfValidatorDoctrineChoice in your case)
The generator.yml uses the form *model_name*Form class by default, but I suggest you copy this class in a lib folder you create in your module, and edit this one instead. Then, you edit your generator.yml to use this new form, this is how you do it
Also, make sure there is not much rows in the table of your relation. Else, symfony will try to generate a html select tag with a lot of options and you will need to kill your php and even webserver processes.
Yes, you need to edit generator.yml, but in the form line:
# apps/backend/modules/whatever/config/generator.yml
config:
...
form:
display:
Whatever...
Here you have some examples. Taken from the official documentation:
http://symfony.com/legacy/doc/jobeet/1_4/en/12?orm=Doctrine#chapter_12_form_views_configuration
This way, the form that the generator uses will be the one you specify. And maybe you don't need to modify something else.
Hi I'm trying to use the CakePHP comments plugin found here http://cakedc.com/downloads/view/cakephp_comments_plugin but the instructions are really hard to follow. I've managed to add comments but it's displaying the commentWidget that's not working.
I'm getting confused at this part i think
To work properly, the component needs
a specific variable to be set in every
action using it. Its name should be
either
Inflector::variable(Controller::$modelClass)
or Comments::$viewVariable should be
set to other name of this view
variable. That variable should contain
single model record. for example you
need to have next line in you view
So far I've created the comments table, added it to the pluging and components arrays and added the following code to the controller:
public function beforeFilter() {
parent::beforeFilter();
$this->passedArgs['comment_view_type'] = 'flat';
}
I added the route
Router::connectNamed(array('comment', 'comment_view', 'comment_action));
And also the Comments.CommentWidget as a helper in my controller.
I'm just wondering if anyone has used this plugin before and can help me out?
thanks,
Jonesy
You're right - the documentation is really confusingly worded. However, if I understand correctly, what it wants is a copy of the record of the piece of data the comment will be attached to passed to the view the comments will render on.
So say you're making an event page, and you want people to comment on the event. You need to send to the view a variable called "event" with a copy of the base data for that event.
From their example they show: $this->set('post', $this->Post->read(null, $id));
For your event, you'd do something like $this->set('event', $this->Event->read(null, $id_of_event));
The Comment view probably needs this data for hidden fields so it can populate it with the model name and the event id.