php oil package install kohana-orm
We'll be adding magic methods (__call and __callStatic) so you can do fancy things like find_by_username_and_password('ben', 'somepassword');
// Additionally, you can use the orm as follows: $clients = ORM::factory('client')->find_all_by_last_name('corlett'); foreach ($clients as $client) { echo nl2br($client->get_first_name() . PHP_EOL); }the find_all_by you'll recognise but i've also added a method where you can call get_ on a property. Not only does it return false if the property isn't set (as opposed to throwing an error, but it also returns false if empty). For example:
if ($client->get_first_name()) { // Do something }
find_first_by_first_name('ben'); find_first_by_last_name('corlett'); find_all_by_first_name_and_last_name('ben', 'corlett'); find_first_by_first_name_and_last_name('ben', 'corlett'); find_all_by_first_name_or_last_name('ben', 'corlett'); find_first_by_first_name_or_last_name('ben', 'corlett'); // etc. $clients = ORM::factory('client')->find_all_by_last_name('corlett'); $client->put_first_name('mike')
$client = ORM::factory('client')->{{now put the magic method}}
$client = ORM::factory('client')->find_by_first_name_and_last_name('ben', 'corlett'); // pointless 'if' statement, just for demo if ($client->get_first_name() == 'ben') { $client->first_name = 'joe'; } $client->save();It will soon be:
$client = ORM::factory('client')->find_by_first_name_and_last_name('ben', 'corlett'); // pointless 'if' statement, just for demo if ($client->get_first_name() == 'ben') { $client->set_first_name('joe'); } $client->save();
$client->set_first_name('');
$client->set_password("MD5('somepassword')"); $client->set_post_edited('NOW()');
------------------------------------------------------------------------------------- To install this package: 1. Through OIL: php oil package install kohana-orm 2. Manually: 1. Download from https://github.com/TJS-Technology/fuel-kohana-orm 2. Copy to DOCROOT/packages/ 3. Go to DOCROOT/app/config.php and find: 'packages' => array( //'activerecord', ), And add 'orm' to array: 'packages' => array( //'activerecord', 'orm', ), 4. Ensure correct database settings are provided in DOCROOT/app/config/db.php ------------------------------------------------------------------------------------- Using the ORM: // DOCROOT/app/class/controller/welcome.php class Controller_Welcome extends Controller { public function action_index() { $clients = Model_Client::factory()->where('last_name', '=', 'Corlett')->find_all(); // Or: // $clients = new Model_Client(); // $clients = $clients->where('last_name', '=', 'Corlett') // ->find_all(); foreach ($clients as $client) { echo nl2br($client->first_name . PHP_EOL); $client->first_name = 'Another name'; $client->save(); } } // Additionally, you can use the orm with magic methods as follows $clients = Model_Client::find_all_by_last_name('Corlett'); // Or: // $clients = Model_Client::factory()->find_all_by_last_name('Corlett'); // Or: // $clients = new Model_Client(); // $clients = $clients->find_all_by_last_name('Corlett'); foreach ($clients as $client) { // Same as $client->first_name except it doesn't throw // An error if not existent, just returns false. if ($client->get_first_name()) { // As opposed to $client->first_name = 'Another name'; $client->set_first_name('Another name'); $client->save(); } } } // DOCROOT/app/classes/model/client.php class Model_Client extends \ORM { } See http://kohanaframework.org/guide/orm for full usage instructions (magic methods are not available in original ORM). ------------------------------------------------------------------------------------- Note: The reason the original method of loading an ORM model ($something = ORM::factory('client')) has been removed is because of namespacing. Without making a heap of assumptions, we cannot load the correct model. Say you've got the following app structure: app/ ---/classes/ -----------/models/client.php // And under modules -----------/somemodule/ ----------------------/classes/ ------------------------------/models/client.php Q: If you went ORM::factory('client'), which module would it load? A: The one in the same namespace (only if that namespace is the global namespace - no module support). The way it's structured now, you can load a model from any module, for example (assuming you have the same structure as above): // DOCROOT/app/classes/controller/welcome.php class Controller_Welcome extends Controller { public function action_index() { // This will use the model under app/classes/models/client.php $clients = Model_Client::factory()->find_by_first_name('ben'); // now let's say you've got a module called `somemodule`, and it's // also got a model called `client`. How do we load this? this is // how: $clients = Client\Model_Client::factory()->find_by_first_name('ben'); // ^^^^ notice the namespace } } For the namespacing (hmvc) support you need to enable your module in DOCROOT/app/config/config.php (around line 130 at the time of writing this). If you have any questions, you can reach me on twitter @ben_corlett or on fuel forums (http://fuelphp.com/users/profile/view/29).
// now let's say you've got a module called `somemodule`, and it's // also got a model called `client`. How do we load this? this is // how: $clients = Somemodule\Model_Client::factory()->find_by_first_name('ben'); // ^^^^ notice the namespace } }
// This will load model client (in same namespace as caller) ORM::factory('client'); // This won't work ORM::factory('\Client'); // Global namespace ORM::factory('Somemodule\Client'); // calling another namespace from a global namespace
$something = Model_Client::factory()->where('age', '=', 19)->find_all(); // or from a different namespace $something = Somemodule\Model_Client::factory()->where('age', '=', 19)->find_all();
public function __call($method, array $arguments)
Fuel\Core\Autoloader::add_classes(array( // Add classes you want to override here // Example: 'View' => APPPATH.'classes/view.php', 'ORM' => APPPATH . 'classes/orm.php', );
Error - 2011-02-04 10:36:06 --> 2048 - Declaration of ORM\ORM::factory() should be compatible with that of ORM\Kohana_ORM::factory() in /Users/ben/Sites/Projects/tjs/wfm/fuel/packages/kohana-orm/classes/orm.php on line 14weren't you? I fixed that just then by instead of returning new static it returns new ORM. My apologies I thought you didn't understand the whole namespace fix. I've since implemented your fixes and will push to github in a minute.
Error - 2011-02-04 10:44:28 --> 2048 - Declaration of ORM\ORM::factory() should be compatible with that of ORM\Kohana_ORM::factory() in /Users/ben/Sites/Projects/tjs/wfm/fuel/packages/kohana-orm/classes/orm.php on line 14
// NOTE THE DOUBLE BACK SLASHES LIKE IN ALL BOOTSTRAP.PHP CLASSES!!!! IMPORTANT // Think about it, \n is a new line. $clients = \ORM::factory('\\Model_Client')->find_all(); // load out of a module $clients = \ORM::factory('Somemodule\\Model_Clients')->find_all_by_last_name('corlett');
// DOCROOT/app/class/controller/welcome.php class Controller_Welcome extends Controller { public function action_index() { $clients = Model_Client::init()->where('last_name', '=', 'Corlett')->find_all(); // Or: // $clients = new Model_Client(); // $clients = $clients->where('last_name', '=', 'Corlett') // ->find_all(); foreach ($clients as $client) { echo nl2br($client->first_name . PHP_EOL); $client->first_name = 'Another name'; $client->save(); } } // Additionally, you can use the orm with magic methods as follows $clients = Model_Client::find_all_by_last_name('Corlett'); // Or: // $clients = Model_Client::factory()->find_all_by_last_name('Corlett'); // Or: // $clients = new Model_Client(); // $clients = $clients->find_all_by_last_name('Corlett'); foreach ($clients as $client) { // Same as $client->first_name except it doesn't throw // An error if not existent, just returns false. if ($client->get_first_name()) { // As opposed to $client->first_name = 'Another name'; $client->set_first_name('Another name'); $client->save(); } } // Due to popular demand, you can use the old factory method, // Although this is NOT recommended. For example $clients = \ORM::factory('\\clients')->find_all(); // Note, you need to provide the namespace for the model to be // loaded using the factory method, replacing single slashes with // double slashes (the same as in all your bootstrap.php files). } // DOCROOT/app/classes/model/client.php class Model_Client extends \ORM { // Relationships are defined as follows: // Same namespace protected $_has_many = array( 'cars' => array( // Yes, I know in Kohana you would // have just put 'Client_Car', but // the orm now works out what namespace // you're in if you don't provide it one, // and having to add the 'Model_' prefix // is just too many assumptions. 'model' => 'Model_Client_Car' )); // Different namespace protected $_has_many = array( 'cars' => array( // Notice the double backslashes! 'model' => 'Somemodule\\Model_Client_Car' )); }
Model_User::instance()->directly_chainable();
static function instance(){ return new static; }
It looks like you're new here. If you want to get involved, click one of these buttons!