E 'questo un approccio bene a che fare con la codifica?
-
09-10-2019 - |
Domanda
Può questo codice essere ottimizzato o ri-scomposto? E 'questo un approccio ottimale per tag?
Il seguente codice è una richiamata nel mio modello post. Si crea un record che associa un tag con un post in un QuestionsTags falegname tavolo. Quando necessario, se un determinato tag non esiste già nella tabella tag, la funzione crea, quindi utilizza il suo id per creare il nuovo record nella tabella QuestionsTags.
La difficoltà di questo approccio è la QuestionsTags tabella dipende dati nella tabella tag che può o non possono esistere.
La funzione assume i seguenti tabelle:
tags(id, tagName),
posts(tags) // Comma delimited list
questionsTags(postId, tagId)
L'idea è quella di loop su un elenco delimitato di tag presentati con un post e controllare per vedere se ciascuna variabile esiste già nella tabella tag
Se variabile esiste:
- Verificare se c'è già un record QuestionTag per questo post e questo tag nella tabella QuestionTags.
- Se sì, non fare nulla (l'associazione già esistente)
- Se no, creare un nuovo record QuestionTag utilizzando l'id del tag esistente e la postId
Se tag non esiste già:
- Crea il nuovo tag nella tabella tag
- Utilizza il suo ID per creare un nuovo record QuestionsTags
Codice
/**
* @hint Sets tags for a given question.
**/
private function setTags()
{
// Loop over the comma and space delmited list of tags
for (local.i = 1; local.i LTE ListLen(this.tags, ", "); local.i = (local.i + 1))
{
// Check if the tag already exists in the database
local.tag = model("tag").findOneByTagName(local.i);
// If the tag exists, look for an existing association between the tag and the question in the QuestionTag table
if (IsObject(local.tag))
{
local.questionTag = model("questionTag").findOneByPostIdAndTagId(values="#this.postId#,#local.tag.id#");
// If no assciatione exists, create a new QuestionTag record using the tagId and the postId
if (! IsObject(local.questionTag))
{
local.newQuestionTag = model("questionTag").new(postId = this.id, tagId = local.tag.id);
// Abort if the saving the new QuestionTag is unsuccesful
if (! local.newQuestionTag.save())
{
return false;
}
}
}
// If the tag does not exist create it
else
{
local.newTag = model("tag").new(tagName = local.i, userId = this.ownerUserId);
// Abort if the the new tag is not saved successfully
if (! local.newTag.save())
{
return false;
}
// Otherwise create a new association in the QuestionTags table using the id of the newly created tag and the postId
local.newQuestionTag = model("questionTag").new(postId = this.id, tagId = local.newTag.id);
// Abort if the new QuestionTag does not save correctly
if (! local.newQuestionTag.save())
{
return false;
}
}
}
}
FYI:. Sto usando CFWheels nella mia richiesta, il che spiega le funzioni ORM usati
Soluzione
Questo è esattamente come vorrei affrontarla. Curioso il motivo per cui si sta utilizzando "" come delimitatore? E se l ' "utente, non ha, permesso, uno spazio"? Mi basta usare virgola e trim () l'elemento di lista.