What's the difference between a Component and a Model in Craft 3?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm trying to familiarize myself with the Craft architecture and I can't understand whether I should be extending craftbaseComponent or craftbaseModel for my business logic models. Component seems like a pretty lean class that only adds a few initialization events to Model.










share|improve this question

























    up vote
    1
    down vote

    favorite












    I'm trying to familiarize myself with the Craft architecture and I can't understand whether I should be extending craftbaseComponent or craftbaseModel for my business logic models. Component seems like a pretty lean class that only adds a few initialization events to Model.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm trying to familiarize myself with the Craft architecture and I can't understand whether I should be extending craftbaseComponent or craftbaseModel for my business logic models. Component seems like a pretty lean class that only adds a few initialization events to Model.










      share|improve this question













      I'm trying to familiarize myself with the Craft architecture and I can't understand whether I should be extending craftbaseComponent or craftbaseModel for my business logic models. Component seems like a pretty lean class that only adds a few initialization events to Model.







      plugin-development craft3 models element






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 28 at 18:50









      Lazlo

      1406




      1406




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          Models are classes that contains abstractions of the reality, for example you could have the model car that contains the technical information about it, how many wheels it has and it's brand. Models are usually not supposed to contain any logic. So it's just a "container" with information



          Components are more like services that contains business logic to work with your models, for example the service car would handle the logic to let your car model drive and move and such. Components in Yii2 are mostly (not always) treated as singletons while you want to have many different instances of your models






          share|improve this answer




















          • Thanks for the clarification! I just realized Services in Craft were actually extending Components in Yii. A bit confusing in terminology, but I'm starting to wrap my head around it!
            – Lazlo
            Aug 28 at 19:51











          • A minor caveat, and a caution. First the caveat: there is a strong argument that anemic models are an anti-pattern. That said, Robin is correct in stating that this is The Craft Way™. That leads me to the caution: bear in mind that your service classes "define your plugin’s API, which your plugin (and other plugins) can access" (source; emphasis added).
            – Stephen Lewis
            Aug 29 at 6:06











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "563"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcraftcms.stackexchange.com%2fquestions%2f27506%2fwhats-the-difference-between-a-component-and-a-model-in-craft-3%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          Models are classes that contains abstractions of the reality, for example you could have the model car that contains the technical information about it, how many wheels it has and it's brand. Models are usually not supposed to contain any logic. So it's just a "container" with information



          Components are more like services that contains business logic to work with your models, for example the service car would handle the logic to let your car model drive and move and such. Components in Yii2 are mostly (not always) treated as singletons while you want to have many different instances of your models






          share|improve this answer




















          • Thanks for the clarification! I just realized Services in Craft were actually extending Components in Yii. A bit confusing in terminology, but I'm starting to wrap my head around it!
            – Lazlo
            Aug 28 at 19:51











          • A minor caveat, and a caution. First the caveat: there is a strong argument that anemic models are an anti-pattern. That said, Robin is correct in stating that this is The Craft Way™. That leads me to the caution: bear in mind that your service classes "define your plugin’s API, which your plugin (and other plugins) can access" (source; emphasis added).
            – Stephen Lewis
            Aug 29 at 6:06















          up vote
          5
          down vote



          accepted










          Models are classes that contains abstractions of the reality, for example you could have the model car that contains the technical information about it, how many wheels it has and it's brand. Models are usually not supposed to contain any logic. So it's just a "container" with information



          Components are more like services that contains business logic to work with your models, for example the service car would handle the logic to let your car model drive and move and such. Components in Yii2 are mostly (not always) treated as singletons while you want to have many different instances of your models






          share|improve this answer




















          • Thanks for the clarification! I just realized Services in Craft were actually extending Components in Yii. A bit confusing in terminology, but I'm starting to wrap my head around it!
            – Lazlo
            Aug 28 at 19:51











          • A minor caveat, and a caution. First the caveat: there is a strong argument that anemic models are an anti-pattern. That said, Robin is correct in stating that this is The Craft Way™. That leads me to the caution: bear in mind that your service classes "define your plugin’s API, which your plugin (and other plugins) can access" (source; emphasis added).
            – Stephen Lewis
            Aug 29 at 6:06













          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          Models are classes that contains abstractions of the reality, for example you could have the model car that contains the technical information about it, how many wheels it has and it's brand. Models are usually not supposed to contain any logic. So it's just a "container" with information



          Components are more like services that contains business logic to work with your models, for example the service car would handle the logic to let your car model drive and move and such. Components in Yii2 are mostly (not always) treated as singletons while you want to have many different instances of your models






          share|improve this answer












          Models are classes that contains abstractions of the reality, for example you could have the model car that contains the technical information about it, how many wheels it has and it's brand. Models are usually not supposed to contain any logic. So it's just a "container" with information



          Components are more like services that contains business logic to work with your models, for example the service car would handle the logic to let your car model drive and move and such. Components in Yii2 are mostly (not always) treated as singletons while you want to have many different instances of your models







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 28 at 19:24









          Robin Schambach

          13.6k1427




          13.6k1427











          • Thanks for the clarification! I just realized Services in Craft were actually extending Components in Yii. A bit confusing in terminology, but I'm starting to wrap my head around it!
            – Lazlo
            Aug 28 at 19:51











          • A minor caveat, and a caution. First the caveat: there is a strong argument that anemic models are an anti-pattern. That said, Robin is correct in stating that this is The Craft Way™. That leads me to the caution: bear in mind that your service classes "define your plugin’s API, which your plugin (and other plugins) can access" (source; emphasis added).
            – Stephen Lewis
            Aug 29 at 6:06

















          • Thanks for the clarification! I just realized Services in Craft were actually extending Components in Yii. A bit confusing in terminology, but I'm starting to wrap my head around it!
            – Lazlo
            Aug 28 at 19:51











          • A minor caveat, and a caution. First the caveat: there is a strong argument that anemic models are an anti-pattern. That said, Robin is correct in stating that this is The Craft Way™. That leads me to the caution: bear in mind that your service classes "define your plugin’s API, which your plugin (and other plugins) can access" (source; emphasis added).
            – Stephen Lewis
            Aug 29 at 6:06
















          Thanks for the clarification! I just realized Services in Craft were actually extending Components in Yii. A bit confusing in terminology, but I'm starting to wrap my head around it!
          – Lazlo
          Aug 28 at 19:51





          Thanks for the clarification! I just realized Services in Craft were actually extending Components in Yii. A bit confusing in terminology, but I'm starting to wrap my head around it!
          – Lazlo
          Aug 28 at 19:51













          A minor caveat, and a caution. First the caveat: there is a strong argument that anemic models are an anti-pattern. That said, Robin is correct in stating that this is The Craft Way™. That leads me to the caution: bear in mind that your service classes "define your plugin’s API, which your plugin (and other plugins) can access" (source; emphasis added).
          – Stephen Lewis
          Aug 29 at 6:06





          A minor caveat, and a caution. First the caveat: there is a strong argument that anemic models are an anti-pattern. That said, Robin is correct in stating that this is The Craft Way™. That leads me to the caution: bear in mind that your service classes "define your plugin’s API, which your plugin (and other plugins) can access" (source; emphasis added).
          – Stephen Lewis
          Aug 29 at 6:06


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcraftcms.stackexchange.com%2fquestions%2f27506%2fwhats-the-difference-between-a-component-and-a-model-in-craft-3%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?