link-in - רשומות קוד

חפש את הקוד שלך >

Full Module – כולל שדות באדמין ותצוגה לקלייט

בניית הטבלה במסד הנתונים

VENDOR/MODULE/Setup/InstallSchema.php

 סכמה לבניית מסד הנתונים , יש לתת שם לטבלה ולהכניס את השדות הרצוניות לבניה
יש לבנה s:sp והקובץ ירוץ פעם אחת ברישום של המודול , במידה וזה לא עבד יש למחוק גם מהטבלה שבה המודול רשום setup_module

<?php
/**
 * Anagal Lesson 5 Task
 * Copyright (C) 2019 
 * 
 * This file is part of AnagalLess5/PostsList.
 * 
 * AnagalLess5/PostsList is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

namespace AnagalLess5\PostsList\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\InstallSchemaInterface;

class InstallSchema implements InstallSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function install(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $table_anagalless5_postslist_post = $setup->getConnection()->newTable($setup->getTable('anagalless5_postslist_post'));

        $table_anagalless5_postslist_post->addColumn(
            'post_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true,'nullable' => false,'primary' => true,'unsigned' => true,],
            'Entity ID'
        );

        $table_anagalless5_postslist_post->addColumn(
            'status',
            \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
            null,
            [],
            'status'
        );

        $table_anagalless5_postslist_post->addColumn(
            'title',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'title'
        );

        $table_anagalless5_postslist_post->addColumn(
            'description',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            null,
            [],
            'description'
        );

        $table_anagalless5_postslist_post->addColumn(
            'updated_at',
            \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
            null,
            [],
            'updated_at'
        );

        $table_anagalless5_postslist_post->addColumn(
            'created_at',
            \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
            null,
            [],
            'created_at'
        );

        $setup->getConnection()->createTable($table_anagalless5_postslist_post);
    }
}

יצור המודול בצורה אוטומטית

משיכת נתונים והדפסה לקליינט

VANDOR/MODULE/Block/CLASS.php

<?php

namespace AnagalLess5\PostsList\Block;

use Magento\Framework\View\Element\Template;

class PostList extends \Magento\Framework\View\Element\Template{

    protected $_postFactory;

    public function __construct(
        Template\Context $context,
        \AnagalLess5\PostsList\Model\PostFactory $postFactory,
        array $data = &#91;&#93;
    )
    {
        $this->_postFactory = $postFactory;
        parent::__construct($context, $data);
    }

    //get all items from anagalless5_postslist_post table
    public function getAllItems()
    {
        $post = $this->_postFactory->create();
        return $post->getCollection();
    }

    //get only active items from anagalless5_postslist_post table
    public function viewActivePosts(){
        return $this->_postFactory->create()
            ->getCollection()
            ->addFieldToSelect('*')
            ->addFieldToFilter('status',
                ['in' => [1]]
            );
    }

}

 

protected $_postFactory;

public function __construct(
Template\Context $context,
\AnagalLess5\PostsList\Model\PostFactory $postFactory,
array $data = []
)
{
$this->_postFactory = $postFactory;
parent::__construct($context, $data);
}

חייבים להכניס בונה קלאס וירטואלי שדרכו ניתן למשוך את המידע

    public function viewActivePosts(){
        return $this->_postFactory->create()
            ->getCollection()
            ->addFieldToSelect('*')
            ->addFieldToFilter('status',
                ['in' => [1]]
            );
    }

הדרך שבה ניתן לבצע פילטרים על המידע

פרויקט לדוגמה