<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: northgoingzax</title>
    <description>The latest articles on Forem by northgoingzax (@northgoingzax).</description>
    <link>https://forem.com/northgoingzax</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55163%2Fb913d6bc-587b-4abb-8ebe-f9d93b33abe5.png</url>
      <title>Forem: northgoingzax</title>
      <link>https://forem.com/northgoingzax</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/northgoingzax"/>
    <language>en</language>
    <item>
      <title>CakePHP 3: Bake by example</title>
      <dc:creator>northgoingzax</dc:creator>
      <pubDate>Fri, 14 Sep 2018 07:26:58 +0000</pubDate>
      <link>https://forem.com/northgoingzax/cakephp-3-bake-by-example-40pp</link>
      <guid>https://forem.com/northgoingzax/cakephp-3-bake-by-example-40pp</guid>
      <description>&lt;p&gt;This article is aimed at newbies to the CakePHP 3 framework.&lt;/p&gt;

&lt;p&gt;I'm a fan of CakePHP, and was completely new to it about 9 months ago. Any framework takes some getting used to, but my biggest issue with Cake 3 is the manual. I feel it is written very much with a feeling of "you're familiar with Cake 2, so we don't need explain as much with this one". &lt;/p&gt;

&lt;p&gt;Once you get used to using the framework however, it is a very rewarding framework, and luckily the &lt;a href="https://irc.cakephp.org/"&gt;IRC channel&lt;/a&gt; and &lt;a href="https://stackoverflow.com/questions/tagged/cakephp-3.x"&gt;cakephp-3.x&lt;/a&gt; tag on stack overflow is actively maintained, so you can work it out or get an answer pretty quickly. &lt;/p&gt;

&lt;p&gt;Rather than moan about the manual, I thought I would post an example-based guide to the code generation feature, to help any other new-to-cake devs out there.&lt;/p&gt;

&lt;p&gt;These examples are deliberately simple, they're just to get you started with the code generator, rather than just the basic list of commands currently available in the &lt;a href="https://book.cakephp.org/3.0/en/bake/usage.html"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Before you use the bake command&lt;/em&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Create some tables, following CakePHP &lt;a href="https://book.cakephp.org/3.0/en/intro/conventions.html#database-conventions"&gt;conventions&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Check you are in the root directory of your project, if you do a directory listing &lt;code&gt;ls&lt;/code&gt; you should see folders "bin", "config", "src", "webroot"&lt;/li&gt;
&lt;/ol&gt;




&lt;h4&gt;
  
  
  Create a controller for a table called &lt;code&gt;users&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake controller Users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;creates src/Controller/UsersController.php&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Create a model (and entity) for a table called &lt;code&gt;users&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake model Users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;creates src/Model/Table/UsersTable.php&lt;br&gt;
creates src/Model/Entities/User.php&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Create the default template files for a table (add/edit/view/index)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake template Users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;creates src/Template/Users/index.ctp add.ctp edit.ctp view.ctp&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Bake all of the above in 1 command
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake all Users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Bake just the index template file
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake template Users index
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;creates src/Template/Users/index.ctp&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Baking for prefix folders (e.g. admin)
&lt;/h4&gt;

&lt;p&gt;If you are baking for an admin section of your site, you will be using separate controller and template files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake controller Users &lt;span class="nt"&gt;--prefix&lt;/span&gt; admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;creates src/Controller/Admin/UsersController.php&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake template Users &lt;span class="nt"&gt;--prefix&lt;/span&gt; admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;creates src/Template/Admin/Users/index.ctp edit.ctp add.ctp view.ctp&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Bake from a different database
&lt;/h3&gt;

&lt;p&gt;The previous examples all use the db connection defined in &lt;code&gt;app.php&lt;/code&gt; as &lt;code&gt;'default'&lt;/code&gt;. If you have a legacy database for handling client records, e.g. &lt;code&gt;db_records&lt;/code&gt;, your &lt;a href="https://book.cakephp.org/3.0/en/orm/database-basics.html#configuration"&gt;config file&lt;/a&gt; might look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// in config/app.php&lt;/span&gt;
&lt;span class="s1"&gt;'Datasources'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'default'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'host'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'username'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'db_user'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'password'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'pass123'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'database'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'db_application'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="s1"&gt;'records'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'host'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'username'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'db_records_user'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'password'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'pass123'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'database'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'db_records'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create a model for a table called &lt;code&gt;user_records&lt;/code&gt; in the records database
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake model UserRecords &lt;span class="nt"&gt;-c&lt;/span&gt; records
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;creates /src/Model/Table/UserRecordsTable.php&lt;br&gt;
creates /src/Model/Entities/UserRecord.php&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This will include in your &lt;code&gt;UserRecordsTable.php&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;defaultConnectionName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'records'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create a model from this database for a table not following cake convention, e.g. &lt;code&gt;tbl_records_user&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake model UserRecords &lt;span class="nt"&gt;-c&lt;/span&gt; records &lt;span class="nt"&gt;--table&lt;/span&gt; tbl_records_user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;creates /src/Model/Table/UserRecordsTable.php&lt;br&gt;
creates /src/Model/Entities/UserRecord.php&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This will add the &lt;code&gt;defaultConnectionName()&lt;/code&gt; and also set&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'tbl_records_user'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are other options you can set here, but some are easier to set by editing the file afterwards. But for example, you can also set the &lt;a href="https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs"&gt;display field&lt;/a&gt; to be something other than the default, e.g. email&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake model UserRecords &lt;span class="nt"&gt;-c&lt;/span&gt; records &lt;span class="nt"&gt;--table&lt;/span&gt; tbl_records_user &lt;span class="nt"&gt;--display-field&lt;/span&gt; email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will modify the display field&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setDisplayField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Associations
&lt;/h4&gt;

&lt;p&gt;By default the &lt;code&gt;bake&lt;/code&gt; command will look for associations. If you are using a legacy table, or a different datasource, any field headings that end in &lt;code&gt;_id&lt;/code&gt; might cause a 'base table or view not found' error.&lt;/p&gt;

&lt;p&gt;To avoid this you can bake without associations&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake model UserRecords &lt;span class="nt"&gt;-c&lt;/span&gt; records &lt;span class="nt"&gt;--table&lt;/span&gt; tbl_records_user &lt;span class="nt"&gt;--no-associations&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Baking with a plugin
&lt;/h3&gt;

&lt;p&gt;Assume you have installed a plugin such as &lt;a href="https://github.com/FriendsOfCake/bootstrap-ui"&gt;friendsofcake/BootstrapUI&lt;/a&gt; so that your template files are using Bootstrap styles by default&lt;br&gt;
You can now add &lt;code&gt;-t BootstrapUI&lt;/code&gt; to any of the above commands&lt;/p&gt;
&lt;h4&gt;
  
  
  Create the template files using the plugin
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;creates /src/Template/Users/index.ctp add.ctp edit.ctp view.ctp&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake template Users &lt;span class="nt"&gt;-t&lt;/span&gt; BootstrapUI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create the whole MVC skeleton (controller, table, entity, templates) using the legacy database
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake all UserRecords &lt;span class="nt"&gt;-c&lt;/span&gt; records &lt;span class="nt"&gt;--table&lt;/span&gt; tbl_records_user &lt;span class="nt"&gt;-t&lt;/span&gt; BootstrapUI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Useful Features
&lt;/h3&gt;

&lt;p&gt;Bake relies on the database connection, so to save you loading up and remembering all the tables in the system, you can call a command without specifying the table name to see a list of available tables. Let's say your database has 3 tables:&lt;br&gt;
&lt;code&gt;users&lt;/code&gt;&lt;br&gt;
&lt;code&gt;articles&lt;/code&gt;&lt;br&gt;
&lt;code&gt;activity_logs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using the command &lt;code&gt;bin/cake bake model&lt;/code&gt; will produce a list of available tables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake model
Choose a model to bake from the following:
Users
Articles
ActivityLogs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same goes for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/cake bake controller
bin/cake bake template
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Hopefully if you're using CakePHP for the first time this is a useful reference for the bake console.&lt;/p&gt;

</description>
      <category>cakephp</category>
      <category>php</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
