Image field using Definitions? - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Image field using Definitions?

  • Image field using Definitions?
  • Hello,
    I am trying to clone Products module (included in SDK) as per my requirements. I have read a lot but am still confused where/how data is being saved?

    It has price, quantity fields etc defined in ProductItem class inheriting Content class. Does it have to do with DynamicFields of Fluent API? I need to have images to upload as well. I feel ease working over Fluent API fascades.

    Q1: How can I have image field along with other string and/or numeric fields?
    Q2: How can I have a single field as a relational field (foreign key) to link with other items?
  • Hi saadi,

    It is not necessary to make this field  adynamic field. The better way to have a field of type Image is to add it to the definition of your class. Consider the following two pieces of code as examples for a reference to a single instance and to a set of instances correspondingly:

    [FieldAlias("image")]
    public virtual Image Image
        get
        
            return this.image;
        
        set
        
            this.image = value;
        
    private Image image;

    [FieldAlias("images")]
    public IList<Image> Images
        get
        
            if (this.images == null)
                this.images = new ProviderTrackedList<Image>(this, "Images");
            this.images.SetCollectionParent(this);
            return this.images;
        
      
    [Depend]
    private ProviderTrackedList<Image> images;

    Another way, to have a reference to an Image object, is to store the Id and the provider (optionally) of the image as fields in your class and to retrieve the object when is needed.

    In order to make use of these fields you will need field controls to modify them in the backend. Please see this thread from our forums http://www.sitefinity.com/devnet/forums/sitefinity-4-x/general-discussions/add-album-selector-as-custom-field.aspx

    Best wishes,
    Ivan Pelovski
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  • Thanks Ivan. Can you please find me a reference or tell me how fields (Ttile, Price, QuantityInStock) of Products module are maintained in database in actual?

    Does class inheriting Content implement DynamicType of Fluent API? If it does, then what type of inheritance it implements (flat, vertical or horizontal)?
  • Hi Saad,

    we use  OpenAccess ORM http://www.telerik.com/products/orm.aspx for the data layer in Sitefinity. All the fields are created by OpenAccess, however, some fields are defined during the compilation (Open Access uses technique called enhancing to add some code to the persistent classes during the compilation), while others are added dynamically (during the runtime).

    Every time you define a member inside of a class (prior to compilation), those are going to be created by OpenAccess on the first connection to the database and we generally call them static. On the other hand, if you use our DynamicData fluent API, you are adding fields after the compilation and hence we call them dynamic types / fields.

    Content base class uses horizontal inheritance, meaning that each subclass will have a separate database table in which ALL (as opposed to vertical) data will be stored.

    I hope this sheds some light on the inner workings of Sitefinity.

    Kind regards,
    Ivan
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  • Hi Saad,
    Are you able to get the image field to the module and successfully save to sitefinity DB?
    I have added image field to productItem class like:
            [DataMember]
            [FieldAlias("image")]
            public Image Image
           
                get
               
                    return this.image;
               


                set
               
                    this.image = value;
               
           
    Also, I modified the productsDefinition.cs to add a image field, each time I try to load products module, I got the following error:  
    -----------------------
    [MetadataException: No database type mapping found for field 'image': CLR type 'Telerik.Sitefinity.Libraries.Model.Image' or column type '' unmapped. ] OpenAccessRuntime.Relational.metadata.RelationalMappingResolver.resolveMapping(RelationalJavaTypeMapping fieldMapping, String fieldName, Class javaType, Boolean complain) +437 OpenAccessRuntime.Relational.RelationalMetaDataBuilder.createColumn(DataObjectsExtension[] nested, String fieldName, Class javaType, String oType, Boolean secondaryTable) +49 OpenAccessRuntime.Relational.RelationalMetaDataBuilder.createJdbcSimpleField(FieldMetaData fmd) +173 OpenAccessRuntime.Relational.RelationalMetaDataBuilder.createJdbcField(FieldMetaData fmd, Boolean quietParam) +69 [MetadataException: The metadata for field 'image' of class 'Products.Model.ProductItem' cannot be initialized: No database type mapping found for field 'image': CLR type 'Telerik.Sitefinity.Libraries.Model.Image' or column type '' unmapped. ] DynamicModule.ns.Wrapped_OpenAccessProvider_567d8f3ff3b6411d964969d0304d1917.GetSecurityRoot() +162 Telerik.Sitefinity.Data.ManagerBase`1.GetSecurityRoot() +30 Telerik.Sitefinity.Web.UI.ContentUI.Views.Backend.Master.MasterGridView.GetSecurityRoot() +33 Telerik.Sitefinity.Web.UI.ContentUI.Views.Backend.Master.MasterGridView.OnInit(EventArgs e) +370 System.Web.UI.Control.InitRecursive(Control namingContainer) +140 System.Web.UI.Control.AddedControl(Control control, Int32 index) +197 System.Web.UI.ControlCollection.Add(Control child) +79 Telerik.Sitefinity.Web.UI.ContentUI.ContentView.LoadView(String viewName) +301 Telerik.Sitefinity.Web.UI.ContentUI.ContentView.CreateChildControls() +57 System.Web.UI.Control.EnsureChildControls() +102 System.Web.UI.Control.PreRenderRecursiveInternal() +42 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
    ------------------------
    Any idea?
    Thanks,
    Qian

  • Hi Qian,

    I tried putting this field to my module. But unfortunately I didnt find any definition control to for type Image.
    I got this link for custom definition element to have in our modules.

    We will have to make our custom definition element which will upload files from beckend.

    Let me know if you need more help.
  • Hi Saad,
    Thanks for your response. Have you tried to use ImageFieldElement in the definition class? I was able to put it in the definition class and load image from either image library or local drive, but the problem is to save the loaded image, I am not exactly sure how the data been saved through OpenAccess. Even with the custom definition element built with the sample link, how do you save the data to sitefinity database, can yo show some code using the definition element in the definition class  (like productsDefinition.cs)?
    Another thing with the custom definition element is that we cannot show the image on the backend after it is selected, only the file path is displayed.
    thanks.
    Qian
  • Hello Qian,

    This is what I faced too in the next phase. I somehow managed to make a ImageFieldElement on my own that would show up file/image upload interface, but I didnt get any clue on post selected files along with other data and all that.

    Now, I am looking to use async image upload using Jquery. I will send you my work soon.

    Cheers
  • Hello Ivan,

                I'm Created 3 textboxes(name, email, phone number) in master page, when i'm entering values in these text boxes, how to save these values in Database with Sitefinity 
  • Hello Ivan,

                I'm Created 3 textboxes(name, email, phone number) in master page, when i'm entering values in these text boxes, how to save these values in Database with Sitefinity