<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Dave Dash</title>
 <link href="http://davedash.com/tag/doctrine/atom.xml" rel="self"/>
 <link href="http://davedash.com/tag/doctrine"/>
 <updated>2012-01-17T21:54:19-08:00</updated>
 <id>http://davedash.com/</id>
 <author>
   <name>Dave Dash</name>
   <email>dd+atom1@davedash.com</email>
 </author>

 
 <entry>
   <title>doctine and getState()</title>
   <link href="http://davedash.com/2007/07/10/doctine-and-getstate/"/>
   <updated>2007-07-10T00:00:00-07:00</updated>
   <id>http://davedash.com/2007/07/10/doctine-and-getstate</id>
   <content type="html">&lt;p&gt;[tags]doctrine, php, symfony, sfDoctrine, database,errors[/tags]&lt;/p&gt;

&lt;p&gt;I tend to have models with a field called &lt;code&gt;state&lt;/code&gt;.  Doctrine offers a few ways of getting to the &lt;code&gt;state&lt;/code&gt; field:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$obj-&amp;gt;get('state');
$obj['state'];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;$obj-&amp;gt;getState()&lt;/code&gt; however conflicts with &lt;code&gt;Doctrine_Record::getState()&lt;/code&gt; from which all objects inherit.  Use one of the above alternatives.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Using sfDoctrine to match allowed email domains</title>
   <link href="http://davedash.com/2007/07/09/using-sfdoctrine-to-match-allowed-email-domains/"/>
   <updated>2007-07-09T00:00:00-07:00</updated>
   <id>http://davedash.com/2007/07/09/using-sfdoctrine-to-match-allowed-email-domains</id>
   <content type="html">&lt;p&gt;[tags]php, propel, doctrine, validators, symfony, optiopt, startup[/tags]&lt;/p&gt;

&lt;p&gt;I'm a co-founder at an online finance web site and I'm in charge with building out the site.  Our rollout strategy is to let a a handful of companies at a time, so we're limiting registration based on your company's email address.&lt;/p&gt;

&lt;p&gt;I decided to follow the bandwagon and use PHP Doctrine.  We'll define two objects, &lt;code&gt;Company&lt;/code&gt; and &lt;code&gt;CompanyDomain&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Company:
  tableName: company
  columns:
    name: {type: string(100)}
    created_at: timestamp
    updated_at: timestamp

CompanyDomain:
  tableName: company_domain
  columns:
    company_id:
      foreignClass: Company
      cascadeDelete: true
    pattern: {type: string(100)}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where a &lt;code&gt;CompanyDomain&lt;/code&gt; represents a domain name for a company.  E.g. Motorola might have both &lt;code&gt;motorola.com&lt;/code&gt; and &lt;code&gt;mot.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can validate a signup form and see if we've got an email address from a domain we recognize.  I like using the &lt;code&gt;validationXXX&lt;/code&gt; methods in an action class for specific validation.  I made one called &lt;code&gt;validateSignup&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public function validateSignup()
{
    if ($this-&amp;gt;isPost())
    {
        $email = $this-&amp;gt;getRequestParameter('company_email');
        if (!$company = CompanyTable::match($email))
        {
            $this-&amp;gt;getRequest()-&amp;gt;setError('company_email', 'Your work email doesn\'t appear to belong to any of the registered companies.');
            return false;
        }
        return true;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In our &lt;code&gt;CompanyTable&lt;/code&gt; we create a static function &lt;code&gt;CompanyTable::match&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;static public function match($email)
{
    $company = Doctrine_Query::create()-&amp;gt;from('Company c, c.CompanyDomains d')-&amp;gt;where('? LIKE CONCAT(\'%\', pattern)', $email)-&amp;gt;execute()-&amp;gt;getFirst();

    return $company;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that we transparently do a join in the &lt;code&gt;-&amp;gt;from()&lt;/code&gt; statement.  If there's a match we get a company object (which we can associate with the new user) otherwise we get a &lt;code&gt;null&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Enjoy.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Saving a file to a database using symfony and doctrine</title>
   <link href="http://davedash.com/2007/07/07/saving-a-file-to-a-database-using-symfony-and-doctrine/"/>
   <updated>2007-07-07T00:00:00-07:00</updated>
   <id>http://davedash.com/2007/07/07/saving-a-file-to-a-database-using-symfony-and-doctrine</id>
   <content type="html">&lt;p&gt;[tags]symfony, doctrine, database, uploads, wordpress, fixme[/tags]&lt;/p&gt;

&lt;p&gt;I like to save content uploaded by website visitors to a database versus the file system.  It makes it easy having the data all in one spot.&lt;/p&gt;

&lt;p&gt;I tend to overcomplicate this process, so I wanted to write down the important steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In the template set &lt;code&gt;multipart=true&lt;/code&gt; for the form.  If you're using symfony helpers you can do this via:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; form_tag('@action', 'multipart=true')
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Extract the binary data you want to store in the database in your action:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; $path = $this-&amp;gt;getRequest()-&amp;gt;getFilePath('my_file');
 $size = $this-&amp;gt;getRequest()-&amp;gt;getFileSize('my_file');
 $type = $this-&amp;gt;getRequest()-&amp;gt;getFileType('my_file');    
 $data = fread(f_open($path, &quot;r&quot;), $size);

 $myObject              = new MyObject();
 $myObject['file_data'] = $data;
 $myObject['file_size'] = $size;
 $myObject['mime_type'] = $type;
 $myObject-&amp;gt;save();
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Wordpress is garbage so f_open should not have an underscore... what to do...&lt;/p&gt;

&lt;p&gt;At least that's how we role with Doctrine.  Note, Doctrine syntax is changing, and this may not be the most efficient way to create a new &lt;code&gt;Doctrine Record&lt;/code&gt; in your application.&lt;/p&gt;
</content>
 </entry>
 

</feed>

