<?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>Agence de Développement Informatique du Nord &#187; Doctrine</title>
	<atom:link href="http://blog.adin.pro/category/doctrine/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.adin.pro</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Thu, 26 Dec 2019 08:54:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>doctrine2 random</title>
		<link>http://blog.adin.pro/2014-03-06/doctrine2-random/</link>
		<comments>http://blog.adin.pro/2014-03-06/doctrine2-random/#comments</comments>
		<pubDate>Thu, 06 Mar 2014 20:21:46 +0000</pubDate>
		<dc:creator><![CDATA[blogadmin]]></dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[doctrine2]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[rand]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[Symfony2]]></category>

		<guid isPermaLink="false">http://blog.adin.pro/?p=367</guid>
		<description><![CDATA[<p>For some really good reasons or, doctrine2 do not implement the RAND() fonction to sort randomly your query results. Here some dirty ways to do it. Don&#8217;t use them it&#8217;s bad. One solution is to shuffle your collection of rows &#8230; <a href="http://blog.adin.pro/2014-03-06/doctrine2-random/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="http://blog.adin.pro/2014-03-06/doctrine2-random/">doctrine2 random</a> appeared first on <a rel="nofollow" href="http://blog.adin.pro">Agence de Développement Informatique du Nord</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>For some really good reasons or, doctrine2 do not implement the RAND() fonction to sort randomly your query results. Here some dirty ways to do it. Don&#8217;t use them it&#8217;s bad.</p>
<p>One solution is to shuffle your collection of rows using php</p>
<pre class="brush: php; title: ; notranslate">
      public function getRandom($max = 10,$site) {
        $results = $this-&gt;createQueryBuilder('u')
                -&gt;where('u.site = :site')
                -&gt;setParameter('site', $site)
                -&gt;orderBy('u.sort', 'DESC')
                -&gt;setMaxResults($max)
                -&gt;getQuery()
                -&gt;getResult();
        shuffle($results);
        return $results;
    }
</pre>
<p>In this solution, you retrieve the last 10 rows and shuffle them after. Peformances are not too bad but you will always retrieves the same last rows from your table.</p>
<p>Another solution is to use the array_rand php fonction</p>
<pre class="brush: php; title: ; notranslate">
      public function getRandom($site) {
        $results = $this-&gt;createQueryBuilder('u')
                -&gt;where('u.site = :site')
                -&gt;setParameter('site', $site)
                -&gt;orderBy('u.sort', 'DESC')
                -&gt;getQuery()
                -&gt;getResult();
        $result2 = array_rand($results);
        return $result2;
    }
</pre>
<p>In this case you fetch all rows from your table, this could be slow and memory consuming&#8230;</p>
<p>If you need to retrieve only one row, you can use somethinfg like this</p>
<pre class="brush: php; title: ; notranslate">
 public function getOneRandom()
{
$em = $this-&gt;getEntityManager();
$max = $em-&gt;createQuery('
SELECT MAX(q.id) FROM questions q
')
-&gt;getSingleScalarResult();
return $em-&gt;createQuery('
SELECT q FROM questions q
WHERE q.id &gt;= :random
ORDER BY q.id ASC
')
-&gt;setParameter('random',rand(0,$max))
-&gt;setMaxResults(1)
-&gt;getSingleResult();
}
</pre>
<p>This solution can only be used if you want to retrieve any of the tables rows, if you add a filtrer you may return an empty result.</p>
<p>This solution is not dirty it just use 2 queries instead of one. the tips is to use the offset. And you can use a filter !!!</p>
<pre class="brush: php; title: ; notranslate">
$qCount = Doctrine::getTable('Questions')
     -&gt;createQuery()
     -&gt;select('count(*)')
     -&gt;where('site = :site')
     -&gt;setParameter('site', $site)
     -&gt;fetchOne(array(), Doctrine::HYDRATE_NONE);
$question = Doctrine::getTable('Questions')
     -&gt;createQuery()
     -&gt;select('*')
     -&gt;where('site = :site')
     -&gt;setParameter('site', $site)
     -&gt;limit(1)
     -&gt;offset(rand(0, $qCount[0] - 1))
     -&gt;fetchOne();
</pre>
<p>And you still can use native queries : http://docs.doctrine-project.org/en/latest/reference/native-sql.html</p>
<p>The post <a rel="nofollow" href="http://blog.adin.pro/2014-03-06/doctrine2-random/">doctrine2 random</a> appeared first on <a rel="nofollow" href="http://blog.adin.pro">Agence de Développement Informatique du Nord</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adin.pro/2014-03-06/doctrine2-random/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Doctrine2 override save method</title>
		<link>http://blog.adin.pro/2013-10-08/doctrine2-override-save-method/</link>
		<comments>http://blog.adin.pro/2013-10-08/doctrine2-override-save-method/#comments</comments>
		<pubDate>Tue, 08 Oct 2013 19:51:44 +0000</pubDate>
		<dc:creator><![CDATA[blogadmin]]></dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[doctrine2]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Sonata]]></category>
		<category><![CDATA[Symfony2]]></category>

		<guid isPermaLink="false">http://blog.adin.pro/?p=308</guid>
		<description><![CDATA[<p>With Doctrine2 you cannot override save method but you can execute preUpdate() method. See below how to implement it. On your entity class, for example AdminBundle/Entity/Annonce.php, add the HasLifecycleCallbacks annontation And your method, with the annotation That&#8217;s all, it should &#8230; <a href="http://blog.adin.pro/2013-10-08/doctrine2-override-save-method/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="http://blog.adin.pro/2013-10-08/doctrine2-override-save-method/">Doctrine2 override save method</a> appeared first on <a rel="nofollow" href="http://blog.adin.pro">Agence de Développement Informatique du Nord</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>With Doctrine2 you cannot override save method but you can execute preUpdate() method.<br />
See below how to implement it.</p>
<p>On your entity class, for example AdminBundle/Entity/Annonce.php, add the HasLifecycleCallbacks annontation</p>
<pre class="brush: php; title: ; notranslate">
/**
 * @ORM\Entity(repositoryClass=&quot;Tripix\AdminBundle\Repository\AnnonceRepository&quot;)
 * @ORM\HasLifecycleCallbacks
 */
</pre>
<p>And your method, with the annotation</p>
<pre class="brush: php; title: ; notranslate">
/** @ORM\PreUpdate() */
        public function preUpdate()
        {
                $this-&gt;created_at = new \DateTime(&quot;now&quot;);
        }
</pre>
<p>That&#8217;s all, it should works.</p>
<p>A complete example, just in case :</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
namespace Tripix\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * @ORM\Entity(repositoryClass=&quot;Tripix\AdminBundle\Repository\AnnonceRepository&quot;)
 * @ORM\HasLifecycleCallbacks
 */
class Annonce
{

/** @ORM\PreUpdate() */
        public function preUpdate()
        {
                $this-&gt;created_at = new \DateTime(&quot;now&quot;);
        }

    public function __construct()
    {
        $this-&gt;created_at = new \DateTime(&quot;now&quot;);
        $this-&gt;visuels = new ArrayCollection();
    }

</pre>
<p>The post <a rel="nofollow" href="http://blog.adin.pro/2013-10-08/doctrine2-override-save-method/">Doctrine2 override save method</a> appeared first on <a rel="nofollow" href="http://blog.adin.pro">Agence de Développement Informatique du Nord</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.adin.pro/2013-10-08/doctrine2-override-save-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
