1

I have the next JSON example and I need to save this with a model via POST.

The problem is how to save the field lines of this JSON...

{
       "sizeCurves": 
        {
            "external_key": "ssdfdsffg",
            "name": "name",
            "group_size": 4,
            "country": 3,
            "distribution_type": "7",
            "chart_color": 33,
            "color_code": 2,
            "lines": [
                    {
                        "size_code": 3,
                        "value": 10,
                        "whs_name": "name"
                    },
                    {
                        "size_code": 5,
                        "value": 6,
                        "whs_name": "name"
                    },
                    {
                        "size_code": 7,
                        "value": 9,
                        "whs_name": "name"
                    }
            ]
        }
}

The method save:

  /**
     * {@inheritdoc}
     */
    public function save(\Vendor\SizeCurves\Api\Data\SizeCurvesInterface $sizeCurves)
    {
        if (
            ($object = $this->get($sizeCurves->getExternalKey()))
        ) {
            $sizeCurves = $this->updateData($object, $sizeCurves);
        }

        try {
            $this->sizeCurvesResource->save($sizeCurves);

        } catch (ValidatorException $e) {
            throw new CouldNotSaveException(__($e->getMessage()));
        } catch (\Exception $e) {
            throw new CouldNotSaveException(__('Unable to save size curve %1', $e->getMessage()));
        }

        return $sizeCurves;
    }

Model SizeCurves:

class SizeCurves extends \Magento\Framework\Model\AbstractModel implements \Vendor\SizeCurves\Api\Data\SizeCurvesInterface
{

    protected $linesCollection;

    protected $sizeCurvesLinesRepository;

    protected $lines = [];

    protected $sizeCurvesLines;

    /**
     * SizeCurves constructor.
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param ResourceModel\SizeCurvesLines\Collection $linesCollection
     */
    public function __construct
    (
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Vendor\SizeCurves\Model\ResourceModel\SizeCurvesLines\Collection $linesCollection,
        \Vendor\SizeCurves\Model\SizeCurvesLinesRepository $sizeCurvesLinesRepository,
        \Vendor\SizeCurves\Model\SizeCurvesLines $sizeCurvesLines
    )
    {
        parent::__construct(
            $context,
            $registry
        );

        $this->sizeCurvesLinesRepository = $sizeCurvesLinesRepository;
        $this->linesCollection = $linesCollection;
        $this->sizeCurvesLines = $sizeCurvesLines;
    }

    public function _construct()
    {
        $this->_init('Vendor\SizeCurves\Model\ResourceModel\SizeCurves');
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->getData(self::ENTITY_ID);
    }

    /**
     * @param int $id
     * @return mixed
     */
    public function setId($id)
    {
        $this->setData(self::ENTITY_ID);
        return $this;
    }

    /**
     * @return string
     */
    public function getExternalKey()
    {
        return $this->getData(self::EXTERNAL_KEY);
    }

    /**
     * @param string $externalKey
     * @return mixed
     */
    public function setExternalKey($externalKey)
    {
        $this->setData(self::EXTERNAL_KEY, $externalKey);
        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->getData(self::NAME);
    }

    /**
     * @param string $name
     * @return mixed
     */
    public function setName($name)
    {
        $this->setData(self::NAME, $name);
        return $this;
    }

    /**
     * @return int
     */
    public function getGroupSize()
    {
        return $this->getData(self::GROUP_SIZE);
    }

    /**
     * @param int $groupSize
     * @return mixed
     */
    public function setGroupSize($groupSize)
    {
        $this->setData(self::GROUP_SIZE, $groupSize);
        return $this;
    }

    /**
     * @return int
     */
    public function getCountry()
    {
        return $this->getData(self::COUNTRY);
    }

    /**
     * @param int $country
     * @return mixed
     */
    public function setCountry($country)
    {
        $this->setData(self::COUNTRY, $country);
        return $this;
    }

    /**
     * @return string
     */
    public function getDistributionType()
    {
        return $this->getData(self::DISTRIBUTION_TYPE);
    }

    /**
     * @param string $distributionType
     * @return mixed
     */
    public function setDistributionType($distributionType)
    {
        $this->setData(self::DISTRIBUTION_TYPE, $distributionType);
        return $this;
    }

    /**
     * @return int
     */
    public function getChartColor()
    {
        return $this->getData(self::CHART_COLOR);
    }

    /**
     * @param int $chartColor
     * @return mixed
     */
    public function setChartColor($chartColor)
    {
        $this->setData(self::CHART_COLOR, $chartColor);
        return $this;
    }

    /**
     * @return int
     */
    public function getColorCode()
    {
        return $this->getData(self::COLOR_CODE);
    }

    /**
     * @param int $colorCode
     * @return mixed
     */
    public function setColorCode($colorCode)
    {
        $this->setData(self::COLOR_CODE, $colorCode);
        return $this;
    }

    /**
     * @param array|null $lines
     * @return $this|mixed
     */
    public function setLines(array $lines = null)
    {
        $this->lines = $lines;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getLines()
    {
        return $this->lines;
    }

Model SizeCurvesLines:

class SizeCurvesLines extends \Magento\Framework\Model\AbstractModel implements \Vendor\SizeCurves\Api\Data\SizeCurvesLinesInterface
{

    public function _construct()
    {
        $this->_init('Vendor\SizeCurves\Model\ResourceModel\SizeCurvesLines');
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->getData(self::ENTITY_ID);
    }

    public function setId($id)
    {
        $this->setData(self::ENTITY_ID, $id);
        return $this;
    }

    /**
     * @return int
     */
    public function getSizeCode()
    {
        return $this->getData(self::SIZE_CODE);
    }

    /**
     * @param int $sizeCode
     * @return mixed
     */
    public function setSizeCode($sizeCode)
    {
        $this->setData(self::SIZE_CODE, $sizeCode);
        return $this;
    }

    /**
     * @return int
     */
    public function getValue()
    {
        return $this->getData(self::VALUE);
    }

    /**
     * @param int $value
     * @return mixed
     */
    public function setValue($value)
    {
        $this->setData(self::VALUE, $value);
        return $this;
    }

    /**
     * @return string
     */
    public function getWhsName()
    {
        return $this->getData(self::WHS_NAME);
    }

    /**
     * @param string $whsName
     * @return mixed
     */
    public function setWhsName($whsName)
    {
        $this->setData(self::WHS_NAME, $whsName);
        return $this;
    }

    /**
     * @return mixed
     */
    public function getParent()
    {
        return $this->getData(self::PARENT);
    }

    /**
     * @param int $parent
     * @return mixed
     */
    public function setParent($parent)
    {
        $this->setData(self::PARENT, $parent);
        return $this;
    }
}

Schema:

public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        $table = $installer->getConnection()->newTable(
            $installer->getTable('vendor_size_curves')
        )->addColumn(
            'entity_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Entity ID'
        )->addColumn(
            'external_key',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            50,
            ['unsigned' => true, 'nullable' => false, 'default' => null],
            'External SAP Code'
        )->addColumn(
            'name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['unsigned' => true, 'nullable' => false, 'default' => null],
            'Description'
        )->addColumn(
            'group_size',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            [],
            'Intern Code sgi_size'
        )->addColumn(
            'country',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            [],
            'Country'
        )->addColumn(
            'distribution_type',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            1,
            [],
            'Type of distribution'
        )->addColumn(
            'chart_color',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            [],
            'Intern Code sgi_color'
        )->addColumn(
            'color_code',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            [],
            'Intern Code Option sgi_color'
        )->addIndex(
                $installer->getIdxName(
                    'vendor_size_curves',
                    ['external_key'],
                    \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                'external_key',
                ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
        )->setComment('Size Curves Main Table');

        $installer->getConnection()->createTable($table);

        $table = $installer->getConnection()->newTable(
            $installer->getTable('vendor_size_curves_lines')
        )->addColumn(
            'entity_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Entity ID'
        )->addColumn(
            'parent',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            50,
            ['unsigned' => true, 'nullable' => false, 'default' => '0'],
            'Size Curves Parent'
        )->addColumn(
            'size_code',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['unsigned' => true, 'nullable' => false, 'default' => '0'],
            'Size Curves Code'
        )->addColumn(
            'value',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            ['unsigned' => false, 'nullable' => false, 'default' => '0'],
            [],
            'Value'
        )->addColumn(
            'whs_name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'Name of Warehouse'
        )->addForeignKey(
            $installer->getFkName(
                'vendor_size_curves_lines',
                'parent',
                'vendor_size_curves',
                'external_key'
                ),
            'parent',
            $installer->getTable('vendor_size_curves'),
            'external_key',
            \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
        )->setComment(
            'Lines of Size Curves'
        );

        $installer->getConnection()->createTable($table);

        $installer->endSetup();
    }
  • You need to create another model same as SizeCurves – Ramkishan Suthar Dec 19 '18 at 9:49
  • @RamkishanSuthar The same? or another for the lines? in this case i have created a SizeCurvesLines, i update the question – chdzma Dec 19 '18 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.