Sometimes we have one form that uses multiples models, and we must validate this model and display the errors messages into the form, doing one normal validate, just one model per time is validate, to do both in the same time we have a simple way to do, that is using the CakePHP model method saveAll.
We must have to tell for the save all that he just has to validate. Let’s see the piece of code.
Using for examplem one model User that hasOne Profile.
1 | $this->User->saveAll($this->data, array('validate' => 'only')); |
With this we validate the data of model User also the data of model Profile, and in the form will show the error message of both models, this will work with more relations.
In django when you delete one object of model and this one have relation, the django apply by default the cascade action deleting all the relations of this current model that you want delete, sometimes you do not want this cascade, you just want to pass the relations model to none or null, to do it you can use the clear method:
For example having these models:
models.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Person(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=100) def __unicode__(self): return self.name class House(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=100) person = models.ForeignKey(Person, null=True) def __unicode__(self): return self.name |
You want to delete one Person, so when delete this Person django automatic delete the house, when you do just it:
1 2 | person = Person.objects.get(pk=1) person.delete() |
To keep the house and pass the relation to null you should do it:
1 2 3 | person = Person.objects.get(pk=1) person.house_set.clear() person.delete() |
So you get the person, set to load the house of this person and clear the relation, and so you are able to delete the person, you can check that the house will still in database but without the foreign key of Person.
A CONRAD CAINE, agência digital com escritórios em Munich, Pelotas e Buenos Aires procura programadores de interface para integrar a equipe de Pelotas-RS.
Responsabilidades
– Codificar páginas que funcionem perfeitamente em diversos browsers, de uma maneira fiel ao projeto original (funcionalmente e visualmente);
– Codificar interfaces interativas utilizando JavaScript/Ajax;
– Pesquisar novas técnicas de desenvolvimento;
– Aplicar com sucesso técnicas de SEO;
– Escrever código limpo e bem organizado;
Qualificações
– Experiência na utilização das bibliotecas JavaScript mais conhecidas no mercado;
– Conhecimento acerca das técnicas de SEO;
– Habilidade para desenvolver websites com um alto padrão de usabilidade / acessibilidade;
– Fluência nos padrões W3C (HTML/XHTML/CSS);
– Pró-ativo e autodidata;
– Rápido na solução de bugs;
– Habilidade para entregar estimativas corretas;
– Inglês intermediário/avançado;
– Experiência com Photoshop/Fireworks;
Currículos devem ser enviados para jobs.pelotas@conrad-caine.com, com o título “Programador de Interface”
I’m doing some development for the iphone one task was to check if the app is running for the first time if was so show some alert popup to do that I used the NSUserDefaults, with this class you can store some data.
Let’s see the example:
1 2 3 4 5 6 7 8 9 10 | NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; if (![prefs objectForKey:@"alert"]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Some title" message:@"Some description" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"alert"]; } [[NSUserDefaults standardUserDefaults] synchronize]; |
So this snippet of code, check if has one object set with the name alert if has do nothing else show an alert and store the alert with the value bool YES