<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>How to Dump the Generated Zend_Db SQL Query</title>
		<link>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/</link>
		<comments>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 14:39:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[From]]></category>
		<category><![CDATA[Insert]]></category>
		<category><![CDATA[PHP programmer]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL keywords]]></category>

		<guid isPermaLink="false">/?p=2709</guid>
		<description><![CDATA[The Typical PHP Approach Typically a PHP programmer will write his SQL query as a string and will execute it via mysql_query. $sql = "SELECT * FROM my_table"; $resource = mysql_query($sql); So eventually when you want to dump this &#8220;complex&#8221; query, or whatever query there is, you can simply &#8220;echo&#8221; it and see what’s its &#8230; <a href="/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/" class="more-link">Continue reading <span class="screen-reader-text">How to Dump the Generated Zend_Db SQL Query</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/07/28/which-model-should-contain-that-method/" rel="bookmark" title="Which Model Should Contain That Method?">Which Model Should Contain That Method? </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>The Typical PHP Approach</h2>
<p>Typically a PHP programmer will write his SQL query as a string and will execute it via <a href="http://php.net/manual/en/function.mysql-query.php" title="PHP: mysql_query" target="_blank">mysql_query</a>.</p>
<pre lang="PHP">
$sql = "SELECT * FROM my_table";
$resource = mysql_query($sql);
</pre>
<p>So eventually when you want to dump this &#8220;complex&#8221; query, or whatever query there is, you can simply &#8220;echo&#8221; it and see what’s its syntax. </p>
<pre lang="PHP">
// this query is WRONG because of the where clause
$sql = "SELECT * FROM my_table WHERE id = ";

// dump and debug the wrong query
die($sql);

// this line won't be executed
$resource = mysql_query($sql);
</pre>
<p>So far so good, but things appear to be a bit different when you start to work with <a href="http://framework.zend.com/" title="Zend Framework" target="_blank">Zend Framework</a>. Higher levels of abstraction come with slightly more difficult ways to dump (debug) your SQL queries.</p>
<p>OK you&#8217;ve two options. Using <a href="http://framework.zend.com/manual/en/zend.db.select.html" title="Zend_Db_Select" target="_blank">Zend_Db_Select</a> or &#8230; not.<br />
<span id="more-2709"></span></p>
<h2>Dump Zend_Db_Select Query</h2>
<p>Debugging queries generated with Zend_Db_Select is as easy as the following snippet. Let’s say there’s a db table “users” and typically the model is called Users.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = 'users';

	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');

		$rows = $this->fetchAll($select);
	}
}
</pre>
<p>Obviously this code is wrong, because the &#8220;where&#8221; clause is wrong and we don&#8217;t pass any values to it. So now the question is how can we dump this SQL. Well, since we use Zend_Db_Select, so the simplest way to dump the SQL is by calling __toString(). The code above will become as follows.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;

	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');

		die($select->__toString());

		$rows = $this->fetchAll($select);
	}
}
</pre>
<p>That&#8217;s great, but how can we dump update/insert queries for instance?</p>
<h2>Dump with Zend_Db_Profiler</h2>
<p>The way you should dump insert or update queries is a bit different. You should use <a href="http://framework.zend.com/manual/en/zend.db.profiler.html" title="Zend_Db_Profiler" target="_blank">Zend_Db_Profiler</a>, which is a more complex way to debug (profile) your queries. You can not only debug insert or update queries, of course, but any queries generated by the Zend_Db abstraction. Let’s see how.</p>
<p>Now from a controller perspective this model can be just called as:</p>
<pre lang="PHP">
$users = new Users();
$users->update(array('name' => 'my name'), 'id =');
</pre>
<p>Obviously this code is wrong, again because of the where clause and now the question is how we can debug the generated SQL. The answer is: using Zend_Db_Profiler. Looking back again to the model, this should look like this.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;

	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);

		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');

		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());

		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
	}
}
</pre>
<p>The thing is that since Zend_Db is using prepared statements, the last row (the one with the Zend_Debug::&#8230;) will dump something like the following line.</p>
<pre lang="SQL">
UPDATE `users` SET `name` = ? WHERE (id =)
</pre>
<p>So as you can see, actually here you don’t see the name’s value, which should be “my name”. To see this you should call getQueryParams().</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = 'users';

	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);

		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');

		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQueryParams());

		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
</pre>
<p>Now the debug info will be something like this.</p>
<pre lang="PHP">
UPDATE `users` SET `name` = ? WHERE (id =)
array
	1 => string 'my name' (length 7)
</pre>
<p>Calling this from the controller there are some changes. Take a look at the example bellow.</p>
<pre lang="PHP">
$users = new Users();

// enable the profiler
$users->getAdapter()->getProfiler()->setEnabled(true);

// execute (or at least try to)
$users->update(array('name' => 'my name'), 'id =');

// debugdump the SQL
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQuery());
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQueryParams());

// don't forget to disable the profiler is
// you don't need it anymore
$users->getAdapter()->getProfiler()->setEnabled(false);
</pre>
<p>In this case you should use the longer model_name->getAdapter()</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/07/28/which-model-should-contain-that-method/" rel="bookmark" title="Which Model Should Contain That Method?">Which Model Should Contain That Method? </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Which Model Should Contain That Method?</title>
		<link>/2010/07/28/which-model-should-contain-that-method/</link>
		<comments>/2010/07/28/which-model-should-contain-that-method/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 15:07:41 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Business/Finance]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data management]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database management systems]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[From]]></category>
		<category><![CDATA[Human Interest]]></category>
		<category><![CDATA[IBM software]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL keywords]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">/?p=1866</guid>
		<description><![CDATA[Let&#8217;s say you have two models &#8211; each one modeling a database table &#8211; Users &#38; Article. Here there&#8217;s nothing to deal with Zend Framwork, but you can think of them as typical models in a ZF application. What happens if you have to write a getUserArticles method? Where would you put it? Whether this &#8230; <a href="/2010/07/28/which-model-should-contain-that-method/" class="more-link">Continue reading <span class="screen-reader-text">Which Model Should Contain That Method?</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/12/mvc-in-practice-designing-models/" rel="bookmark" title="MVC in Practice: Designing Models">MVC in Practice: Designing Models </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s say you have two models &#8211; each one modeling a database table &#8211; <strong>Users</strong> &amp; <strong>Article</strong>. Here there&#8217;s nothing to deal with Zend Framwork, but you can think of them as typical models in a ZF application.</p>
<p>What happens if you have to write a getUserArticles method? Where would you put it? Whether this will be the User model or the Article model?<a href="/wp-content/uploads/2010/07/question.jpg"><img class="aligncenter size-full  wp-image-1875" title="question" src="/wp-content/uploads/2010/07/question.jpg" alt="Where?" width="430" height="231" srcset="/wp-content/uploads/2010/07/question.jpg 430w, /wp-content/uploads/2010/07/question-300x161.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<p>Although technically you can put it in both models my advice is to look at the SQL query. If the FROM clause is containing the user table &#8211; than put the method in the User model, but here you&#8217;d have something like:</p>
<pre lang="sql">
SELECT * FROM Article WHERE user_id = 1
</pre>
<p>I&#8217;d prefer to place it in the Article model!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/12/mvc-in-practice-designing-models/" rel="bookmark" title="MVC in Practice: Designing Models">MVC in Practice: Designing Models </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/28/which-model-should-contain-that-method/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
