Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@JohnP's doesn't take into account the table's prefix as configured in <a href="http://book.cakephp.org/2.0/en/development/configuration.html#database-configuration" rel="nofollow">database.php</a>. Here's a slightly more robust approach. </p> <p>The <a href="http://api.cakephp.org/2.3/class-DboSource.html" rel="nofollow">DboSource</a> object attached to each model already has a <a href="http://api.cakephp.org/2.3/class-DboSource.html#_fullTableName" rel="nofollow">fullTableName()</a> method that does exactly what we need.</p> <p>First, <a href="http://book.cakephp.org/2.0/en/models.html#understanding-models" rel="nofollow">create</a> <code>Model/AppModel.php</code> if it doesn't already exist, and add this method to it:</p> <pre><code>/** * fullTableName * * Provides access to the Model's DataSource's ::fullTableName() method. * Returns the fully quoted and prefixed table name for the current Model. * * @access public * @param boolean $quote Whether you want the table name quoted. * @param boolean $schema Whether you want the schema name included. * @return string Full quoted table name. */ public function fullTableName($quote = true, $schema = true) { $datasource = $this-&gt;GetDataSource(); return $datasource-&gt;fullTableName($this, $quote, $schema); } </code></pre> <p>With this, you can get the full table name, including the prefix, for <strong>any</strong> Model in your Cake app: </p> <p><code>$this-&gt;Model-&gt;fullTableName();</code></p> <p>We can do better though. Next, add this method to AppModel too:</p> <pre><code>/** * truncate * * Truncates ALL RECORDS from the Model it is called from! VERY DANGEROUS! * Depends on the ::fullTableName() method to concatenate the configured * table prefix and table name together and quote the whole bit properly. * * @access public * @return mixed */ public function truncate() { $fullName = $this-&gt;fullTableName(); $q = 'TRUNCATE TABLE %s'; return $this-&gt;query(sprintf($q, $fullName)); } </code></pre> <p>Now you can (easily, so be careful!) truncate any Model in your app like so: </p> <p><code>$this-&gt;Model-&gt;truncate();</code></p> <p>And if you ever need to adjust the SQL query to match a different DataSource, you can do so in a central place in your app. You can also easily override the <code>truncate()</code> method in specific models if they use a different DataSource with a different syntax.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload