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

Probs with module API Formfield

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 04-17-2017, 12:09 AM
  LoveHurts's Avatar 
LoveHurts LoveHurts is offline
 

Advanced Member
  
Join Date: Mar 2007
Posts: 39
 

Red face Probs with module API Formfield

Hi;

X-cart 5.3.2.7
try my first module... unable to get it working..

i try this :http://devs.x-cart.com/en/basics/adding_new_property_to_a_product/adding_product_property_via_formmodel_api.html

I made a simple module, activate it.
I made my folders, with code in it.

i want to store my purchaseprice in database of my products (example '4.99').


Problem:
http://i63.tinypic.com/2dqkar6.jpg

Every value i try tot put in gives a warning 'Test field must be unique'.
A string or number of characters are not stored in database when updating my productdetails.
What is going wrong? Thanks for helps

Main.php

Code:
<?php namespace XLite\Module\Yves\PurchasePrice; abstract class Main extends \XLite\Module\AModule { /** * Author name * * @return string */ public static function getAuthorName() { return 'Yves'; } /** * Module name * * @return string */ public static function getModuleName() { return 'PurchasePrice'; } /** * Get module major version * * @return string */ public static function getMajorVersion() { return '5.3'; } /** * Module version * * @return string */ public static function getMinorVersion() { return 0; } /** * Module description * * @return string */ public static function getDescription() { return 'Purchae Price Define'; } }
Decorate the \XLite\Model\Product class We are creating the <X-Cart>/classes/XLite/Module/XCExample/NewProductFieldDemo/Model/Product.php file with the following content:

Code:
<?php namespace XLite\Module\Yves\PurchasePrice\Model; use XLite\Core\Cache\ExecuteCachedTrait; use XLite\Core\Model\EntityVersion\EntityVersionInterface; use XLite\Core\Model\EntityVersion\EntityVersionTrait; use XLite\Model\Product\ProductStockAvailabilityPolicy; class Product extends \XLite\Model\Product implements \XLite\Base\IDecorator { /** * @Column (type="string", length=32) */ protected $testField; /** * Public getter * @return string */ public function getTestField() { return $this->testField; } /** * Public setter * @param string $value */ public function setTestField($value) { return $this->testField; } }
To be able to specify value of this property on product details page in admin area, we have to decorate \XLite\View\FormModel\Product\Info class and create <X-Cart>/classes/XLite/Module/XCExample/NewProductFieldDemo/View/FormModel/Product/Info.php file with the following content:

Code:
<?php namespace XLite\Module\Yves\PurchasePrice\View\FormModel\Product; class Info extends \XLite\View\FormModel\Product\Info implements \XLite\Base\IDecorator { /** * @return array */ protected function defineFields() { $schema = parent::defineFields(); $schema[self::SECTION_DEFAULT]['testField'] = [ 'label' => static::t('Test field'), 'position' => 100, ]; return $schema; } }
The following code will decorate the class \XLite\Model\DTO\Product\Info (which is responsible for transfering data from product editing page):
**i made a file : XLite\Module\Yves\PurchasePrice\Model\DTO\Product\ Info.php

Code:
<?php namespace XLite\Module\Yves\PurchasePrice\Model\DTO\Product; use Symfony\Component\Validator\Context\ExecutionContextInterface; use XLite\Core\Translation; use XLite\Model\DTO\Base\CommonCell; /** * Product */ class Info extends \XLite\Model\DTO\Product\Info implements \XLite\Base\IDecorator { /** * Method to initialize default data * * @param mixed|\XLite\Model\Product $object */ protected function init($object) { parent::init($object); // Here you have all sections data available through $this->default, $this->shipping, $this->marketing etc. $this->default->testField = $object->getTestField(); } /** * Method which transfers data from DTO to given object (product entity, in our case) * * @param \XLite\Model\Product $object * @param array|null $rawData * * @return mixed */ public function populateTo($object, $rawData = null) { parent::populateTo($object, $rawData); // The same here, $this->default, $this->shipping etc. vars contain the state of the field sections. $object->setTestField($this->default->testField); } /** * @param Info $dto * @param ExecutionContextInterface $context */ public static function validate($dto, ExecutionContextInterface $context) { parent::validate($dto, $context); if (!empty($dto->default->testField) && static::isTestFieldUnique($dto)) { static::addViolation($context, 'default.testField', Translation::lbl('Test field must be unique')); } } protected static function isTestFieldUnique($dto) { $testField = $dto->default->testField; $identity = $dto->default->identity; // Entity identifier, e.g. product ID $entity = \XLite\Core\Database::getRepo('XLite\Model\Product')->findOneByTestField($testField); return !$entity || (int) $entity->getProductId() === (int) $identity; } }
__________________
Core version: 5.3.4.4
PHP: 5.6
Web server: Apache
---stll running a---
x-cart PRO 4.1.6 [unix] updated to 4.1.9
Belgium
Reply With Quote
  #2  
Old 04-28-2017, 07:56 AM
  LoveHurts's Avatar 
LoveHurts LoveHurts is offline
 

Advanced Member
  
Join Date: Mar 2007
Posts: 39
 

Default Re: Probs with module API Formfield

Maybe someone is trying the same:

I had to change this code
Quote:
<?php

namespace XLite\Module\Yves\PurchasePrice\Model;

use XLite\Core\Cache\ExecuteCachedTrait;
use XLite\Core\Model\EntityVersion\EntityVersionInterf ace;
use XLite\Core\Model\EntityVersion\EntityVersionTrait;
use XLite\Model\Product\ProductStockAvailabilityPolicy ;

class Product extends \XLite\Model\Product implements \XLite\Base\IDecorator
{
/**
* @Column (type="string", length=32)
*/
protected $testField;

/**
* Public getter
* @return string
*/
public function getTestField() {
return $this->testField;
}

/**
* Public setter
* @param string $value
*/
public function setTestField($value) {
return $this->testField;
}
}
whit this
Quote:
<?php

namespace XLite\Module\XCExample\NewProductFieldDemo\Model;

class Product extends \XLite\Model\Product implements \XLite\Base\IDecorator
{
/**
* Product
*
* @var float
*
* @Column (type="string", length=32)
*/
protected $testField ;

/**
* Set testField
*
* @param float $testField
* @return Product
*/
public function setTestField($testField)
{
$this->testField = $testField;
return $this;
}

/**
* Get testField
*
* @return float
*/
public function getTestField()
{
return $this->testField;
}
}
__________________
Core version: 5.3.4.4
PHP: 5.6
Web server: Apache
---stll running a---
x-cart PRO 4.1.6 [unix] updated to 4.1.9
Belgium
Reply With Quote
  #3  
Old 05-03-2017, 03:17 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Probs with module API Formfield

I haven't had a chance to replicate your module on my local installation, but, as far as I remember, in DTO classes you should translate your object to raw values. Something like this:
PHP Code:
// .........

use XLite\Model\DTO\Base\CommonCell;

/**
 * Product
 */
class Info extends \XLite\Model\DTO\Product\Info implements \XLite\Base\IDecorator
{
    protected function 
init($object)
    {
        
parent::init($object);

        
$schema = [
            
'auto_reward_points' => $object->getAutoRewardPoints(),
            
'reward_points'      => $object->getRewardPoints(),
        ];
        
$this->prices_and_inventory->reward_points_box = new CommonCell($schema);
    }

    public function 
populateTo($object$rawData null)
    {
        
$box $this->prices_and_inventory->reward_points_box;
        
$object->setAutoRewardPoints((boolean) $box->auto_reward_points);
        
$object->setRewardPoints(intval($box->reward_points));

        
parent::populateTo($object$rawData);
    }

__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote

The following user thanks qualiteam for this useful post:
LoveHurts (05-06-2017)
  #4  
Old 05-06-2017, 05:31 AM
  LoveHurts's Avatar 
LoveHurts LoveHurts is offline
 

Advanced Member
  
Join Date: Mar 2007
Posts: 39
 

Default Re: Probs with module API Formfield

Thx for reply Alex. My module is working now, maybe i need/test your code later.
__________________
Core version: 5.3.4.4
PHP: 5.6
Web server: Apache
---stll running a---
x-cart PRO 4.1.6 [unix] updated to 4.1.9
Belgium
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



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 03:01 PM.

   

 
X-Cart forums © 2001-2020