<?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>Symfony中文教程 &#187; symfony Repository</title>
	<atom:link href="http://www.newlifeclan.com/symfony/archives/tag/symfony-repository/feed" rel="self" type="application/rss+xml" />
	<link>http://www.newlifeclan.com/symfony</link>
	<description>站在巨人肩膀上的phpweb框架</description>
	<lastBuildDate>Fri, 12 Dec 2025 00:58:27 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>KU案例2之11Repository Security</title>
		<link>http://www.newlifeclan.com/symfony/archives/430</link>
		<comments>http://www.newlifeclan.com/symfony/archives/430#comments</comments>
		<pubDate>Fri, 10 Apr 2015 07:48:17 +0000</pubDate>
		<dc:creator><![CDATA[napoleon]]></dc:creator>
				<category><![CDATA[实战教程]]></category>
		<category><![CDATA[symfony Repository]]></category>

		<guid isPermaLink="false">http://www.newlifeclan.com/symfony/?p=430</guid>
		<description><![CDATA[<p>现在，让我们的用户有一个电子邮件的字段，让它能够使用邮件或者用户登录。 给User一个email 让我们开始吧 [&#8230;]</p>
<p><a rel="nofollow" href="http://www.newlifeclan.com/symfony/archives/430">KU案例2之11Repository Security</a>，首发于<a rel="nofollow" href="http://www.newlifeclan.com/symfony">Symfony中文教程</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>现在，让我们的用户有一个电子邮件的字段，让它能够使用邮件或者用户登录。</p>
<p><span id="more-430"></span></p>
<h2>给User一个email</h2>
<p>让我们开始吧。添加这个属性到User类并使用annotation方式：</p><pre class="crayon-plain-tag">// src/Yoda/UserBundle/Entity/User.php
// ...

/**
 * @ORM\Column(type="string", length=255)
 */
private $email;</pre><p>下一步，为属性生成getter和setter。提醒一下：我们可以使用doctrine:generate:entities命令去生成：</p><pre class="crayon-plain-tag">php app/console doctrine:generate:entities UserBundle --no-backup</pre><p>&#8211;no-backup阻止命令去创建一个备份文件。当你正在使用版本控制的时候。</p>
<p>下一步，更新数据库</p><pre class="crayon-plain-tag">php app/console doctrine:schema:update --force</pre><p>最后，更新fixtures，使每个用户都有一个email：</p><pre class="crayon-plain-tag">// src/Yoda/UserBundle/DataFixtures/ORM/LoadUsers.php
// ...

public function load(ObjectManager $manager)
{
    // ...
    $user-&gt;setEmail('darth@deathstar.com');

    // ...
    $admin-&gt;setEmail('wayne@deathstar.com');

    // ...
}</pre><p>重新加载并刷新数据库</p><pre class="crayon-plain-tag">php app/console doctrine:fixtures:load</pre><p>&nbsp;</p>
<h2></h2>
<h2>Doctrine Repositories</h2>
<p>当有一个用户登录，我们的Security系统会使用username字段。因为我们的Security.yml之前配置过。我想改成email登录，现在我们没有办法控制email和username。如果想让它更加灵活，首先，我们需要了解一下Doctrine repositories。</p>
<p>找到并打开UserRepository：</p><pre class="crayon-plain-tag">// src/Yoda/UserBundle/Entity/UserRepository.php
namespace Yoda\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
}</pre><p>这就是一个Doctrine repository并且他是我们生成的。每个entity都有一个，通过在user类指定annotation就知道这个repository类了。</p><pre class="crayon-plain-tag">/**
 * ...
 *
 * @ORM\Entity(repositoryClass="Yoda\UserBundle\Entity\UserRepository")
 */
class User implements AdvancedUserInterface, Serializable</pre><p></p>
<blockquote><p> 注意，如果你不去配置repository类，Doctrine就只给你一个entity。</p></blockquote>
<p>Repositories是放至查询逻辑的地方。我们可以创建findActiveUsers之类的方法，从数据库中查询用户的isActive字段为1的数据。</p>
<h2>使用Repositories</h2>
<p>实际上，我们已经在项目中使用repositories了。打开EventController，并查看indexAction方法：</p><pre class="crayon-plain-tag">// src/Yoda/EventBundle/Controller/EventController.php
// ...

public function indexAction()
{
    $em = $this-&gt;getDoctrine()-&gt;getManager();

    $entities = $em-&gt;getRepository('EventBundle:Event')-&gt;findAll();

    return array(
        'entities' =&gt; $entities,
    );
}</pre><p></p>
<h2> 基础的EntityRepository</h2>
<p>我们调用entity管理里的getRepository方法来获得EventRepository。当我们打开这个类发现他是空的：</p><pre class="crayon-plain-tag">// src/Yoda/EventBundle/Entity/EventRepository.php
namespace Yoda\EventBundle\Entity;

use Doctrine\ORM\EntityRepository;

class EventRepository extends EntityRepository
{
    // nothing here... boring!
}</pre><p>请问findAll方法在哪里呢？答案在基础的EntityRepository类里。如果我们打开它，你会发现很多有用的方法包括findAll().所以说每一个repository类都可以使用这些附带方法。</p>
<p>让getRepository返回我们自己的EventRepository，让我们来继承父类的findAll()方法，方法里只是输入die来看看我们的代码：</p><pre class="crayon-plain-tag">// src/Yoda/EventBundle/Entity/EventRepository.php
// ...

class EventRepository extends EntityRepository
{
    public function findAll()
    {
        die('NOOOOOOOOO!!!!!!!!!!');
    }
}</pre><p>当我们调用这个页面，页面会给我们一个呐喊。</p>
<h2>repositoryClass配置</h2>
<p>现在打开Event entity。类的上面，你会看到一个@ORM\Entity注释：</p><pre class="crayon-plain-tag">// src/Yoda/EventBundle/Entity/Event.php
// ...

/**
 * @ORM\Entity(repositoryClass="Yoda\EventBundle\Entity\EventRepository")
 */
class Event</pre><p>这个repositoryClass是什么？是告诉Doctrine使用这个EventRepository。让我们删除这部分，看看会发生什么：</p><pre class="crayon-plain-tag">// src/Yoda/EventBundle/Entity/Event.php
// ...

/**
 * ...
 *
 * @ORM\Entity()
 */
class Event</pre><p>我们再刷新，就看不到呐喊声了。实际上，每个工作都很完美！我们并没有告诉Doctrine自定义的Repository，所以当我们在控制器调用getRepository时，他仅仅给我们一个实例化的EntityRepository类。</p>
<p>让我们还原刚才删除的部分并移除这个findAll假方法：</p><pre class="crayon-plain-tag">// src/Yoda/EventBundle/Entity/Event.php
// ...

/**
 * @ORM\Entity(repositoryClass="Yoda\EventBundle\Entity\EventRepository")
 */
class Event</pre><p>&nbsp;</p><pre class="crayon-plain-tag">// src/Yoda/EventBundle/Entity/EventRepository.php
// ...

class EventRepository extends EntityRepository
{
    public function findAll()
    {
        die('NOOOOOOOOO!!!!!!!!!!');
    }
}</pre><p>所以每一个entity都有一个Repository和一些辅助方法例如findAll。并且当我们的快捷方法无法满足你的工作时，你还可以自己定义自己的方法。我们所有的查询逻辑都应该放在这里-他会使你的生活更加有序。</p>
<p><a rel="nofollow" href="http://www.newlifeclan.com/symfony/archives/430">KU案例2之11Repository Security</a>，首发于<a rel="nofollow" href="http://www.newlifeclan.com/symfony">Symfony中文教程</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.newlifeclan.com/symfony/archives/430/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
