Windows has 4 different driver implementations that could be used. In order of support:pdo_sqlsrv, pdo_sybase, pdo_mssql, and pdo_odbc.
pdo_dblib can be built against either FreeTDS (pdo_sybase) or MS SQL Server (pdo_mssql) dblib implementations. The driver is not a complete PDO driver implementation and lacks support for transactions or driver attributes.
This is a driver released in August 2010 by Microsoft for interfacing with MS SQL Server. This is a very complete PDO driver implementation and will provide the best results when using Propel on the Windows platform. It does not return blobs as a resource right now but this feature will hopefully be added in a future release. There is also a bug with setting blob values to null that Propel has a workaround for.
When built against FreeTDS dblib it will be called pdo_sybase. This requires properly setting up the FreeTDS freetds.conf andlocales.conf. There is a workaround for the lack of transactions support in the pdo_dblib driver by using MssqlDebugPDO orMssqlPropelPDO classes.
c:\freetds.conf
[global]client charset=UTF-8 tds version = 8.0 text size = 20971520
When built against MS SQL Server dblib the driver will be called pdo_mssql. It is not recommended to use the pdo_mssql driver because it strips blobs of single quotes when retrieving from the database and will not return blobs or clobs longer that 8192 characters. The dsn differs from pdo_sybase in that it uses a comma between the server and port number instead of a colon and mssql instead of sybase for the driver name.
Currently pdo_odbc cannot be used to access MSSQL with propel because of a long standing bug with the MS SQL Server ODBC Client. Last update on 8/3/2010 was that it would be resolved in a future release of the SQL Server Native Access Client. This bug is related to two php bugs (Bug #44643 and Bug #36561)
pdo_dblib is built against the FreeTDS dblib implementation. The driver is not a complete PDO driver implementation and lacks support for transactions or driver attributes. This requires properly setting up the FreeTDS freetds.conf and locales.conf. There is a workaround for the lack of transactions support in the pdo_dblib driver by using MssqlDebugPDO or MssqlPropelPDO classes.
pdo_odbc using UnixODBC and FreeTDS. This should be supported in propel but with ubuntu 10.04 and php 5.2.x any statement binding causes apache to segfault so I have not been able to test it further. If anyone has any additional experience with this please post information to the propel development group. If you would like to experiment there are some instructions you can follow here for getting it setup on ubuntu.
$conn = mssql_connect($serverName, $DBUsername, $DBPassword) or die("Couldn't connect to SQL Server on $myServer"); if (!$conn) { die('Something went wrong while connecting to MSSQL'); } ?>
Faktory is a tool for easily building test objects ala FactoryGirl, but for PHP. It's still in it's early stages, but give it a go if you're interested, and open issues for the features it's missing that you think are really important.
Installing with Composer
You can install this package via Composer on the command line from your project's root:
composer require adamwathan/faktory
Laravel 4
If you are using Laravel 4, you can get started very quickly by registering the included service provider.
Modify the providers array in app/config/app.php to include the FaktoryServiceProvider:
You can now start using Faktory by calling methods directly on the Faktory facade:
Faktory::define('User', function ($f) {$f->first_name='John';$f->last_name='Doe';});
Outside of Laravel 4
To use outside of Laravel 4, just instantiate a new Faktory. Make sure you register this as a singleton in your favorite dependency injection container, since you probably want to be using the same instance everywhere.
$faktory=newAdamWathan\Faktory\Faktory;
Note: When using outside of Laravel 4 and not having access to the Faktory facade, you will need to make sure you use your $faktory instance in any nested closures that need to generate other objects. Sucks but that's PHP.
Using Faktory
Defining factories
Define factories anywhere you want.
In Laravel 4, I've been creating a factories.php file in my tests directory and adding it to app/bootstrap/testing.php like so:
The most basic factory definition requires a class name and a closure that defines the default attributes for the factory. This will define a factory named after that class that generates instances of that same class.
Faktory::define('Album', function ($f) {$f->name='Diary of a madman';$f->release_date=newDateTime('1981-11-07');});Faktory::define('Song', function ($f) {$f->name='Over the mountain';$f->length=271;});
Using factories
Once you have your factories defined, you can very easily generate objects for your tests.
Objects can be generated using one of two different build strategies.
build creates objects in memory only, without persisting them.
create creates objects and persists them to whatever database you have set up in your testing environment.
Note: The create strategy is meant for Laravel 4's Eloquent ORM, but as long as your objects implement a save()method and a getKey() method to retrieve the object's ID, it should work outside of Eloquent.
To generate an object, simply call build or create and pass in the name of the factory you want to generate the object from.
// Returns an Album object with the default attribute values$album=Faktory::build('Album');$album->name;// 'Diary of a madman'$album->release_date;// '1981-11-07'// Create a basic instance and persist it to// the database$album=Faktory::create('Album');$album->id// 1
Overriding attributes
The real benefit of using these factories appears when you are writing a test that requires your objects to satisfy some precondition, but you don't really care about the rest of the attributes.
You can specify the values you need for the attributes that matter for the test, and let the factory handle filling out the attributes you don't care about with default data so that the object is in a valid state.
If you just need to change some simple attributes to static values, you can just pass an array of attribute overrides as a second argument:
// Create an instance and override some properties$album=Faktory::build('Album', ['name'=>'Bark at the moon']),]);$album->name;// 'Bark at the moon'$album->release_date;// '1981-11-07'
If you need to do something trickier, you can pass in a closure that provides all of the same functionality you get when actually defining the factory. This is most useful when working with relationships:
// Create an instance and override some properties$album=Faktory::build('Album', function ($album) {$album->name=>'Bark at the moon';$album->songs->quantity(4)->attributes(['length'=>267]);});$album->name;// 'Bark at the moon'$album->songs->count();// 4$album->songs[0]->length;// 267
Named factories
Factories can also be given a name, so that you can define multiple factories for the same class that generate objects in different predefined states.
To define a named factory, pass the name as the second parameter, and move the callback to the third parameter.
Faktory::define('Album', 'album_with_copies_sold', function ($f) {$f->name='Diary of a madman';$f->release_date='1981-11-07';$f->copies_sold=3200000;});$album=Faktory::build('album_with_copies_sold');get_class($album);// 'Album'$album->name;// 'Diary of a madman'$album->release_date;// '1981-11-07'$album->copies_sold;// 3200000
Factory inheritance
You can create factories that inherit the attributes of an existing factory by nesting the definition. This allows you to define a basic factory, as well as more specific factories underneath it to generate objects in a specific state without having to redeclare the attributes that don't need to change.
Faktory::define('User', 'basic_user', function ($f) {$f->first_name='John';$f->last_name='Doe';$f->is_admin=false;$f->define('admin', function ($f) {$f->is_admin=true; });});$user=Faktory::build('admin');$user->first_name;// 'John'$user->last_name;// 'Doe'$user->is_admin;// true
Lazy attributes
If you don't want an attribute to be evaluated until you try to build an object, you can define that attribute as a closure.
Lazy attributes to the rescue again. The closure also takes an autoincrementing integer as it's second parameter, which is really handy for ensuring that a field value is unique.
Faktory::define('User', function ($f) {$f->first_name='John';$f->last_name='Doe';$f->email=function ($f, $i) {return"example{$i}@example.com"; };});$user1=Faktory::build('User');$user1->email;// 'example1@example.com'$user2=Faktory::build('User');$user2->email;// 'example2@example.com'
Defining relationships
Faktory lets you easily define relationships between objects.
Currently, there is support for belongsTo, hasOne, and hasMany relationships.
Belongs to
Define a belongsTo relationship by assigning a belongsTo call to an attribute.
belongsTo() takes the name of the factory that should be used to generate the related object as the first argument, the name of the foreign key column as the second argument, and an optional array of override attributes as the third argument.
$faktory->define('Song', 'song_with_album', function ($f) {$f->name='Concatenation';$f->length=257;$f->album=$f->belongsTo('album', 'album_id', ['name'=>'Chaosphere', ]);});$faktory->define('Album', 'album', function ($f) {$f->name='Destroy Erase Improve';});// Build the objects in memory without persisting to the database$song=Faktory::build('song_with_album');$song->album;// object(Album)(// 'name' => 'Destroy Erase Improve'// )$song->album_id;// NULL// Save the objects to the database and set up the correct// foreign key associations$song=Faktory::create('song_with_album');$song->album_id;// 1Album::find(1);// object(Album)(// 'name' => 'Destroy Erase Improve'// )
Has one
Define a hasOne relationship by assigning a hasOne call to an attribute.
hasOne() takes the name of the factory that should be used to generate the related object as the first argument, the name of the foreign key column (on the related object) as the second argument, and an optional array of override attributes as the third argument.
$faktory->define('User', 'user_with_profile', function ($f) {$f->username='johndoe';$f->password='top-secret';$f->profile=$f->hasOne('profile', 'user_id');});$faktory->define('Profile', 'profile', function ($f) {$f->email='johndoe@example.com';});// Build the objects in memory without persisting to the database$user=Faktory::build('user_with_profile');$user->profile;// object(Profile)(// 'email' => 'johndoe@example.com'// )// Save the objects to the database and set up the correct// foreign key associations$user=Faktory::create('user_with_profile');$user->id;// 1Profile::first();// object(Album)(// 'user_id' => 1,// 'email' => 'johndoe@example.com'// )
Has many
Define a hasMany relationship by assigning a hasMany call to an attribute.
hasMany() takes the name of the factory that should be used to generate the related objects as the first argument, the number of objects to generate as the second argument, the name of the foreign key column (on the related object) as the third argument, and an optional array of override attributes as the final argument.
$faktory->define('Album', 'album_with_songs', function ($f) {$f->name='Master of Puppets';$f->release_date=newDateTime('1986-02-24');$f->songs=$f->hasMany('song', 8, 'album_id');});$faktory->define('Song', 'song', function ($f) {$f->title='The Thing That Should Not Be';$f->length=397;});
Relationships and build strategies
Relationships are handled differently by each build strategy.
When using the build strategy, the related object(s) will be available directly as a property on the base object.
When using the create strategy, the related object(s) will be persisted to the database with the foreign key attribute set to match the ID of the base object, and nothing will actually be set on the actual attribute itself, allowing you to retrieve the related object through the methods you've actually defined in the base object's class.
Overriding relationship attributes
If you need to override attributes on a relationship when building or creating an object, you can do so by manipulating the actual relationship attribute itself.
// Define the factories$faktory->define('Song', 'song_with_album', function ($f) {$f->name='Concatenation';$f->length=257;$f->album=$f->belongsTo('album', 'album_id');});$faktory->define('Album', 'album', function ($f) {$f->name='Destroy Erase Improve';$f->release_date=newDateTime('1995-07-25');});// Build a song but override the album name$song=Faktory::build('song_with_album', function ($song) {$song->album->name='Chaosphere';});$song->album;// object(Album)(// 'name' => 'Chaosphere'// )// Build a song but override a couple attributes at once$song=Faktory::build('song_with_album', function ($song) {$song->album->attributes(['name'=>'Chaosphere','release_date'=>newDateTime('1998-11-10'), ]);});$song->album;// object(Album)(// 'name' => 'Chaosphere'// )$song->album->release_date;// '1998-11-10'
Building multiple instances at once
You can use buildMany and createMany to generate multiple objects at once:
// Create multiple instances$albums=Faktory::buildMany('Album', 5);// Create multiple instances with some overridden properties$songs=Faktory::buildMany('Song', 5, [ 'length'=>100 ])$songs[0]->length;// 100// ...$songs[4]->length;// 100// Add a nested relationship where each item is different$album=Faktory::build('Album', ['name'=>'Bark at the moon','songs'=> [Faktory::build('Song', [ 'length'=>143 ]),Faktory::build('Song', [ 'length'=>251 ]),Faktory::build('Song', [ 'length'=>167 ]),Faktory::build('Song', [ 'length'=>229 ]), ],]);// Add a nested relationship where each item shares the same// properties$album=Faktory::build('Album', ['name'=>'Bark at the moon','songs'=>Faktory::buildMany('Song', 5, [ 'length'=>100 ] ),]);// Add a nested relationship where each item is different,// but using buildMany$album=Faktory::build('Album', ['name'=>'Bark at the moon','songs'=>Faktory::buildMany('Song', 4, ['length'=> [143, 251, 167, 229] ]),]);// Add a nested relationship using buildMany, but wrap// it in a collection$album=Faktory::build('Album', ['name'=>'Bark at the moon','songs'=>function () {returnnewCollection(Faktory::buildMany('Song', 4, ['length'=> [143, 251, 167, 229] ])); }]);