mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
352 lines
14 KiB
HTML
352 lines
14 KiB
HTML
|
|
|
|
<!DOCTYPE html>
|
|
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
|
|
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
|
|
<head>
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Database Seeding — CodeIgniter4 4.0.0-beta.3 documentation</title>
|
|
|
|
|
|
|
|
|
|
<link rel="shortcut icon" href="../_static/ci-icon.ico"/>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="top" title="CodeIgniter4 4.0.0-beta.3 documentation" href="../index.html"/>
|
|
<link rel="up" title="Managing Databases" href="index.html"/>
|
|
<link rel="next" title="Library Reference" href="../libraries/index.html"/>
|
|
<link rel="prev" title="Database Migrations" href="migration.html"/>
|
|
|
|
|
|
<script src="../_static/js/modernizr.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body class="wy-body-for-nav" role="document">
|
|
|
|
|
|
<div class="wy-grid-for-nav">
|
|
|
|
|
|
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
|
<div class="wy-side-scroll">
|
|
<div class="wy-side-nav-search">
|
|
|
|
|
|
|
|
<a href="../index.html" class="icon icon-home"> CodeIgniter4
|
|
|
|
|
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
<div role="search">
|
|
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
|
|
<input type="text" name="q" placeholder="Search docs" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a></li>
|
|
</ul>
|
|
<ul class="current">
|
|
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Managing Databases</a><ul class="current">
|
|
<li class="toctree-l2"><a class="reference internal" href="forge.html">Database Manipulation with Database Forge</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="migration.html">Database Migrations</a></li>
|
|
<li class="toctree-l2 current"><a class="current reference internal" href="#">Database Seeding</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../license.html">The MIT License (MIT)</a></li>
|
|
<li class="toctree-l1"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
|
|
</ul>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|
|
|
|
|
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
|
|
|
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|
<a href="../index.html">CodeIgniter4</a>
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
<div class="wy-nav-content">
|
|
<div class="rst-content">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div role="navigation" aria-label="breadcrumbs navigation">
|
|
|
|
<ul class="wy-breadcrumbs">
|
|
|
|
<li><a href="../index.html">Docs</a> »</li>
|
|
|
|
<li><a href="index.html">Managing Databases</a> »</li>
|
|
|
|
<li>Database Seeding</li>
|
|
|
|
|
|
<li class="wy-breadcrumbs-aside">
|
|
|
|
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
|
|
<hr/>
|
|
</div>
|
|
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
|
<div itemprop="articleBody">
|
|
|
|
<div class="section" id="database-seeding">
|
|
<h1>Database Seeding<a class="headerlink" href="#database-seeding" title="Permalink to this headline">¶</a></h1>
|
|
<p>Database seeding is a simple way to add data into your database. It is especially useful during development where
|
|
you need to populate the database with sample data that you can develop against, but it is not limited to that.
|
|
Seeds can contain static data that you don’t want to include in a migration, like countries, or geo-coding tables,
|
|
event or setting information, and more.</p>
|
|
<p>Database seeds are simple classes that must have a <strong>run()</strong> method, and extend <strong>CodeIgniterDatabaseSeeder</strong>.
|
|
Within the <strong>run()</strong> the class can create any form of data that it needs to. It has access to the database
|
|
connection and the forge through <code class="docutils literal"><span class="pre">$this->db</span></code> and <code class="docutils literal"><span class="pre">$this->forge</span></code>, respectively. Seed files must be
|
|
stored within the <strong>app/Database/Seeds</strong> directory. The name of the file must match the name of the class.</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">namespace</span> <span class="nx">App\Database\Seeds</span><span class="p">;</span>
|
|
|
|
<span class="k">class</span> <span class="nc">SimpleSeeder</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Database\Seeder</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">run</span><span class="p">()</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$data</span> <span class="o">=</span> <span class="p">[</span>
|
|
<span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'darth'</span><span class="p">,</span>
|
|
<span class="s1">'email'</span> <span class="o">=></span> <span class="s1">'darth@theempire.com'</span>
|
|
<span class="p">];</span>
|
|
|
|
<span class="c1">// Simple Queries</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">query</span><span class="p">(</span><span class="s2">"INSERT INTO users (username, email) VALUES(:username:, :email:)"</span><span class="p">,</span>
|
|
<span class="nv">$data</span>
|
|
<span class="p">);</span>
|
|
|
|
<span class="c1">// Using Query Builder</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">table</span><span class="p">(</span><span class="s1">'users'</span><span class="p">)</span><span class="o">-></span><span class="na">insert</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="section" id="nesting-seeders">
|
|
<h2>Nesting Seeders<a class="headerlink" href="#nesting-seeders" title="Permalink to this headline">¶</a></h2>
|
|
<p>Seeders can call other seeders, with the <strong>call()</strong> method. This allows you to easily organize a central seeder,
|
|
but organize the tasks into separate seeder files:</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">namespace</span> <span class="nx">App\Database\Seeds</span><span class="p">;</span>
|
|
|
|
<span class="k">class</span> <span class="nc">TestSeeder</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Database\Seeder</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">run</span><span class="p">()</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">call</span><span class="p">(</span><span class="s1">'UserSeeder'</span><span class="p">);</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">call</span><span class="p">(</span><span class="s1">'CountrySeeder'</span><span class="p">);</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">call</span><span class="p">(</span><span class="s1">'JobSeeder'</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>You can also use a fully-qualified class name in the <strong>call()</strong> method, allowing you to keep your seeders
|
|
anywhere the autoloader can find them. This is great for more modular code bases:</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">function</span> <span class="nf">run</span><span class="p">()</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">call</span><span class="p">(</span><span class="s1">'UserSeeder'</span><span class="p">);</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">call</span><span class="p">(</span><span class="s1">'My\Database\Seeds\CountrySeeder'</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="using-seeders">
|
|
<h2>Using Seeders<a class="headerlink" href="#using-seeders" title="Permalink to this headline">¶</a></h2>
|
|
<p>You can grab a copy of the main seeder through the database config class:</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$seeder</span> <span class="o">=</span> <span class="nx">\Config\Database</span><span class="o">::</span><span class="na">seeder</span><span class="p">();</span>
|
|
<span class="nv">$seeder</span><span class="o">-></span><span class="na">call</span><span class="p">(</span><span class="s1">'TestSeeder'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="section" id="command-line-seeding">
|
|
<h3>Command Line Seeding<a class="headerlink" href="#command-line-seeding" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can also seed data from the command line, as part of the Migrations CLI tools, if you don’t want to create
|
|
a dedicated controller:</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">></span> <span class="nx">php</span> <span class="nx">spark</span> <span class="nx">db</span><span class="o">:</span><span class="nx">seed</span> <span class="nx">TestSeeder</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
<div class="articleComments">
|
|
|
|
</div>
|
|
</div>
|
|
<footer>
|
|
|
|
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
|
|
|
<a href="../libraries/index.html" class="btn btn-neutral float-right" title="Library Reference" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
|
|
|
|
|
|
<a href="migration.html" class="btn btn-neutral" title="Database Migrations" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
|
|
|
|
</div>
|
|
|
|
|
|
<hr/>
|
|
|
|
<div role="contentinfo">
|
|
<p>
|
|
© Copyright 2014-2019 British Columbia Institute of Technology.
|
|
Last updated on May 06, 2019.
|
|
|
|
</p>
|
|
</div>
|
|
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
|
|
|
</footer>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT:'../',
|
|
VERSION:'4.0.0-beta.3',
|
|
COLLAPSE_INDEX:false,
|
|
FILE_SUFFIX:'.html',
|
|
HAS_SOURCE: false,
|
|
SOURCELINK_SUFFIX: ''
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="../_static/jquery.js"></script>
|
|
<script type="text/javascript" src="../_static/underscore.js"></script>
|
|
<script type="text/javascript" src="../_static/doctools.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript" src="../_static/js/theme.js"></script>
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
jQuery(function () {
|
|
SphinxRtdTheme.StickyNav.enable();
|
|
});
|
|
</script>
|
|
|
|
|
|
</body>
|
|
</html> |