HOWTO: CONVERT A PROPEL OBJECT TO AND FROM ARRAYS (HASHES)
Propel contains methods to convert to and from array, but they aren't generated by default. To get
toArray()andfromArray(), add the following to build.properties:propel.addGenericAccessors = true # gets you toArray() propel.addGenericMutators = true # gets you fromArray()Regenerate your classes.
Now you can:
$obj->toArray(BasePeer::TYPE_FIELDNAME) // see BasePeer for other // key styles(
fromArray()presumably works analogously, but I haven't tried it.)
HOWTO: SELECT DATABASE AT RUNTIME
(For example, if you want to connect to different databases from dev,
stage and live sites and don't want to, or can't modify
runtime-conf.xml.)
Instead of initialising Propel via:
Propel::init('/path/to/myproject-conf.php');
do something like:
// $config comes from the '/path/to/myproject-conf.php'
// (as built by propel-gen)
$config = array(
'propel' => array(
'datasources' => array(
'skirepublic' => array(
'adapter' => 'mysql',
'connection' => array(
'phptype' => 'mysql',
'database' => Config::get("database"),
'hostspec' => Config::get("hostname"),
'username' => Config::get("username"),
'password' => Config::get("password")
),
),
'default' => 'skirepublic',
),
),
);
Propel::setConfiguration($config);
Propel::initialize();
GOTCHA: EMPTY OBJECTS AREN'T SAVED TO THE DATABASE
Completely empty objects don't get saved:
$p = new Person();
$p->save(); // this doesn't save anything to the database
You need to make some change to the Person first:
$p = new Person();
$p->setFirstName("Clem");
$p->save(); // this will commit to the database
GOTCHA: DELETE() DOES AN IMPLICIT SAVE()
The delete() method does an implicit save(); it doesn't (for example)
set a flag that's acted upon by a save().
GOTCHA: DELETES PROPAGATE ACROSS OBJECTS
Deletes propagate in the database, but not in their representative objects. So, for example, if you have:
$album = new Album();
$track = new Track();
$track->setName("XXX");
$album->addTrack($track);
$album->save(); // album and associated track written to database
assertFalse($album->isDeleted());
assertFalse($track->isDeleted());
$album->delete();
assertTrue($album->isDeleted());
assertTrue($track->isDeleted()); // this will FAIL!
GOTCHA: COUNT*() RETURNS THE DATABASE COUNT, NOT THE OBJECT COUNT
The count[Column]() method returns the number of relevant items in the
database, not the number of relevant items in the object. (i.e.
count*() doesn't flush all changed objects to disk before doing the
count.) So, for example:
$album = new Album();
$album->save();
assertEqual($album->countTracks(), 0);
$album->addTrack(new Track());
$album->addTrack(new Track());
$album->addTrack(new Track());
assertEqual($album->countTracks(), 0); // still 0!
$album->save();
assertEqual($album->countTracks(), 3); // now it's 3
HOWTO: GET A DATABASE HANDLE
i.e. the equivalent of mysql_connect().
If you're using MySQL, it's probably:
Propel::getConnection()->getResource()
The mysql_query() will use the current connection by default, though,
so if a connection has already been established by Propel and you're
happy to be lazy, you can do mysql_query(...) without specifying a
database connection and it will do the right thing.
HOWTO: GENERATE A schema.xml FROM AN EXISTING DATABASE
Haven't tried it, but see
http://propel.phpdb.org/trac/wiki/Users/Documentation/1.2/HowTos/ExistingDatabases