{"id":1994,"date":"2019-06-18T14:21:39","date_gmt":"2019-06-18T12:21:39","guid":{"rendered":"https:\/\/www.h-hennes.fr\/blog\/?p=1994"},"modified":"2019-06-18T14:21:46","modified_gmt":"2019-06-18T12:21:46","slug":"prestashop-generer-des-modeles-via-la-console","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2019\/06\/18\/prestashop-generer-des-modeles-via-la-console\/","title":{"rendered":"Prestashop: G\u00e9n\u00e9rer des mod\u00e8les via la console"},"content":{"rendered":"\n<p>Cet article est la suite de <a href=\"https:\/\/www.h-hennes.fr\/blog\/2019\/04\/16\/prestashop-generer-des-modules-via-la-console\/\">Prestashop : G\u00e9n\u00e9rer des modules via la console. <\/a><\/p>\n<p>Toujours dans la m\u00eame logique d&rsquo;optimisation de cr\u00e9ation des modules via la ligne de commande \ud83d\ude42<br \/><br \/>Je rappelle \u00e9galement\u00a0 que cette fonctionnalit\u00e9 n\u2019est pas disponible via la console native de Prestashop ( disponible \u00e0 partir de prestashop 1.7 ) mais sur mon outil de console externe : prestashopConsole ( cf. <a href=\"https:\/\/github.com\/nenes25\/prestashop_console\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/nenes25\/prestashop_console<\/a> )<br \/><br \/><\/p>\n<p>Cette fois-ci l&rsquo;id\u00e9e est de pouvoir g\u00e9n\u00e9rer rapidement et facilement un mod\u00e8le associ\u00e9 \u00e0 votre module qui h\u00e9ritera de la classe ObjectModel de Prestashop, le tout sans \u00e9crire une seule ligne de code \ud83d\ude42<\/p>\n<p>Une nouvelle commande fait donc son apparition :<\/p>\n<p><strong>module:generate:model <\/strong><\/p>\n<p>Les param\u00e8tres obligatoires sont :<\/p>\n<ul>\n<li>Nom du module<\/li>\n<li>Classe du Mod\u00e8le.<\/li>\n<\/ul>\n<p>Voici par exemple comment cr\u00e9er un objet <strong>Sample<\/strong> pour le module <strong>hhdev<\/strong><\/p>\n\n\n\n<pre lang=\"bash\">\n.\/prestashopConsole.phar module:generate:model hhdev Sample\n<\/pre>\n\n\n\n<p>Une saisie interactive va ensuite vous demander les informations n\u00e9cessaires \u00e0 la cr\u00e9ation de votre mod\u00e8le.<br \/><br \/>Pour l&rsquo;exemple nous souhaitons que cet objet ait les propri\u00e9t\u00e9s suivantes :<\/p>\n<ul>\n<li>table : sample ( Nom de la table sql sans le prefix )<\/li>\n<li>primary : id_sample ( Cl\u00e9 primaire de l&rsquo;objet )<\/li>\n<li>Champs :\n<ul>\n<li>name ( Nom du champ )\n<ul>\n<li>type : string<\/li>\n<li>obligatoire : oui<\/li>\n<li>validation : isName<\/li>\n<li>longueur : 100 ( Longueur maximum du champ )<\/li>\n<li>langue : Non<\/li>\n<\/ul>\n<\/li>\n<li>firstname\n<ul>\n<li>type: string<\/li>\n<li>obligatoire : non<\/li>\n<li>validation : isName<\/li>\n<li>longueur : 120<\/li>\n<li>langue: non<\/li>\n<\/ul>\n<\/li>\n<li>email\n<ul>\n<li>type: string<\/li>\n<li>obligatoire : oui<\/li>\n<li>validation: isEmail<\/li>\n<li>longueur : 100<\/li>\n<li>langue : non<\/li>\n<\/ul>\n<\/li>\n<li>description\n<ul>\n<li>type : string<\/li>\n<li>obligatoire : non<\/li>\n<li>validation: isCleanHtml<\/li>\n<li>langue : oui<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Voici l\u2019aper\u00e7u des informations qui sont saisies pour g\u00e9n\u00e9rer ce mod\u00e8le.<br \/>Les champs Field Type et Field Validation sont compl\u00e9t\u00e9s automatiquement avec les informations n\u00e9cessaires.<br \/><br \/>Le champs <strong>Field Type<\/strong> r\u00e9cup\u00e8re les propositions depuis les types de classe situ\u00e9s dans le fichier classes\/ObjectModel.php<\/p>\n<p>Le champ <strong>Field Validation<\/strong> r\u00e9cup\u00e8re les propositions \u00e0 partir des fonctions de la classe classes\/Validate.php<\/p>\n<p>\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"535\" height=\"594\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/06\/generate-objectmodel.png\" alt=\"\" class=\"wp-image-1995\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/06\/generate-objectmodel.png 535w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2019\/06\/generate-objectmodel-270x300.png 270w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><figcaption>G\u00e9n\u00e9ration de l&rsquo;objet via la console<\/figcaption><\/figure>\n\n\n\n<p>Suite \u00e0 la saisie de ces informations le code suivant est g\u00e9n\u00e9r\u00e9 automatiquement dans le fichier modules\/<em>hhdev<\/em>\/classes\/<em>Sample<\/em>.php<\/p>\n\n\n\n<pre lang=\"php\" escaped=\"true\">\n <?php\n \/**\n * 2007-2019 PrestaShop\n *\n * NOTICE OF LICENSE\n *\n * This source file is subject to the Open Software License (OSL 3.0)\n * that is bundled with this package in the file LICENSE.txt.\n * It is also available through the world-wide-web at this URL:\n * https:\/\/opensource.org\/licenses\/OSL-3.0\n * If you did not receive a copy of the license and are unable to\n * obtain it through the world-wide-web, please send an email\n * to license@prestashop.com so we can send you a copy immediately.\n *\n * DISCLAIMER\n *\n * Do not edit or add to this file if you wish to upgrade PrestaShop to newer\n * versions in the future. If you wish to customize PrestaShop for your\n * needs please refer to http:\/\/www.prestashop.com for more information.\n *\n * @author    PrestaShop SA <contact@prestashop.com>\n * @copyright 2007-2019 PrestaShop SA\n * @license   https:\/\/opensource.org\/licenses\/OSL-3.0 Open Software License (OSL 3.0)\n * International Registered Trademark & Property of PrestaShop SA\n * @generated by PrestashopConsole  \n * @copyright 2016-2019 Hennes Herv\u00e9 http:\/\/www.h-hennes.fr\/blog\/\n *\/\n \n\nif (!defined('_PS_VERSION_')) {\n    exit;\n}\n\nclass Sample extends ObjectModel\n{\n\n   \/** @var int Object id *\/\n    public $id;\n    \n    public $name;\npublic $firstname;\npublic $email;\npublic $description;\n\n    \n    public static $definition = [\n        'table' => 'sample',\n        'primary' => 'id_sample',\n        'fields' => [ \n'name' => [ 'type' => self::TYPE_STRING,'validate' => 'isName', 'length' => 100, ], \n'firstname' => [ 'type' => self::TYPE_STRING,'validate' => 'isName', 'length' => 120, ], \n'email' => [ 'type' => self::TYPE_STRING,'validate' => 'isEmail', 'length' => 100, ], \n'description' => [ 'type' => self::TYPE_STRING,'validate' => 'isCleanHtml', 'lang' => true,], \n],'multilang' => true \n];\n    \/\/@Todo generate content\n    \n    \n        \/**\n         * Model Sql installation\n         * Add it in your module installation if necessary\n         *\/ \n        public static function installSql(){\n\n Db::getInstance()->execute(\n                \"CREATE TABLE IF NOT EXISTS `ps_sample`(\n`id_sample` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` VARCHAR (100) NOT NULL,\n`firstname` VARCHAR (120) ,\n`email` VARCHAR (100) NOT NULL,\n\n        PRIMARY KEY (`id_sample`)\n        )\n        ENGINE=InnoDB DEFAULT CHARSET=UTF8;\"\n                );\n\n\n Db::getInstance()->execute(\n                \"CREATE TABLE IF NOT EXISTS `ps_sample_lang`(\n`id_sample` int(10) unsigned NOT NULL AUTO_INCREMENT,\n`id_lang` int(10) unsigned NOT NULL ,\n`description` VARCHAR (255) \n,\n        PRIMARY KEY (`id_sample`,`id_lang`)\n        )\n        ENGINE=InnoDB DEFAULT CHARSET=UTF8;\"\n                );\n\n}\n}\n<\/pre>\n\n\n\n<p>Le formatage du contenu du fichier est encore \u00e0 am\u00e9liorer, mais le code est bien fonctionnel.<br \/><br \/>Il est possible de facilement cr\u00e9er la table sql associ\u00e9e \u00e0 ce mod\u00e8le en appelant la fonction <em>installSql()<\/em> de celui-ci.<\/p>\n<p>Pour g\u00e9rer rapidement un crud dans l&rsquo;administration il est possible d&rsquo;utiliser la commande <strong>module:generate:controller<\/strong> en lui passant en param\u00e8tre le mod\u00e8le. (cf. article pr\u00e9c\u00e9dent )<br \/><br \/>Ceci permettra d&rsquo;afficher un listing et un formulaire de modification basiques pour saisir des informations sur votre objet nouvellement cr\u00e9\u00e9 \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cet article est la suite de Prestashop : G\u00e9n\u00e9rer des modules via la console. Toujours dans la m\u00eame logique d&rsquo;optimisation de cr\u00e9ation des modules via la ligne de commande \ud83d\ude42 Je rappelle \u00e9galement\u00a0 que cette fonctionnalit\u00e9 n\u2019est pas disponible via la console native de Prestashop ( disponible \u00e0 partir de prestashop 1.7 ) mais sur [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[245],"tags":[440,544,238,104],"class_list":["post-1994","post","type-post","status-publish","format-standard","hentry","category-prestashop-2","tag-console","tag-generation","tag-objectmodel","tag-prestashop"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=1994"}],"version-history":[{"count":2,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1994\/revisions"}],"predecessor-version":[{"id":1997,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/1994\/revisions\/1997"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=1994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=1994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=1994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}