i am trying to save a document (image in article). i use article uid as document filename. what i want to save in document is:
-caption (string text consists of the description of the image).
-fileName (string text consists of the article uid)
-fileHref (path to image location).
here is my code:
Document Handler:
function save (event, rc, prc) localmode="modern" {
prc.article = articleService.findWhere({"uid"=rc.fileName});
try {
if (prc.article.hasDocument()) {
response = {
"status" = "error",
"message" = "Article already have an image.",
"data" = {}
};
} else {
structUpdate(rc,"fileName", #rc.fileName#&".jpg");
prc.document = documentService.new(rc);
documentService.save(prc.document);
prc.article.addDocument(prc.document);
articleService.save(prc.article);
response = {
"status" = "success",
"message" = "",
"data" = prc.document// Include newly uploaded image info. in struct
};
}
} catch(e) {
response = {
"status" = "error",
"message" = e.message,
"data" = {}
};
log.fatal("Failed to create image file.", e.message);
}
event.renderData(type="json", data=response);
}
Document bean:
component entityname="Document" persistent="true" extends="BaseEntity" {
// primary key
// secondary key
property name="uid" ormtype="string" unique="true";
// non-relational columns
property name="fileName" ormtype="string" unique="true";
property name="caption" ormtype="string";
// one-to-one
// one-to-many
// many-to-one
property name="article" fieldtype="many-to-one" cfc="Article" fkcolumn="articleId" fetch="join";
// many-to-many
// calculated properties
property name="fileHref" ormtype="string" persistent="false";
// non-persistent
// object constraints
this.constraints = {
"caption" = { required=true, requiredMessage="Please enter Document caption." },
"fileName" = { required=true, requiredMessage="Please enter Document file." }
};
// methods
function init() localmode="modern" {
if(isNull(variables.uid)){
variables.uid = lCase(right(createUUID(), 16));
}
return this;
}
function onDIComplete() localmode="modern" {
variables.fileHref = "/docs/article/" & variables.fileName;
}
}
as you can see i already have set the fileName in my bean. but system can access that fileName property. sorry i am not fluent in english.
i called the wrong bean in the handler so this is happen.