cfwheels - updating nested properties with a composite key table - coldfusion

I'm trying to get a better setup working for updating a model with nested properties.
Right now in my edit view I define the textFieldTag manually to create the params struct by setting up the name as "myModel[myNestedProperty][#modelID#,#key2id#][name]"
in update action...
if I just use myModel.update(params.myModel) I can't get the update to work if there are any elements that require deletion
so I destroy all the models of the nested property that have the same id as myModel, in which case it works.
The downside is that if the update fails, the nested properties are all gone.
I've tried grabbing all of the models first before deleting them and .saveing them, but for some reason that's not working.
Looks like cfwheel is setup for this kind of relation with the checkboxes, but I need it to work with textfield and select items in my form.
Update
I realized I have another issue. Essentially I would like to expand on this to be able to use it across multiple nested properties and relationships.
the issue is in the way I setup the name especially for select dropdowns:
name="myModel[myNestedProperty][#modelID#, ][nestedID]"
the issue is that the second id cannot be declared, because it will be assigned as the id rather than using the value that I select.
To be honest, this is the one issue I've been battling with my whole time. I'm dealing with it by regenerating the models in the controller, I just forgot I haven't solved that issue yet.
Is there a way I can have these values not be used at all, and have them populated from the structure dynamically?
let's say I have (truncated) a name tcat[34,0][catID] or tcat[34,][catID], (where the catID should be the 2nd ID).
the params' tcat structure that gets generated is
[34,0]{catID = 12,14,18}
or
[34,]{catID = 12,14,18}
I'd like the params' tcat structure to have multiple structs like:
[34,12]{tID = 34; catID = 12}
[34,14]{tID = 34; catID = 14}
[34,18]{tID = 34; catID = 18}
Is there a way around this?
I'm working with a lot of composite key nested properties, and if I could have this part alone working it would make it a lot easier. I know one way is to update the name with javascript, but I think that would be the (very,very) last resort.

Can you give this a try?
Set up a callback in the parent model that checks to see if name is blank and flags for deletion if it's blank.
<cffunction name="init">
...
<!--- This could also be `beforeValidation` if you want to make `name` required for the child model --->
<cfset beforeSave("$provisionMyNestedProperty")>
</cffunction>
<cffunction name="$provisionMyNestedProperty">
<cfscript>
if (StructKeyExists(this, "myNestedProperty") && IsArray(this.myNestedProperty))
{
for (local.i = 1; local.i <= ArrayLen(this.myNestedProperty); local.i++)
{
if (!StructKeyExists(this.myNestedProperty[local.i], "name") || !Len(Trim(this.myNestedProperty[local.i].name)))
this.myNestedProperty[local.i]._delete = true;
}
}
</cfscript>
</cffunction>
I'll keep editing my answer until we can bang out a solution. Hopefully that gives you a good start though.

Related

Updating and creating a new instance at the same time

When a user updates an invoice form, i want to create a new invoice record with the updated attributes, but also change one or two fields of the old record and save it, too.
How would the outline of a controller action look like which could accomplish this?
Instead of a controller action i put the code in the model, using callbacks:
before_save do |rec|
if !rec.new_record?
attrb = rec.attributes.delete_if{|k, v| ["id"].include? k }
Book.create(attrb)
rec.restore_attributes
rec.year = rec.year + 2 # some custom change
true
end
end
I keep all attributes unless the 'id' (otherwise i get an error) for create a new record with the new attributes.
Then i restore the attributes of the existing record. I do some custom change before saving.
I am rather new with Rails but this seems pretty straightforward. As you mention the user is 'updating" an invoice, your controller view has probably been passed all the data available to the user for further change.
When submitting the form, your update action can easily update the current record data, as well as creating a new one on top of this
Though as it is automated, you need to make clear:
if a new invoice record is created each time an invoice record is
updated (thi can create a lot of copies of the same invoice)
how you make the old record an archive to avoid duplicates
can the 'additional" amendments be automated and easily processed through an algorithm...
Nested attributes made things a bit tricky. So in order to create new instances I had to use the dup method for both the resource and its nested items.
Generally, it is advisable to keep the controllers slim and make the models fat. Nevertheless, I have decided to include this code into my Invoices controller:
def revise_save
#contact = Contact.find(params[:contact_id])
#invoice = #contact.invoices.find(params[:invoice_id])
#invoice_old = #invoice.dup
#invoice.invoice_items.each do |item|
#invoice_old.invoice_items << item.dup
end
#invoice.datum = DateTime.now.to_date
# archive old invoice
# #invoice_old. ...
#invoice_old.save
# make old new invoice
#invoice.datum = Time.now
# ...
#invoice.update(invoice_params)
redirect_to invoices_path
end
Note that in this solution the currently edited (original) invoice becomes the new invoice, the old one is paradoxically created anew.
Thanks to #iwan-b for pointing me in the right direction.

Sitecore return field "Fields" with zero count in SocialProfiles

Sitecore return field "Fields" with zero count in SocialProfiles, but this fields in social network really exist. How I can get need fields?
var socialProfileManager = new SocialProfileManager();
var twitterNetwork = allProfiles.FirstOrDefault(x => x.NetworkName == "Twitter");
if (twitterNetwork.Fields.Count != 0) //Dicitionary "Fields" is empty here
{
...
}
I had a similar situation where I was trying to retrieve the fields and dealing with the zero field count. Take a look at this post: https://stackoverflow.com/a/30519345/4897782
Specifically, my issue was resolved when I passed false as the second parameter in the base login method. The default true parameter attempts to update the profile asynchronously, making it unavailable during my attempts to do what you are doing.
To be able to override the parameter though, I had to deviate from the standard out of the box login controls and implement my own version of what happens when you click login. It's pretty simple though. Take a look at the post and hopefully that resolves your issue.

symfony 1.4 and propel - foreign constraints fails saved relation

I am working in a Symfony 1.4 project with Propel 1.4.2.
I have 2 related tables. workshop and trainers which is a many to many relation mapped by a join table (workshop_trainers) which contains the workshop_id and the trainer_id).
In my Workshop Form I have a select box for adding the trainers to the workshop. The problem is when the workshop is new (Create) I get an error:
Cannot add or update a child row: a foreign key constraint fails
This happens because, when saving the workshop_trainers relation the workshop_id field is null. IsnĀ“t Propel intelligent enough to know that there is a relation between the tables and save the base object first? What I am doing wrong?
My trainer list widget.
$this->widgetSchema['workshop_trainer_list'] = new sfWidgetFormChoice(array(
'choices' => $trainers,
'multiple' => true,
));
Thanks for your help.
This is not fixing the problem but that's the easiest practical solution to this problem:
In the form, simply deactivate the workshop_trainer_list field if the object is a new one (doesn't have an ID yet).
Something like:
if ($this->getObject()->isNew())
{
$this->offsetUnset('workshop_trainer_list'); // not sure of that method name
}
A better solution is to update the doSave method to have the ID first, something like this:
protected function doSave($con = null)
{
$isNew = $this->getObject()->isNew();
if (null === $con)
{
$con = $this->getConnection();
}
// retrieve the value of workshop_trainer_list here and remove it from the form
$trainers = ...
$this->offsetUnset('workshop_trainer_list');
// save without it
parent::doSave($con);
// add it back
$this->getObject()->set...
// save
$this->getObject()->save($con);
}

Django ModelForm Validate custom Autocomplete for M2M, instead of ugly Multi-Select

Given the following models (cut down for understanding):
class Venue(models.Model):
name = models.CharField(unique=True)
class Band(models.Model):
name = models.CharField(unique=True)
class Event(models.Model):
name = models.CharField(max_length=50, unique=True)
bands = models.ManyToManyField(Band)
venue = models.ForeignKey(Venue)
start = models.DateField()
end = models.DateField()
The admin area works great for what I'm doing, but I'd like to open the site up a bit so that certain users can add new Events. For the public portions, I have several "administrative" fields on these models that I don't want the public to see (which is easy enough to fix).
My specific problem, though, is changing the display of the ManyToMany selections when creating a new Event. Because the number of Bands possible to list for an event should not be sent along as a multiselect box, I'd like to use an AutoComplete that handles multiples (like the Tags box, here on StackOverflow!).
I have this part working, and it correctly fills in a hidden input with the Band.id's separated by commas for a value. However, I can't understand how to put together letting Django do the validation using the ModelForms, and somehow also validating the 'Bands' selection.
Ideally, I want to auto-complete like the tags here on StackOverflow, and send along the selected Bands ID's in some kind of Delimited string - all while letting Django validate that the bands passed exist, etc, as if I left the annoying multi-select list in place.
Do I have to create my own Auto-Complete Field type for a form or model, and use that? Is there something else I'm overlooking?
I have seen some existing AutoComplete widgets, but I'd really-really-really like to use my own Autocomplete code, since it's already set up, and some of them look a bit convoluted.
There was a lot more text/explanation here, but I cut back because I'm avoiding Wall Of Text. If I left important stuff out, let me know.
It's a little hard to say without knowing exactly what your autocomplete code is doing, but as long as it is sending the ids of the bands like they would be sent with the <select>, the ModelForm should validate them as usual.
Basically, your POST string should look like:
name=FooBar2009&bands=1&bands=3&bands=4&venue=7&start=...
The easiest way to do this might be to use Javascript to add (and remove) a hidden input field for each band entered with the name band and the id of the band as the value. Then, when the user submits the form, the browser will take care of posting the right stuff, and the ModelForm will validate it.
Using the annointed jquery autocomplete plugin,
On the client-side I have something like this:
jQuery("#id_tags").autocomplete('/tagging_utils/autocomplete/tasks/task/', {
max: 10,
highlight: false,
multiple: true,
multipleSeparator: " ",
scroll: true,
scrollHeight: 300,
matchContains: true,
autoFill: true,
});
So, I have a view that returns when I type in a:
http://skyl.org/tagging_utils/autocomplete/tasks/task/?q=a&limit=10&timestamp=1259652876009
You can see the view that serves that here:
http://github.com/skyl/skyl.org/blob/master/apps/tagging_utils/views.py
Now, it's going to be a little tricky .. you might except the POST, then in the clean method of the field try to .get() based on the strings and raise a form validation error if you can't get it ... right, name = ... unique=True .. so something like (off the top of my head) ... :
def clean_bands(self):
return Band.objects.filter( name__in = self.cleaned_data['bands'].split(' ') )
You could also check each string and raise a form error if there are no bands by that name .. not sure that the clean method should return a qs. Let me know if this helps and you want me to keep going/clarify.

Search value in body tag in Sitecore

I want to implement search functionality, So my requirement is I want to search some keyword in body tag in all content page. I do not know how I can search keyword in body tag in Sitecore. Please guide me?
As Anton outlined, the concept of searching the Body tag is wrong for Sitecore. You want to think in terms of Content in fields of Items. Sitecore's ContentSearch is how you can achieve this.
Sitecore comes with default indexes out-of-the-box that you should use for the search. You should rebuild these via the Index Manager in the Content Editor and then base your search on the basic example I've outlined for you below.
public IEnumerable<Item> Search(string searchterm)
{
string indexName = "sitecore_web_index";
using (var index = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
{
var predicate = PredicateBuilder.True<SearchResultItem>();
IQueryable<SearchResultItem> query = index.GetQueryable<SearchResultItem>().Where(i => i.Content.Contains(searchterm)).Filter(predicate);
var searchResults = query.GetResults();
foreach (var hit in searchResults.Hits)
{
yield return hit.Document.GetItem();
}
}
}
jRobbins's answer is sensible (he get's my upvote). However, it is technically possible to index the content of the body tag. I would be cautious with this. I've seen it working well, but I've also seen it completely destroy the performance of a site.
The approach involves the creating a computed field in your index. You populate the computed field by making a web request to your newly published page and scraping the response body tag.
Here's are a couple of module that more or less does that:
https://github.com/efocus-nl/sitecorewebsearch
https://github.com/hermanussen/sitecore-html-crawler
If you can accept something a little less accurate, then you could loop through each of the components on your page and extract content from their datasources. That approach is discussed in this video:
http://www.techphoria414.com/Blog/2012/May/Sitecore_Page_Editor_Unleashed