Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Can't save image

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 07-28-2019, 01:13 AM
 
Ed B. Ed B. is offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

Default Can't save image

I am writing a module with new model with image, and page for editing the entities.
So, the first step was combine https://devs.x-cart.com/basics/model_editing_page.html (ModelEditingDemo) and https://devs.x-cart.com/basics/understanding_models.html (RepoDemo). As I don't want to have two modules, I put the only php file from RepoDemo inside the ModelEditingDemo tree. I change all namespaces, "product" to
"testentity", etc. So far everything works.


The next step : add a property with image. There is an example https://devs.x-cart.com/basics/using_images_widget.html but there are some differences.


We don't have to decorate TestEntity class, it suffices to add the image and getter/setter function in Model/TestEntity.php. So I put these lines
Code:
/** * @OneToMany (targetEntity="XLite\Module\XCExample\ModelEditingDemo\Model\I mage\TestEntity\SecondaryImage", mappedBy="testentity", cascade={"all"}) * @OrderBy ({"orderby" = "ASC"}) */ protected $secondaryImages;
and
Code:
public function __construct(array $data = array()) { parent::__construct($data); $this->secondaryImages = new \Doctrine\Common\Collections\ArrayCollectio n(); } public function getSecondaryImages() { return $this->secondaryImages; } public function addSecondaryImages($image) { $this->secondaryImages[] = $image; return $this; }


I also create the class for images, Model/Image/TestEntity/SecondaryImage.php
(well, the images aren't secondary, but the more changes I make, the more places I could make mistakes, so...) just as in the image widget example, with a modification

(aside from the namespace)
Code:
/** * @ManyToOne (targetEntity="\XLite\Module\XCExample\ModelEditingDemo\Model \TestEntity", inversedBy="secondary_images") * @JoinColumn (name="testentity_id", referencedColumnName="id") */ protected $testentity;
(Note that TestEntity entities have id's, not testentity_id's).


So far things look fine, in the database, we get a table testentity_secondary_images.


Next, I make Model/Repo/Image/TestEntity/SecondaryImage.php
with
Code:
public function getStorageName() { return 'testentity'; }
Here things don't go well, as I don't see the directory xcart/image/testentity_getting created. But let's go on. What we haven't done yet (as far as the admin page is concerned) in xcart kb image widget example are DTO decoration and FormModel modification. Now, the other property of TestEntity (text) doesn't use DTO, and we don't have appropriate DTO class to modify, so presumably we don't need DTO class. Or am I wrong here?


In any case, as to FormModel, again, as we are using View/Model/TestEntity.php to output detail form, it is this file to modify, and I

put
Code:
'image' => array( self::SCHEMA_CLASS => 'XLite\View\FormField\FileUploader\Image', self::SCHEMA_LABEL => 'icon', self::SCHEMA_REQUIRED => false, ),
.


With these modifications (and some other obvious ones) I have entity editing form with image uploader, but the images are never saved. What am I doing wrong?
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #2  
Old 07-31-2019, 05:38 AM
 
Ed B. Ed B. is offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

Default Re: Can't save image

OK, I think I have solved this.



As I am imitating the model "Category" I should be putting only one image for my entity (and this is enough for my purpose).


So here is how it goes : in my module, aside from Main.php, I have 7 php files and one twig file. The entity is called "Author", module is "Librairie" and vendor name is EdB.


The files are:



Controller file Controller/Admin/AuthorEdit.php
Page view file View/Page/Admin/AuthorEdit.php which calls the twig file
the twig file page/myentity_edit/body.twig which calls the input forms, which is
View/Model/Author.php
View/Form/Model/Author.php


and the definition of entities

Model/Author.php
Model/Image/Author/Image.php
and the path to the image storage
Model/Repo/Image/Author/Image.php


The relevant part of the codes are


the definition of image property and getter and setter in Model/Author.php
Code:
/** * One-to-one relation with category_images table * * @var \XLite\Module\EdB\Librairie\Model\Image\Author\Image * * @OneToOne (targetEntity="XLite\Module\EdB\Librairie\Model\Image\Author\I mage", mappedBy="author", cascade={"all"}) */ protected $image; ... ... /** * Get image * * @return \XLite\Module\EdB\Librairie\Model\Image\Author\Image */ public function getImage() { return $this->image; } /** * Set image * * @param \XLite\Module\EdB\Librairie\Model\Image\Author\Image $image Image OPTIONAL * * @return void */ public function setImage(\XLite\Module\EdB\Librairie\Model\Image\Author\Imag e $image = null) { $this->image = $image; } /** * Check if category has image * * @return boolean */ public function hasImage() { return null !== $this->getImage(); }
(OK, I was too lazy to change some commented out text, but this shouldn't cause the trouble). The definition of the image entity in Model/Image/Author/Image.php


Code:
namespace XLite\Module\EdB\Librairie\Model\Image\Author; /** * Author * * @Entity * @Table (name="author_images") */ class Image extends \XLite\Model\Base\Image { /** * Relation to a author entity * * @var \XLite\Module\EdB\Librairie\Model\Author * * @OneToOne (targetEntity="XLite\Module\EdB\Librairie\Model\Author", inve rsedBy="image") * @JoinColumn (name="author_id", referencedColumnName="author_id", onDelete ="CASCADE") */ protected $author; ... ... /** * Set author * * @param \XLite\Module\EdB\Librairie\Model\Author $author * @return Image */ public function setAuthor(\XLite\Module\EdB\Librairie\Model\Author $author = null) { $this->author = $author; return $this; } /** * Get author * * @return \XLite\Module\EdB\Librairie\Model\Author */ public function getAuthor() { return $this->author; }
(Omitted lines are about alternative texts)


and the image uploader definition in

View/Model/Author.php

Code:
'image' => array( self::SCHEMA_CLASS => 'XLite\View\FormField\FileUploader\Image', self::SCHEMA_LABEL => 'Image', self::SCHEMA_REQUIRED => false, ),
as well as the path setup in Model/Repo/Image/Author/Image.php

Code:
namespace XLite\Module\EdB\Librairie\Model\Repo\Image\Author; class Image extends \XLite\Model\Repo\Base\Image { /** * Returns the name of the directory within 'root/images' where images store d * * @return string */ public function getStorageName() { return 'author'; } }


I hope this helps people having similar issues.
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote

The following user thanks Ed B. for this useful post:
memoto (03-12-2020)
  #3  
Old 03-06-2020, 09:51 AM
 
Ed B. Ed B. is offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

Default Re: Can't save image

I realized that I missed the file Model/Repo/Author.php , but things do work without it anyway. Anyhow, for anyone who might care, attached is the complete module.
Attached Files
File Type: tgz EdB-LibrairieC.5.4.0.0.tgz (2.8 KB, 287 views)
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote

The following user thanks Ed B. for this useful post:
memoto (03-12-2020)
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 05:50 AM.

   

 
X-Cart forums © 2001-2020